VB.NET Technology

VB.Net Projects

VB.Net Project 1

Radio Button
Previous Home Next
adplus-dvertising

Radio buttons themselves as a label with a dot to the left of it, which can be either selected or not. You should use the radio buttons when you want to give the user a choice between several mutually exclusive options. for Example, if you want to ask for the gender of the user.

To group radiobuttons together so that they create one logical unit you must use a GroupBox control. By first placing a group box on a form, and then placing the RadioButton controls you need within the borders of the group box.

The RadioButton controls will know to change their state to reflect that only one within the group box can be selected. If you do not place them within a group box, only one RadioButton on the form can be selected at any given time.

CheckBox Controls

A CheckBox traditionally displays itself as a label with a small box with a checkmark to the left of it. You should use the check box when you want to allow the user to choose one or more options. An example could be a questionnaire asking which operating systems the user has tried (for example, Windows 95, Windows 98, Linux, Max OS X, and so on.)

RadioButton Properties

NameAvailabilityDescription
AppearanceRead/WriteA RadioButton can be displayed either as a label with a circular check to the left, middle or right of it, or as a standard button. When it is displayed as a button, the control will appear pressed when selected and 3D otherwise.
AutoCheckRead/WriteWhen this property is true, a check mark is displayed when the user clicks the radio button. When it is false, the check mark is not displayed by default.
CheckAlignRead/WriteBy using this property, you can change the alignment of the radio button. It can be left, middle, and right.
CheckedRead/WriteIndicates the status of the control. It is true if the control has a check mark, and false otherwise.

RadioButton Events

NameDescription
CheckChangedThis event is sent when the check of the RadioButton changes. If there is more than one RadioButton control on the form or within a group box, this event will be sent twice, first to the control, which was checked and now becomes unchecked, then to the control which becomes checked.
ClickThis event is sent every time the RadioButton is clicked. This is not the same as the change event, because clicking a RadioButton twice or more times in succession only changes the checked property once and only if it wasn't checked already.

CheckBox Properties

NameAvailabilityDescription
AnchorRead/Write

Unlike the RadioButton, a CheckBox can have three states: Checked, Indeterminate, and Unchecked. When the state of the check box is Indeterminate, the control check next to the label is usually grayed, indicating that the current value of the check is not valid or has no meaning under the current circumstances.

An example of this state can be seen if you select several files in the Windows Explorer and look at their properties. If some files are ReadOnly and others are not, the ReadOnly checkbox will be checked, but grayed indeterminate.

ThreeStateRead/WriteWhen this property is false, the user will not be able to change the CheckBox' state to Indeterminate. You can, however, still change the state of the check box to Indeterminate from code.

CheckBox Events

NameDescription
CheckedChanged Occurs whenever the Checked property of the check box changes. Note that in a CheckBox where the ThreeState property is true, it is possible to click the check box without changing the Checked property. This happens when the check box changes from checked to indeterminate state.
CheckedStateChanged Occurs whenever the CheckedState property changes. As Checked and Unchecked are both possible values of the CheckedState property, this event will be sent whenever the Checked property changes. In addition to that, it will also be sent when the state changes from Checked to Indeterminate.

Example

In following image i have created a window form for saving information of student like personal info and subjects of his/her. For this you have to drag and drop 4 textboxes, 2 radiobuttons, 5 labels and 3 buttons. Change all controls properties like in picture.

Form No. 1

Form No. 2

Form No. 3

Form No. 4

In figure 2 if user direct click on ok button then he/she will get massage for alert to fill all information. In Figure 3 if user fill personal information and then click on Ok button then he/she will get alert message for choosing gender. And in figure 4 if user not choose subject and click on ok button then he will get another alert message for choosing subject. To making all these validation you have to write following code on Ok button Click.

Code For Ok Button Click

Private Sub Button2_Click(ByVal sender As System.Object, 
	ByVal e As System.EventArgs) Handles Button2.Click
        If textBox1.Text = "" OrElse textBox2.Text = "" OrElse textBox4.Text = "" Then
            MessageBox.Show("Please fill all personal information",
	 "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning)
        ElseIf radioButton1.Checked = False AndAlso radioButton2.Checked = False Then
            MessageBox.Show("Please select Gender", "Warning", 
		MessageBoxButtons.OKCancel, MessageBoxIcon.Warning)
        ElseIf checkedListBox1.Text = "" Then
            MessageBox.Show("Please select Subjects", "Warning", 
		MessageBoxButtons.OKCancel, MessageBoxIcon.Warning)
        Else
            If radioButton1.Checked Then
                ' Concatenate the text values of the four TextBoxes
                output = "Name: " & Convert.ToString(Me.textBox1.Text) & vbCr & vbLf
                output += "Address: " & Convert.ToString(Me.textBox2.Text) & vbCr & vbLf
                output += "Course: " & Convert.ToString(Me.textBox4.Text) & vbCr & vbLf
                output += "Sex: " & Convert.ToString(Me.radioButton1.Text) & vbCr & vbLf
                output += "Subject:"
                For Each [sub] As String In checkedListBox1.CheckedItems
                    output += "  " & [sub]
                Next
            End If
            If radioButton2.Checked Then
                ' Concatenate the text values of the four TextBoxes
                output = "Name: " & Convert.ToString(Me.textBox1.Text) & vbCr & vbLf
                output += "Address: " & Convert.ToString(Me.textBox2.Text) & vbCr & vbLf
                output += "Course: " & Convert.ToString(Me.textBox4.Text) & vbCr & vbLf
                output += "Sex: " & Convert.ToString(Me.radioButton2.Text) & vbCr & vbLf
                output += "Subject:"
                For Each [sub] As String In checkedListBox1.CheckedItems
                    output += "  " & [sub]
                Next
            End If
            Me.textBox3.Text = output
        End If
    End Sub

Now if you to give condition for user that he should must choose at least three subjects then you can add code for this on Checklistbox Leave Event. Like following code.

Code for Leave Event on Checklist Box.

Private Sub CheckedListBox1_Leave(ByVal sender As System.Object, 
ByVal e As System.EventArgs) Handles CheckedListBox1.Leave
        Dim count As Integer = CheckedListBox1.CheckedItems.Count
        If True Then
            If count <= 3 Then
                MessageBox.Show("Your minimum choice is Three subject")
            End If
        End If
    End Sub

Final output will show like in below figure

Previous Home Next