Problem solve Get help with specific problems with your technologies, process and projects.

What is the best/simplest decision structure for checkboxes?

If a form has several checkboxes in VB.NET, and these are assigned different monetary values, what is the simplest decision structure to check for checkedstate, calculate the various values and calculation to Textbox.Text?
Much of this depends on exactly how many checkboxes you are dealing with. Let's assume it is maybe 50, so just inline code will be voluminous.

I believe I would iterate the controls collection of the control on which the check boxes reside. If the checkboxes are in multiple controls collections you need to recursively iterate the controls collections.

In the following example I have put the number value in the Text property of each of the checkboxes. I suspect you will probably use the Tag property.

''' private declarations
Dim decTotalValue As Decimal = 0

''' iterate the controls collection looking for checkboxes
Dim aControl As Windows.Forms.Control
For Each aControl In Me.Controls

 '' test for checkbox
 If aControl.GetType Is GetType(Windows.Forms.CheckBox) Then

  ' retrieve reference to control and add text value to total
  Dim aCheckBox As Windows.Forms.CheckBox = DirectCast(aControl, Windows.Forms.CheckBox)
  If aCheckBox.Checked = True Then decTotalValue += CDec(aCheckBox.Text)
 End If


Me.TextBox1.Text = Format(decTotalValue, "c"

Dig Deeper on Win Development Resources

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.