Saturday, November 24, 2012

How create a form from scratch in Visual Basic

How create a form from scratch in Visual Basic


You need to know how to create a form from nothing in Visual Basic. Yes is possible, but I won’t do it from scratch. I will only show you a few earlier steps and I hope this could give you the information you are looking for.


Add the reference for the form.


You absolutely need to make sure the reference System.Windows.Forms is part of you project. You could add it directly from Visual Basic Express or Visual Studio. You could add it dynamically but is just a little bit more complicate.



With the library, you will have access to all the stuff useful to create your windows forms.

You have to choose your startup object, is not realy important what type of application you choose since this is a sample.

I made a video earlier and I choose the worst choice: the console Application. A console application is from my point of view the most prehistoric type of environment. But the basics are the same. Add the reference and declare it.



Create the form object 


Inside the main function, you could have something like this:

Module Module1

    'define you form object
    Private oForm As System.Windows.Forms.Form

    Sub Main()


        Try

            'create the form object with NEW
            oForm = New System.Windows.Forms.Form

            'i am printing some information in the console
            Console.WriteLine("will show form")

            'i put information or set the form
            'but of course, you could do and add all the suff in the form
            'before or after, is up to you
            oForm.Text = "Form title"
            oForm.ShowDialog() ' the form is displayed

            'you have to STOP the program fron exiting
            'or the program will close the form.
            'this is of course a very simple and basic program.

            Do 'this is a simple loop, the user press exit to quit.
                Console.WriteLine("type exit to quit")
            Loop Until Console.ReadLine = "exit"
        Catch ex As Exception

        End Try
    End Sub

End Module




When you run the program, the form is created and displayed. This is very simple.









If you like this post, leave a comment or share it.

Reference



Download the sample project: SampleCreateFormDynamically.zip
Use the same Visual Studio than mine: Visual Studio 2010 Professional
Get the latest version available : Visual Studio Professional with MSDN 2012






1 comment: