VB.NET 1.1 Tutorial - Hello Mum - Windows Style - Part 2In the last section we created an empty form with a title. In this version, we shall add some text to the form by adding a label to the form. Adding a LabelIn order to add the label, we change our first program in two ways. First we declare an instance of a label object, and then we do something with it in the constructor of the class in order to display it. Below is the complete listing of the new program. In order to be able to download each file I have changed the class name from HelloFrm to HelloLabel. Listing 4-1 - Windows Application with Label Imports System
Imports System.Drawing
Imports System.Windows.Forms
Public Class HelloLabel
Inherits Form
Private lblHello As Label
'Run the application
<STAThread()> Public Shared Sub Main()
Application.Run( New HelloLabel() )
End Sub
Public Sub New()
MyBase.New()
Me.Text = ""
lblHello = New Label()
With lblHello
.Location = New Point( 24, 16 )
.Size = New Size( 150, 24 )
.Font = New Font( "MS Sans Serif", 20 )
.Text = "Hello Mum!"
.TextAlign = ContentAlignment.MiddleCenter
End With
Controls.Add(lblHello)
End Sub
End Class
Copy and paste the text into notepad and save the file as HelloLabel.vb. Alternatively, you can download the file HelloLabel.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.drawing.dll,system.windows.forms.dll hellolabel.vb Close the command line and run the HelloLabel.exe file just like any other executable in windows. You can download my copy of the executable from here.
Figure 4-1 - Windows Application with Label Adding the LabelPrivate lblHello As Label The above line declares an instance of a Label and assigns it to the variable lblHello. See references for more information. Drawing the LabellblHello = New Label() With lblHello .Location = New Point( 24, 16 ) .Size = New Size( 150, 24 ) .Font = New Font( "MS Sans Serif", 20 ) .Text = "Hello Mum!" .TextAlign = ContentAlignment.MiddleCenter End With Controls.Add( lblHello ) The first programming line in the above code instantiates the label using the New keyword. Next we then set up the attributes of the label. So that we do not have to keep referring to the same object, we use the With keyword. The Location attribute sets the top-left corner of the Label to a position, in pixels, with reference to the top-left corner of the application's main window (not including the caption heading bar). This is done by specifying a new Point object on the form using the Point keyword. The Size attribute sets the width and height in pixels. This is done by specifying a new Size object by using the Size keyword. In this case, the width is 150 and the height is 24. The Font attribute sets the font of the text on the label. This can be set to any font on the machine, though beware that unless you supply the font with the application, make sure that you choose a common font. This is done by specifying a new Font object. The Text attribute defines what is written on the label and is simply a string of text. In this case it is set to "Hello Mum!". The TextAlign attribute defines the alignment of the text within the label. This is done using the ContentAlignment enumeration. As we want the text completely central we set it to MiddleCenter. Once we have set up all of the label attributes, we add the label to the form using the Controls class of the Form, and simply using the method Add() and passing to it the reference of our label. ReferencesFor more information on the System.Drawing namespace, visit MSDN at microsoft here. For more information on the As keyword, visit MSDN at microsoft here. For more information on the Label class, visit MSDN at microsoft here. For more information on the Point structure, visit MSDN at microsoft here. For more information on the Size structure, visit MSDN at microsoft here. For more information on the Font class, visit MSDN at microsoft here. For more information on the ContentAlignment enumeration, visit MSDN at microsoft here. For more information on the Controls.Add method, visit MSDN at microsoft here. What Next?Next we will create the same program, but we will draw the text directly on to the windows form. Return to the Tutorial Contents. |