Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleUse a tristate CheckBox in Visual Basic .NET
DescriptionThis example shows how to use a tristate CheckBox in Visual Basic .NET.
Keywordscontrols, ComboBox, tristate, tri-state, indeterminate, Visual Basic .NET, VB.NET
CategoriesControls
 

Normally a CheckBox is either checked or unchecked. You can catch the CheckedChanged event handler to learn when the control's value has changed and you can use its Checked property to get its current value.

A CheckBox can also display a third indeterminate state. For example, a program might use the three states to indicate that a group of options is selected (checked), not selected (unchecked), or some options are selected (indeterminate).

To do this, first set the ComboBox's ThreeState property to True. Next catch the control's CheckStateChanged event instead of CheckedChanged. In the event, use the control's CheckState property to see whether it is checked, unchecked, or indeterminate.

The following code shows how this example sets the lunch ComboBox's state to indeterminate when the program starts.

 
' Make lunch indeterminate.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    chkLunch.CheckState = CheckState.Indeterminate
    lstState.Items.Clear()
End Sub
 
The following code shows how the program displays CheckBox states when they change. The program uses the same event handler for all three CheckBoxes.
 
' Display the CheckBox's current state.
Private Sub chkBreakfast_CheckStateChanged(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    chkBreakfast.CheckStateChanged, _
    chkLunch.CheckStateChanged, chkDinner.CheckStateChanged
    Dim chk As CheckBox = DirectCast(sender, CheckBox)
    lstState.Items.Add(chk.Text & ": " & _
        chk.CheckState.ToString())
End Sub
 
 
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated