articles

Home / DeveloperSection / Articles / Windows Form Controls

Windows Form Controls

Abdir Jama 8439 11-Jan-2014

Label Control

Microsoft Visual Studio .NET controls are the graphical tools you use to build the user interface of a VB.Net program. Labels are one of the most frequently used Visual Basic control.

 A Label control lets you place descriptive text , where the text does not need to be changed by the user. The Label class is defined in the System.Windows.Forms namespace.

Windows Form Controls

 Add a Label control to the form. Click Label in the Toolbox and drag it over the forms Designer and drop it in the desired location.


 If you want to change the display text of the Label, you have to set a new text to the Text property of Label.

  Label1.Text = "This is my first Label"

 You can load Image in Label control , if you want to load an Image in the Lable control you can code like this

  Label1.Image = Image.FromFile("C:\testimage.jpg")


 The following source code shows how to set some properties of the Label through coding.

Public Class Form1


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

        Label1.Text = "This is my first Label"

        Label1.BorderStyle = BorderStyle.FixedSingle

        Label1.TextAlign = ContentAlignment.MiddleCenter

    End Sub

End Class

Button Control

Windows Forms controls are reusable components that encapsulate user interface functionality and are used in client side Windows applications. A Button is a control, which is an interactive component that enables users to communicate with an application which we click and release to perform some actions. Windows Form Controls

 

 The Button control represents a standard button that reacts to a Click event. A Button can be clicked by using the mouse, ENTER key, or SPACEBAR if the button has focus.

 When you want to change display text of the Button , you can change the Text property of the button.


  Button1.Text = "My first Button"


 Similarly if you want to load an Image to a Button control , you can code like this

  Button1.Image = Image.FromFile("C:\testimage.jpg")


 The following vb.net source code shows how to change the button Text property while Form loading event and to display a message box when pressing a Button Control.


Public Class Form1


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

        Button1.Text = "Click Here"

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        MsgBox("http://vb.net-informations.com")

    End Sub

End Class

 

TextBox Control

VB.Net provides several mechanisms for gathering input in a program. A TextBox control is used to display, or accept as input, a single line of text.

VB.Net programmers make extensive use of the TextBox control to let the user view or enter large amount of text. A text box object is used to display text on a form or to get user input while a VB.Net program is running. In a text box, a user can type data or paste it into the control from the clipboard.

For displaying a text in a TextBox control , you can code like this

  TextBox1.Text = "http://Starcomputercollege.com"

 You can also collect the input value from a TextBox control to a variable like this way 

  Dim var As String

  var = TextBox1.Text 

 when a program wants to prevent a user from changing the text that appears in a text box, the program can set the controls Readonly property is to True.

  TextBox1.ReadOnly = True

 By default TextBox accept single line of characters , If you need to enter more than one line in a TextBox control, you should change the Multiline property is to True.

  TextBox1.Multiline = True

 Sometimes you want a textbox to receive password from the user. In order to keep the password confidential, you can set the PasswordChar property of a textbox to a specific character.

  TextBox1.PasswordChar = "*"

 The above code set the PasswordChar to * , so when the user enter password then it display only * instead of other characters.

 From the following VB.Net source code you can see some important property settings to a TextBox control.

Public Class Form1 

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


                        TextBox1.Width = 200

                        TextBox1.Height = 50

                        TextBox1.Multiline = True

            End Sub



            Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

                        Dim var As String

                        var = TextBox1.Text

                        MsgBox(var)

            End Sub



End Class

 

ComboBox Control

VB.Net controls are located in the Toolbox of the development environment, and you use them to create objects on a form with a simple series of mouse clicks and dragging motions. The ComboBox control , which lets the user choose one of several choices. 

The user can type a value in the text field or click the button to display a drop down list. In addition to display and selection functionality, the ComboBox also provides features that enable you to efficiently add items to the ComboBox.

  ComboBox1.Items.Add("Sunday")

 You can set which item should shown while it displaying in the form for first time.

  ComboBox1.SelectedItem = ComboBox1.Items(3)

 When you run the above code it will show the forth item in the combobox. The item index is starting from 0.

 If you want to retrieve the displayed item to a string variable , you can code like this

  Dim var As String

  var = ComboBox1.Text

 You can remove items from a combobox in two ways. You can remove item at a the specified index or giving a specified item by name. 

ComboBox1.Items.RemoveAt(1)

 The above code will remove the second item from the combobox.

  ComboBox1.Items.Remove("Friday")

 The above code will remove the item "Friday" from the combobox.

 The DropDownStyle property specifies whether the list is always displayed or whether the list is displayed in a drop down. The DropDownStyle property also specifies whether the text portion can be edited.

  ComboBox1.DropDownStyle = ComboBoxStyle.DropDown

 The following VB.Net source code add seven days in a week to a combo box while load event of a Windows Form and display the fourth item in the combobox.

Public Class Form1

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


                        ComboBox1.Items.Add("Sunday")

                        ComboBox1.Items.Add("Monday")

                        ComboBox1.Items.Add("Tuesday")

                        ComboBox1.Items.Add("wednesday")

                        ComboBox1.Items.Add("Thursday")

                        ComboBox1.Items.Add("Friday")

                        ComboBox1.Items.Add("Saturday")

                        ComboBox1.SelectedItem = ComboBox1.Items(3)

            End Sub

            Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

                        Dim var As String

                        var = ComboBox1.Text

                        MsgBox(var)

            End Sub

End Class

   

Windows Form Controls

 

ListBox Control


VB.Net provides several mechanisms for gathering input in a program. A Windows Forms ListBox control displays a list of choices which the user can select from.


Windows Form Controls

You can use the Add or Insert method to add items to a list box. The Add method adds new items at the end of an unsorted list box. The Insert method allows you to specify where to insert the item you are adding. 

listBox1.Items.Add("Sunday")


 The SelectionMode property determines how many items in the list can be selected at a time. A ListBox control can provide single or multiple selections using the SelectionMode property .


 If you want to retrieve a single selected item to a variable , you can code like this

  Dim var As String

  var = ListBox1.SelectedItem

 If you change the selection mode property to multiple select , then you will retrieve a collection of items from ListBox1.SelectedItems property.

  ListBox1.SelectionMode = SelectionMode.MultiSimple

 The following VB.Net program initially fill seven days in a week while in the form load event and set the selection mode property to MultiSimple. At the Button click event it will display the selected items.

Public Class Form1


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


                        ListBox1.Items.Add("Sunday")

                        ListBox1.Items.Add("Monday")

                        ListBox1.Items.Add("Tuesday")

                        ListBox1.Items.Add("Wednesday")

                        ListBox1.Items.Add("Thursday")

                        ListBox1.Items.Add("Friday")

                        ListBox1.Items.Add("Saturday")

 

                        ListBox1.SelectionMode = SelectionMode.MultiSimple

            End Sub

 

RadioButton Control


A radio button or option button is a type of graphical user interface element that allows the user to choose only one of a predefined set of options. When a user clicks on a radio button, it becomes checked, and all other radio buttons with same group become unchecked

Windows Form Controls

The radio button and the check box are used for different functions. Use a radio button when you want the user to choose only one option. When you want the user to choose all appropriate options, use a check box. Like check boxes, radio buttons support a Checked property that indicates whether the radio button is selected.


Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        If RadioButton1.Checked = True Then

            MsgBox("You are selected Red !! ")

            Exit Sub

        ElseIf RadioButton2.Checked = True Then

            MsgBox("You are selected Blue !! ")

            Exit Sub

        Else

            MsgBox("You are selected Green !! ")

            Exit Sub

        End If

    End Sub

End Class


CheckBox Control

CheckBoxes allow the user to make multiple selections from a number of options. You can click a check box to select it and click it again to deselect it.

Windows Form Controls

CheckBoxes comes with a caption, which you can set in the Text property.

 CheckBox1.Text = "Net-informations.com"

 You can use the CheckBox control ThreeState property to direct the control to return the Checked, Unchecked, and Indeterminate values. You need to set the check boxs ThreeState property to True to indicate that you want it to support three states.

  CheckBox1.ThreeState = True

 To apply the same property settings to multiple CheckBox controls, use the Style property. The following VB.Net program shows how to find a checkbox is selected or not.

Public Class Form1

                Private Sub Button1_Click(ByValSender As System.Object, ByVal  e As System.EvemtArgs


Handles Button1.Click

                                Dim msg As String = ""

                                If CheckBox1.Checked = True Then

                                                msg = "Net-Star.com"

                                End If

                                If CheckBox2.Checked = True Then

                                                msg = msg & "Vb- Star.com"

                                End If

                                If CheckBox3.Checked = True Then

                                                msg = msg & " csharp.net-Star.com"

                                End If

                                If msg.Length > 0 Then

                                                MsgBox(msg & " selected ")

                                Else

                                                MsgBox("No checkbox selected")

                                End If

                                CheckBox1.ThreeState = True

                End Sub

End Class


PictureBox Control

The Windows Forms PictureBox control is used to display images in bitmap, GIF,icon, or JPEG formats.

Windows Form Controls

You can set the Image property to the Image you want to display, either at design time or at run time. You can programmatically change the image displayed in a picture box, which is particularly useful when you use a single form to display different pieces of information.

PictureBox1.Image = Image.FromFile("C:\testImage.jpg")

 The SizeMode property, which is set to values in the PictureBoxSizeMode enumeration, controls the clipping and positioning of the image in the display area.

  PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage

 There are five different PictureBoxSizeMode is available to PictureBox control.

  AutoSize                   - Sizes the picture box to the image.

  CenterImage            - Centers the image in the picture box.

  Normal         - Places the upper-left corner of the image at upper

                                                left in the picture box

  StretchImage           - Allows you to stretch the image in code

The following VB.Net program shows how to load a picture from a file and display it in streach mode.

Public Class Form1


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

                                PictureBox1.Image = Image.FromFile("d:\testImage.jpg")

                                PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage

                End Sub

End Class


ProgressBar Control

The ProgressBar control visually indicates the progress of a lengthy operation such as calculating a complex result, downloading a large file from the Web etc.

Windows Form Controls

The Maximum and Minimum properties define the range of values to represent the progress of a task.

  Minimum : Sets the lower value for the range of valid values for progress.

  Maximum : Sets the upper value for the range of valid values for progress.

  Value   : This property obtains or sets the current level of progress.

 By default, Minimum and Maximum are set to 0 and 100. As the task proceeds, the ProgressBar fills in from the left to the right. To delay the program briefly so that you can view changes in the progress bar clearly.

 The following VB.Net program shows a simple operation in a progressbar .


Public Class Form1 

                        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

                                    Dim i As Integer

                                    ProgressBar1.Minimum = 0

                                    ProgressBar1.Maximum = 200

                                    For i = 0 To 200

                                                ProgressBar1.Value = i

                                    Next

                        End Sub

End Class


<![if !vml]><![endif]>
Me.BackColor = Color.Blue
        Me.ForeColor = Color.Red
        Label1.Text = "welcome to star computer college.com"
ProgressBar1.Increment(1)
        If ProgressBar1.Value = ProgressBar1.Maximum Then
            Me.Hide()
            Timer1.Stop()
        End If

  

DateTimePicker Control

The DateTimePicker control allows you to display and collect date and time from the user with a specified format.

Windows Form Controls

The DateTimePicker control prompts the user for a date or time using a graphical calendar with scroll arrows. The most important property of the DateTimePicker is the Value property, which holds the selected date and time.

DateTimePicker1.Value = "12/31/2010"

 The Value property is set to the current date by default. You can use the Text property or the appropriate

 member of Value to get the date and time value.

  Dim idate As String

  idate = DateTimePicker1.Value

 The control can display one of several styles, depending on its property values. The values can be displayed in four

 formats, which are set by the Format property: Long, Short, Time, or Custom.

  DateTimePicker1.Format = DateTimePickerFormat.Short

 The following VB.Net program shows how to set and get the value of a DateTimePicker1 control.

Public Class Form1 

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

                                    DateTimePicker1.Format = DateTimePickerFormat.Short

                                    DateTimePicker1.Value = "12/31/2010"

                        End Sub

                        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

                                    Dim idate As String

                                    idate = DateTimePicker1.Value

                                    MsgBox("Selected date is : " & idate)

                        End Sub

End Class


 


Updated 01-Feb-2020
i am computer instructor and i like to join your website because you are a very respectable teachers

Leave Comment

Comments

Liked By