devxlogo

Force Tri-State Checkbox Cycling

Force Tri-State Checkbox Cycling

The CheckBox control in VB supports three positions: Checked, Unchecked, and Grayed. Unfortunately, the default behavior for the control is to cycle between Checked and Unchecked. To set it to Grayed, you must do it programatically. This code shows you how to cycle between the three positions (the order is Checked->Unchecked->Grayed->Checked …):

 Private Sub Check1_Click()Static iState As CheckBoxConstantsStatic bUserClick As Boolean' Trap if the user clicked on the control' or if the event was fired because you' changed the value belowbUserClick = (iState <> Check1.Value)' Prevents you from entering an infinite' loop and getting an Out of Stack Space errorIf bUserClick ThenSelect Case iStateCase vbCheckediState = vbUncheckedCase vbUncheckediState = vbGrayedCase vbGrayediState = vbCheckedEnd Select' This will raise another click event but' your boolean check prevents you from loopingCheck1.Value = iStateEnd IfEnd Sub
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist