programming languagesVb.net programming

Vb.net Tutorial: Visual basic Form Properties, with examples

Managing Forms:

Vb.net Tutorial: Visual basic Form PropertiesForm is the fundamental object in the development of a visual basic project. All controls of graphical user interface are typically placed on a form such as labels, textboxes and buttons etc. A form appears automatically when a new project is created. The user can add as many forms as required in the application.


Parts of Form:

Different parts of a form are as follows:

Border: The border of the form changes the look of the form. Visual basic provides different types of border styles that can be applied to any form.

Title Bar: The title bar is the colored bar on the top of the form. The form can be moved from one place to another by dragging the title bar with the mouse. You can also double click it to Maximize/Restore the form.

Control Menu: Control menu is also used to close, minimize, maximize or move the form. It appears when the user click the icon on the left side of the title bar.

vb.net tutorial form properties

Minimize: This button is used to minimize the form. When the form is minimized, it is shown only on the taskbar.

Maximize: This button is used to enlarge the form to full size if the form is in normal size.

Restore: This button is used to change the form back to its normal form.

Close: This button is used to close the form.



Properties of Form:

Some important properties of the form are as follows:

Accept Button:

The accept button property is used to specify the default accept button. The accept button is the button that is automatically activated when enter key is pressed no matter which control has the focus at that time. The button with the caption Button1 is usually set as accept button for a form.

vb.net tutorial form properties

Auto Scale Mode:

The auto scale mode property is used to specify how the controls on the form are scaled to the height of the current font. The default value of this property is font. This property affects the controls on the form.

vb.net tutorial form properties

Different values of this property are as follows:

None: It indicates that automatic scaling is disabled.

Font: It indicates that the controls on the form are scaled relative to the size of the font.

Dpi: It stands for dots per inch. It indicates that the controls on the form are scaled relative to the display resolution.

Inherit: It indicates that the controls are scaled according to the auto scale mode property of their parent class.


Auto Scroll:

The auto scroll property is a Boolean property. It is used to specify whether the scroll bars automatically appear on the form when all controls are not visible.

Auto Scroll Min size:

The auto scroll min size property is used to specify the minimum size of the form before the scroll bars appear on it.

Back Color:

The back color controls the background color of the form. The default value of this property is control that displays a light gray color. The user click the drop down arrow to display three tabs that divide the color in categories.

vb.net tutorial form properties

vb.net tutorial form properties

vb.net tutorial form properties

Cancel Button:

The cancel button property is used to specify the default cancel button. The cancel button is the button that is automatically activated when Ese key is pressed. The button with the caption cancel is usually set as cancel button for a form.

Control Box:

The control box property is a Boolean property. It is used to specify whether the control button will appear on the forms title bar or not. The default value of this property is true. It can be set to false to hide the control button from the title bar of the form. This property overrides the values of minimize box and maximize box properties.


Enabled:

The enabled property is a Boolean property. The form responds to the events if it is set to true. Otherwise, the form does not respond to any event and its working is disabled.

Font:

The font property specifies the type, style and size of the font used to display the text on the form. A font is the general shape of the characters in the text. The default font for form is Microsoft sans serif. The font styles include regular, bold and italic. The font size is typically measured in points where one point is equal to 1/72 of an inch.

vb.net tutorial form properties

ForeColor:

The fore color property indicates the color of the text displayed on the form.

Form Border Style:

The Form Border Style property is used to specify the border style of the form. There are five predefined values of this property.

vb.net tutorial form properties

None: This option does not display minimize, maximize and restore button. The form has no border.

Fixed Single: The form size is fixed and uses a single line border. The form is displayed with minimize, maximize and close buttons on its title bar. The form cannot be resized by its edges or corners.


Fixed 3D: This option displays the form as a window with a fixed visible border raised relative to the main area. This setting allows users to minimize and close the window.

Sizable: This is default setting. The form can be resized by dragging its borders. The form is displayed with minimize, maximize, and close button on its title bar.

Fixed Dialog: The form can be moved or closed but cannot be resized.

Fixed tool Window: It is similar to fixed dialog but also has a close button. It does not appear on the taskbar.

Sizable Tool Window: It is same as fixed tool window but it is resizable. It dost not include maximize or minimize button and dost not appear on the taskbar.

Icon: The icon property is used to specify the picture icon that appear on left corner of forms title bar. The icon appears on the taskbar when the form is minimized.

Maximize Box: The maximize box property is a Boolean property. It is used to specify whether the maximize button will appear on the forms title bar or not. The default value of this property is true. It can be set to false to hide the maximize button from the title bar of the form.

Maximum Size: The maximum size property is used to specify the maximum size of a form. The user can set the maximum height and width for the form. The form can be resized at run time up to the maximum dimensions given in this property. The default value 0,0 indicates that there is no restriction on the size and the user can maximize the form as desired.

Minimize Box: The minimize box property is a Boolean property. It is used to specify whether the minimize button will appear on the forms title bar or not. The default value of this property is true. It can be set to false to hide the minimize button from the title bar of the form.

Minimum Size: The minimum size property is used to specify the minimum size of a form. The user can set minimum height and width for the form. The form can be resized at run time up to the minimum dimensions given in this property. The default value 0,0 indicates that there is no restriction on the size and the user can minimize the form as desired.

Name: The name property specifies the name of the form. The name is used to refer to the form in code. The name of all controls must be unique. The default name for the first form in the project is form1.

Opacity:

The opacity property is used to specify opaqueness of the form to make it partially transparent. It is given as a percentage where 100 percent means the form is completely opaque. The value of 0 percent means that the form is completely transparent. But in my case I set the opacity value 50%.

vb.net tutorial form properties



Size:

The size property is used to specify the size of the form. The default size is 300 pixels by 300 pixels shown as 300, 300. The first number specifies the width and the second number specifies the height. The plus sign next to the property name can be expanded to see the width and height settings.

vb.net tutorial form properties

Size Grip Style:

The size grip style property is used to specify the style of the sizing handle to display in the lower – right corner of the form. Different values of this property are as follows:

Auto: The size grip is displayed as needed.

Show: The size grip is displayed at all times.

Hide: The size grip is not displayed but users can resize the form with the mouse.

Start Position:

The start position property is used to specify the initial of the form when it appears on the screen at runtime. The default value is windows default location.

Different values of this property are as follows:

Center Parent: The form is centered in the area of its parent form.

Center Screen: The form is centered on the monitor.

Manual: The location and size of form will determine its starting position.

Windows Default Bounds: The form is positioned at the default location and size determined by windows.

Windows Default Location: The form is positioned at the windows default location and has the dimensions set at design time


Text:

The text property of a form indicates the text that appears in the title bar of the form. The text does not appear if form border style property is set to none.

Top Most:

The top most property is Boolean property. It is used to specify whether the form will remain on top of all other forms in the application. Its default value is false.

Window State:

The window state property is used to specify the initial visual state of the form.

Different values of this property are as follows:

Normal: It is the default value of this property. It indicates that the form will appear in the same dimensions it was designed.

Minimized: It indicates that the form will appear as minimized at startup.

Maximized: It indicates that the form will appear as maximized at startup.

Form Events:

Some important events of the form are as follows:

Activated: The activated event occurs when a form becomes the active window. It occurs just after the load event as the control is passed to the form. This event is also triggered each time the form gets the focus.

Deactivate: The deactivate event occurs when the form is not the active form and another form becomes the active window. For example, it occurs when the user clicks on another window.

Double Click: The double click event occurs when the user double clicks the form.

Drag Drop: The drag drop event occurs when a drag operation over the form completes.

Drag Over: The drag Over event occurs during a drag operation over the form.

Form Closing: The form closing event occurs when the form is about to close.

Form Closed: The form closed event occurs when the form is closed.

Load: The load event occurs when the form loads in the memory and before it appears on the screen. It is the most commonly used event of the form. It is the default event of the form and appears when the user double clicks the form in designer window.

vb.net tutorial form properties

Mouse Enter: The mouse enter event occurs when the mouse first moves over the form.

Mouse Hover: The mouse hover event occurs when the mouse remains stationary over the form for a while. It is raised once when the mouse first hovers and is not raised again until the mouse leaves the form and returns. The mouse moving over forms control is also considered as leaving away from the form.

Mouse Leave: The mouse leave event occurs when the mouse leave the form.

Mouse Move: The mouse move event occurs when the mouse moves while over the form.

Mouse Up: The mouse up event occurs when the user releases a mouse button.

Resize: The Resize event occurs when the user resizes the form.


Example1: how to use multiline in visual basic

The following program changes the size of a textbox when the form size changes.

  1. Start Visual studio.
  2. Click new project button on the toolbar OR click File > New project…. A new project dialog box will appear.
  3. Click visual basic in installed templates box to the left side of window.
  4. Click windows under visual basic option. A list of templates will appear in the templates pane in the center section of window.
  5. Select windows forms application in the center section of the window.
  6. Double click textbox control in the toolbox. A textbox will appear in the top left of the form.

vb.net tutorial form properties

  1. Set multiline property of textbox1 to true.

vb.net tutorial form properties

  1. Add following code in resize event of the form.

vb.net tutorial form properties

Public Class Form1

    Private Sub Form1_Resize(sender As Object, e As EventArgs) Handles Me.Resize
        TextBox1.Width = Me.Width
        TextBox1.Height = Me.Height

    End Sub
End Class
  1. Run the project and resize the form. The size of the textbox will change accordingly.


Form Methods:

Some important methods of form are follows:

Center to Screen: The center to screen method is used to bring the form in the center of the screen. The syntax of this method is as follows: Form Name. Center to Screen ()

Close: The close method is used to remove a form from the display as well as from memory. The syntax of this method is as follows: Form Name. Close ()

Hide: The hide method is used to hide the form. The form remains in the memory but does not appear on the screen. The syntax of this method is as follows: Form Name. Hide ()

Show: The show method is used to load the form in memory and display it on the screen. The hidden form can also be displayed again by using this method.

The syntax of this method is as follows: Form Name. Show()



Example2:  perform some basic operations on form in visual basic:

The following example uses the properties and methods of form to perform different actions.

  1. Start Visual studio and create new project.
  2. Place three buttons on the form.
  3. Set text properties of the buttons as show, clear and close.
  4. Set name properties to Btnshow, Btnclear and Btnclose respectively.

vb.net tutorial form properties

  1. Add the following code in click event of BtnShow button.
Me.Text = "Electronic Clinic"
  1. Add the following code in click event of BtnClear button.
Me.Text=""
  1. Add the following code in click event of BtnClose button.
Me.Close()
  1. Run the project. The first button will show “Electronic Clinic” on the title bar of the form. The second will clear it. The third button will close the form.

vb.net tutorial form properties

vb.net tutorial form properties

vb.net tutorial form properties


Complete code :

Public Class Form1

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

    End Sub

    Private Sub BtnShow_Click(sender As Object, e As EventArgs) Handles BtnShow.Click
        Me.Text = "Electronic Clinic"
    End Sub

    Private Sub BtnClear_Click(sender As Object, e As EventArgs) Handles BtnClear.Click
        Me.Text = ""
    End Sub

    Private Sub BtnClose_Click(sender As Object, e As EventArgs) Handles BtnClose.Click
        Me.Close()
    End Sub
End Class

Example3: how to make a simple form resizing project in visual basic:

The following program resizes the form using buttons. The form should appear in the center of the screen when the program starts.

  1. Start visual studio and create new project.
  2. Place four buttons on the form.
  3. Set text property of the first button to height increase and name to BtnHeightIncr.
  4. Set text property of the second button to height decrease and name to BtnHeightDecr.
  5. Set text property of the third button to width increase and name to BtnWidthIncr.
  6. Set text property of the fourth button to width decrease and name to BtnWidthDecr.

vb.net tutorial form properties

  1. Double click height increase button and add following code in its click event.
Me.Height = Me.Height + 50
  1. Double click height decrease button and add following code in its click event.
Me.Height = Me.Height - 50
  1. Double click width increase button and add following code in its click event.
Me.Width = Me.Width + 50
  1. Double click width decrease button and add following code in its click event.
Me.Width = Me.Width - 50
  1. Double click the form and add following code in its load event.
Me.CenterToScreen()
  1. Run the project and click the buttons to change form size.

vb.net tutorial form properties

vb.net tutorial form properties

vb.net tutorial form properties

vb.net tutorial form properties


Complete code:

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        Me.CenterToScreen()
    End Sub


    Private Sub BtnHeightIncr_Click(sender As Object, e As EventArgs) Handles BtnHeightIncr.Click
        Me.Height = Me.Height + 50
    End Sub

    Private Sub BtnHeightDecr_Click(sender As Object, e As EventArgs) Handles BtnHeightDecr.Click
        Me.Height = Me.Height - 50
    End Sub

    Private Sub BtnWidthIncr_Click(sender As Object, e As EventArgs) Handles BtnWidthIncr.Click
        Me.Width = Me.Width + 50
    End Sub

    Private Sub BtnWidthDecr_Click(sender As Object, e As EventArgs) Handles BtnWidthDecr.Click
        Me.Width = Me.Width - 50
    End Sub
End Class

 

 

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button