2.1 Customizing the Form

2.1 Customizing the Form

When you start a new Visual Basic 2022 project, the VB2022 IDE will display the default form along with the Solution Explorer window and the Properties window, as shown in Figure 2.1. The name of the default form is Form1. The properties window displays all the properties associate with Form1 and their corresponding attributes or values. You can change the name of the form, the title of the form using the text property, the background color, the foreground color, size and more. Try changing the properties as shown in Table 2.1

Table 2.1

Property

Value

Name

MyForm

Text

My First VB2022 App

BackColor

LavenderBlush

ForeColor

Crimson

MaximizeBox

False

 

You do not have to type in the color manually, you can select a color from the color drop-down list that comprises three tabs, Custom, Web, and System, as shown in Figure 2.1. Clicking the drop-down arrow will bring out a color palette or a list of color rectangles where you can select a color.

               
Figure 2.1



Another method of setting the colors is to manually type the RGB color code or the hex color code. The values of R, G and B ranges from 0 to 255, therefore, by varying the values of the RGB we can obtain different colors.

The hex color code system uses a six-digit, three-byte hexadecimal number to represent colors. The bytes represent the red, green and blue components of the color. One byte represents a number ranging from 00 to ff, or 0 to 255 in decimal notation. For example, #0000ff represents the cyan color. However, when you type the Hex color code in the properties window, it automatically converts the color to RGB color. Figure 2.2 shows a list of Hex color codes and the corresponding colors.


Figure 2.2 Hex Color Codes

The design interface is shown in Figure 2.3 and the runtime interface is shown in Figure 2.4. On the runtime interface, notice that the title has been changed from Form1 to My First VB 2022 Application, background color changed to Lavender Blush, the text OK color is Crimson and the window cannot be maximized.


Figure 2.3  Design UI


                                            Figure 2.4 Runtime UI

You can also change the properties of the form at runtime by writing relevant codes. The default form is an object, and an instant of the form can be denoted by the name MeThe property of the object can be defined by specifying the object’s name followed by a period, as follows:

ObjectName.property

For example, setting the background of the form to blue using the following code:

Me.BackColor=Color.Blue

In addition, you can use the FromArgb method to specify the color using the RGB codes, as follows:

Me.BackColor = Color.FromArgb(0, 255, 0)

Example 2.1 Changing Properties at Runtime

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles_ MyBase.Load
Me.Text = "My First Visual Basic 2022 Application"
Me.BackColor = Color.Turquoise
Me.ForeColor = Color.Ivory
MyBtn.BackColor = Color.DodgerBlue
Me.MaximizeBox = False
Me.MinimizeBox = True

End Sub


To runtime UI is shown in Figure 2.5. Notice that it is now different from that shown in Figure 2.4,

 

 Figure 2.5

In the place of Turquoise, you can use RGB code as follows:

Me.BackColor = Color.FromArgb(64,224,208)

In addition, you can specify the size, the opacity  and the position of the default form using the code, as in Example 2.2

Example 2.2 Customizing the Form

Private Sub Form1_Load(sender As Object, e As EventArgs Handles MyBase.Load


Me.Text = "My First VB2022 App"
Me.BackColor =Color.Beige
Me.MaximizeBox = False
Me.MinimizeBox = True
Me.Size = New Size(400, 400)
Me.Opacity = 0.85
Me.CenterToParent()


End Sub

The property Opacity sets the degree of transparency. The runtime interface is as shown in Figure 2.6



Figure 2.6

Post a Comment

0 Comments