DotNet Tutorials

Server Intellect

 Programming Controls in a Windows Form in VB

This tutorial will show how we can use the many .NET Controls in a Windows Form and how we can use them to gather information from the user and provide more interactivity. VB version.

In this tutorial we are going to look at ways we can implement .NET Controls into a Windows Form and how we can code them to make them functional to our needs. We are going to see how to implement and utilize textboxes, buttons, radio buttons, a list box, and a combo box.

The resulting Form will look something like this:

First, we will start off by adding a button to initiate a Message Box that poses a question to the user. We simply drag a button onto the form, and create the event handler for its click:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim yourAnswer As DialogResult = MessageBox.Show("Do you understand how this works?", "Caption", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If yourAnswer = DialogResult.Yes Then
MessageBox.Show("You clicked Yes, well done.")
Else
MessageBox.Show("You clicked No!")
End If
End Sub

Try Server Intellect for Windows Server Hosting. Quality and Quantity!

In this block of code, we are making a Message Box appear as soon as the user clicks the button. The Message Box will have Yes and No buttons and ask if the user understands how it works. Then we capture the user's answer, and display another Message Box dependant upon that answer.

Next, we will create a TextBox and a button. When the button is pressed, a Message Box will be displayed, with the data the user typed in. If no data was input, the user will be instructed to do so.

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If TextBox1.Text <> "" Then
MessageBox.Show("Your name is: " & TextBox1.Text & ".")
Else
MessageBox.Show("Please type your name.")
End If
End Sub

Next, we will add the Combo Box and add code to the SelectedIndexChanged event handler, and again, display a Message Box to the user, announcing the option they choose:

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
MessageBox.Show("You are: " & ComboBox1.SelectedItem.ToString())
End Sub

We can also add a List Box Control, a Text Box and a Button to let the user add their own items into the List Box. We do this simply with the following code:

We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If TextBox2.Text <> "" Then
ListBox1.Items.Add(TextBox2.Text)
Else
MessageBox.Show("Please enter text to add.")
End If
End Sub

We can also add another buttons allowing the user to clear the contents of the List Box:

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
ListBox1.Items.Clear()
End Sub

Finally, we can add a bunch of Radio Buttons to allow the user to set the background color of the Form, from a pre-determined set of colors:

Private Sub RadioButtonBlue_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButtonBlue.CheckedChanged
Me.BackColor = Color.Blue
End Sub Private Sub RadioButtonWhite_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButtonWhite.CheckedChanged
Me.BackColor = Color.White
End Sub Private Sub RadioButtonGreen_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButtonGreen.CheckedChanged
Me.BackColor = Color.Green
End Sub Private Sub RadioButtonGrey_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButtonGrey.CheckedChanged
Me.BackColor = Color.WhiteSmoke
End Sub

The entire code-behind will look something like this:

We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Text = "dotNetTutorials.com"
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim yourAnswer As DialogResult = MessageBox.Show("Do you understand how this works?", "Caption", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If yourAnswer = DialogResult.Yes Then
MessageBox.Show("You clicked Yes, well done.")
Else
MessageBox.Show("You clicked No!")
End If
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If TextBox1.Text <> "" Then
MessageBox.Show("Your name is: " & TextBox1.Text & ".")
Else
MessageBox.Show("Please type your name.")
End If
End Sub

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
MessageBox.Show("You are: " & ComboBox1.SelectedItem.ToString())
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If TextBox2.Text <> "" Then
ListBox1.Items.Add(TextBox2.Text)
Else
MessageBox.Show("Please enter text to add.")
End If
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
ListBox1.Items.Clear()
End Sub

Private Sub RadioButtonBlue_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButtonBlue.CheckedChanged
Me.BackColor = Color.Blue
End Sub

Private Sub RadioButtonWhite_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButtonWhite.CheckedChanged
Me.BackColor = Color.White
End Sub

Private Sub RadioButtonGreen_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButtonGreen.CheckedChanged
Me.BackColor = Color.Green
End Sub

Private Sub RadioButtonGrey_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButtonGrey.CheckedChanged
Me.BackColor = Color.WhiteSmoke
End Sub
End Class

Looking for the C#.NET 2005 Version? Click Here!

Looking for more .NET Tutorials? Click Here!

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!
 
123 ASP

411 ASP

Dot Net Freaks

Server Intellect