VB.NET working with Windows Form and Events in Visual Basic .NET

In this article, I will explain you about working with Windows Form and Events in Visual Basic .NET.
  • 2389

Working with Forms 

Now we can start working with forms. When we open a new form we can see the default properties of the form by selecting View->Properties Window (F4). The properties window opens with default properties set to form by the .NET Framework

Briefly on Properties

Appearance

When we want to change the appearance of the form then we use Appearance properties. Appearance properties can change a background color, background image, border style, change the cursor, set the font for the text and so on.

Behavior

By setting Behavior property to True or False we can enable or disable the form notable this property is the enabled property.

Layout

When we want to change the structure of the form then we use layout properties. With these properties we can set the location, maximum size, minimum size, exact size of the form with the size property. Layout properties let use specify the location of the form where it should appear when we run the application which we can select from a predefined list.

Window Style

ControlBox property comes under Window style properties which by default is True. If we want that minimize, maximize and the cancel buttons become invisible on the form. Set this property to False.

Form Event

The default event of a form is the load event which looks like this in code:

Public Class Form1
 
    Private Sub Form1_Load(ByVal sender As System.ObjectByVal e As System.EventArgsHandlesMyBase.Load
 
    End 
Sub
End Class

You can also write code in the load event of the form, I show you how:

Public Class Form1
 
    Private Sub Form1_Load(ByVal sender As System.ObjectByVal e As System.EventArgsHandlesMyBase.Load
        MessageBox.Show("Press this button to load the form")
    End 
Sub
End Class

When you write this code in the load event and by selecting Debug->Start from the main menu(F5) to run the application, the message box is displayed on your screen with a text you written in the Show() method and button named OK. It looks like the image below:

img1.gif
 

When you press the OK button on the message box the blank form is displayed. It looks like the image below:

img2.gif
 

Example:

Now, add a TextBox and a Button to the form from the toolbox. The toolbox can be selected fromView->ToolBox on the main menu or by holding Ctrl+Alt+X on the keyboard. Once adding the TextBox and Button to the form. We need to write an event for the Button stating something should happen when you click it. To do that get back to design view and double-click on the  Button. Doing that opens an event handler for the Button where you specify what should happen when you click the button. Now that code is looks like this:


Public
 Class Form1
 
    Private Sub Form1_Load(ByVal sender As System.ObjectByVal e As System.EventArgsHandlesMyBase.Load
        
    End Sub
 
    Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs)Handles Button1.Click
         
    End 
Sub
End Class

Now place the code TextBox1.Text = "This is my first Form" in the Click event of the Button and run the application. When you click the Button the output "This is my first Form" is displayed in the TextBox.

 

Example:


Public
 Class Form1
 
    Private Sub Form1_Load(ByVal sender As System.ObjectByVal e As System.EventArgsHandlesMyBase.Load
 
    End Sub
 
    Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs)Handles Button1.Click
        TextBox1.Text = "This is my first Form"
    End 
Sub
End Class

Image of the Output is given below:

img3.gif

Alternatively, you can also use the MsgBox or MessageBox functions to display text when you click on the Button. To do that place this line of code, MsgBox("This is my first Form") orMessageBox.Show("This is my first Form") in the click event of the Button and run the application. 

Summary:

I hope this article help you to understand about how to working with Windows Form and Events in Visual Basic .NET.

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.