VB.NET 1.1 Tutorial - Hello Mum - Windows Style - Part 1Programs in windows have a Graphical User Interface (GUI), which can be seen in use in any windows program. The GUI consist of graphics objects drawn on the screen that the user can interact with. Windows programs are usually event driven programs, that is to say that they mainly only do what the user wants, when they want. For example, when typing in a text editing program, text appears on the screen. In Visual Basic .Net, all graphics are drawn on a Windows Form. So our first program will display a blank form with the heading "Hello Mum". Imports System
Imports System.Windows.Forms
Namespace HelloFrm
Public Class HelloFrm
Inherits Form
'Run the application
Copy and paste the text into notepad and save the file as helloFrm.vb. Alternatively, you can download the file helloFrm.vb from the link.If you have Visual Studio, you can download the solution here. Now open a Command Line in the directory where you saved the file and type the following. Alternatively, you can download my batch file here. vbc /t:winexe /r:system.dll,system.windows.forms.dll hellofrm.vb Close the command line and run the helloFrm.exe file just like any other executable in windows. You can download my copy of the executable from here.
Blank Windows Application with Heading Imports and NamespacesIn the namespaces area, we include Imports System.Windows.Forms. It is this namespace that includes the information to allow us to create a form. See the reference section at the bottom of the page for more information on System.Windows.Forms. We then decalre our own Namespace called HelloFrm. This allows us to group more than one class together in a logical manner. Class DeclarationA class is defined using the keyword Class followed by the name of the class we are creating. All classes are declared as Public, regardless of whether we add the access modifier or not. Public Class HelloFrm Inherits Form We inherit our HelloFrm class from the System class Form. This is done by using the Inherits keyword after the class name accompanied by the class from which it is inherited. This can be done on the line after the class declaration or compiler errors will occur. Alternatively we can use a colon (:) if we wish to put the code on a single line. Public Class HelloFrm : Inherits Form To define the scope of the class, we enclose all of the class code between the class declaration and the following line: End Class For a simple overview about inheritance, click here. Main ExecutionThis code starts the application by calling the static Run() method of the Application class found in the System.Windows.Forms namespace, accepting as a parameter the startup form of the application. 'Run the application <STAThread()> Shared Sub Main() Application.Run( New HelloFrm() ) End Sub The first line after the comment contains a single piece of information, <STAThread()>. This means that there is only one thread running that will accomplish all of the tasks for the application. The Shared keyword means that Main() method is not associated with a specific instance of a class. The Application.Run method processes messages from the operating system to the application. If you comment out the line of code, the application will compile and execute, but because it is not in the message loop, it will exit after an instance of the form is created. The argument passed to the Run method is the name of our form with the keyword New prefixing it. This calls the constructor of the form. ConstructorA constructor is the first sub-routine that is invoked upon instantiation of a class. The Sub New procedure acts as the constructor, and is used to initialize the object (instance of a class). The constructor should always have the access modifier Public, (for more information see access modifiers). Public Sub New() MyBase.New() . . . End Sub All classes in Visual Basic.NET are derived from the Object class. Therefore, when creating a class, you need to call the constructor of the Object class. To do this, you must include the MyBase.New statement as the first line in the constructor of your class. As previously mentioned, the MyBase keyword is used to invoke code written in a base class from a derived class. After the MyBase.New statement, you can add code to initialize objects and variables that you create. Adding Some TextNow we shall add the text "Hello Mum!" into the Caption Heading. This is done in the constructor of the HelloFrm class. Public Sub New() MyBase.New() Me.Text = "Hello Mum" End Sub The Me Keyword is used to reference the current instance of a class from within the class module. So here, Me refers to the instance of the HelloFrm class. To access Properties and Functions/Sub-routines of a class we use dot notation (.), shown here as Me.Text. All Properties and Functions/Sub-routines of the class can be referenced this way. Here we are setting the caption of the Form to "Hello Mum". ReferencesFor more information on the System.Windows.Forms namespace, visit MSDN at microsoft here. For more information on the Namespace statement, visit MSDN at microsoft here. For more information on the Class statement, visit MSDN at microsoft here. For more information on the Application class, visit MSDN at microsoft here. What Next?Next we add some text to the Form area. Return to the Tutorial Contents. |