Click here to Skip to main content
15,894,337 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, all.

Novice level VB programmer here.

I've got a form that contains a combo box from which the user can select a PC host name. When the host name is selected, the program loops through and performs three functions:

1. enable all check boxes
2. select/clear the appropriate boxes
3. disable selected check boxes (to indicate they are the minimum required)

The problem is that, to me, there seems like there would be more streamlined way to go about the tasks. I dabble mostly with PHP and that sort of thing, so I'm in the mindset of using the DOM where form items are indexed and can be referenced by the form.elements[i] syntax.

Looking for illuminating (and relevant) concepts here.

Thanks!

VB
' Create array containing hostnames and values for the 21 associated check boxes
Dim arrAccounts(,) As String = { _
    {"HOST-01", True, False, True, True, False, False, False, False, False, True, False, False, True, False, False, False, False, False, False, False}, _
    {"HOST-02", True, False, True, True, False, False, False, False, False, True, False, False, True, False, False, False, True, True, False, False}, _
    {"HOST-03", True, False, True, True, False, False, False, False, False, True, False, False, True, False, False, False, False, False, False, False}, _
    {"HOST-04", True, False, True, True, False, False, False, False, False, True, False, False, True, False, False, False, True, True, False, False}, _
    {"HOST-05", True, False, True, True, False, False, False, False, False, True, False, False, True, False, False, False, True, True, False, False} _
}

' Loop through account to set the values and disable afterward
For HostName = 0 To (arrAccounts.Length / 21) - 1
    ' Determine the relevant hostname by comparing to selection in combo box
    If arrAccounts(HostName, 0) = ComboBox1.Text Then

        ' Enable check boxes
        ckFirefox.Enabled = True
        ckChrome.Enabled = True
        ckClocX.Enabled = True
        ckJRE.Enabled = True
        ckKronos.Enabled = True
        ckWinlink.Enabled = True
        ckTokairo.Enabled = True
        ckNovarad.Enabled = True
        ckCART.Enabled = True
        ckAcrobat.Enabled = True
        ckEduTracker.Enabled = True
        ckStaffing.Enabled = True
        ck610.Enabled = True
        ck1515.Enabled = True
        ck161.Enabled = True
        ck1060.Enabled = True
        ckBasisAlarm.Enabled = True
        ckBasisSys.Enabled = True
        ckBasisID.Enabled = True
        ckOpOutlook.Enabled = True

        ' Check the appropriate check boxes
        ckFirefox.Checked = arrAccounts(HostName, 1)
        ckChrome.Checked = arrAccounts(HostName, 2)
        ckClocX.Checked = arrAccounts(HostName, 3)
        ckJRE.Checked = arrAccounts(HostName, 4)
        ckKronos.Checked = arrAccounts(HostName, 5)
        ckWinlink.Checked = arrAccounts(HostName, 6)
        ckTokairo.Checked = arrAccounts(HostName, 7)
        ckNovarad.Checked = arrAccounts(HostName, 8)
        ckCART.Checked = arrAccounts(HostName, 9)
        ckAcrobat.Checked = arrAccounts(HostName, 10)
        ckEduTracker.Checked = arrAccounts(HostName, 11)
        ckStaffing.Checked = arrAccounts(HostName, 12)
        ck610.Checked = arrAccounts(HostName, 13)
        ck1515.Checked = arrAccounts(HostName, 14)
        ck161.Checked = arrAccounts(HostName, 15)
        ck1060.Checked = arrAccounts(HostName, 16)
        ckBasisAlarm.Checked = arrAccounts(HostName, 17)
        ckBasisSys.Checked = arrAccounts(HostName, 18)
        ckBasisID.Checked = arrAccounts(HostName, 19)
        ckOpOutlook.Checked = arrAccounts(HostName, 20)

        ' Disable CHECKED check boxes
        ckFirefox.Enabled = Not Boolean.Parse(arrAccounts(HostName, 1))
        ckChrome.Enabled = Not Boolean.Parse(arrAccounts(HostName, 2))
        ckClocX.Enabled = Not Boolean.Parse(arrAccounts(HostName, 3))
        ckJRE.Enabled = Not Boolean.Parse(arrAccounts(HostName, 4))
        ckKronos.Enabled = Not Boolean.Parse(arrAccounts(HostName, 5))
        ckWinlink.Enabled = Not Boolean.Parse(arrAccounts(HostName, 6))
        ckTokairo.Enabled = Not Boolean.Parse(arrAccounts(HostName, 7))
        ckNovarad.Enabled = Not Boolean.Parse(arrAccounts(HostName, 8))
        ckCART.Enabled = Not Boolean.Parse(arrAccounts(HostName, 9))
        ckAcrobat.Enabled = Not Boolean.Parse(arrAccounts(HostName, 10))
        ckEduTracker.Enabled = Not Boolean.Parse(arrAccounts(HostName, 11))
        ckStaffing.Enabled = Not Boolean.Parse(arrAccounts(HostName, 12))
        ck610.Enabled = Not Boolean.Parse(arrAccounts(HostName, 13))
        ck1515.Enabled = Not Boolean.Parse(arrAccounts(HostName, 14))
        ck161.Enabled = Not Boolean.Parse(arrAccounts(HostName, 15))
        ck1060.Enabled = Not Boolean.Parse(arrAccounts(HostName, 16))
        ckBasisAlarm.Enabled = Not Boolean.Parse(arrAccounts(HostName, 17))
        ckBasisSys.Enabled = Not Boolean.Parse(arrAccounts(HostName, 18))
        ckBasisID.Enabled = Not Boolean.Parse(arrAccounts(HostName, 19))
        ckOpOutlook.Enabled = Not Boolean.Parse(arrAccounts(HostName, 20))
    End If
Next
Posted
Updated 16-Jun-10 8:40am
v2

I usually do something like this (please note this is coding from memory without the benefit of the debugger)

1. Set the tag property for your checkboxes to the index value of the array.

chkChrome.Tag = 1

2. Loop through all controls and set their property based on the values in the host array. Note that I assumed you already found the correct host array that matches the combo box setting.

For Each control As Control In Form1.Controls

   If control.Tag Is Not Nothing Then 'We have a checkbox here
   
        Dim checked = CBool(HostArray(control.Tag))
        control.Checked = checked
        control.Enabled = Not checked
  
   End If

Next
 
Share this answer
 
Comments
Robert Nicholas 22-Jun-10 15:14pm    
This seems like a very handy way to go about applying the same property to every 'TAGGED' control.

In my case, each host name in the combo box has a different arrangement of checked and cleared controls, so will a similar approach also work in for such multiple arrangements? The array of controls would still require a data source--something that defined explicitly the status of the controls. In my first submission the source was the array (arrAccounts), for example.
Gordon Kushner 23-Jun-10 8:08am    
OK, how about when the combo box value changes, you set a module level variable that corresponds to the index of the 2-d array you set up?

(On combobox_change)
hostIndex = ComboBox1.SelectedIndex

Then you could change this line:
CBool(HostArray(control.Tag))
to this:
CBool(arrAccounts(hostIndex, control.Tag))
Robert Nicholas 24-Jun-10 15:52pm    
This seems to be getting me in the right direction, and I've been fiddling with different ways of doing things.

The Control Class, however, does not have a "Checked" property like the CheckBox Class does. I've been looking for a way to access the CheckBox.Checked property from the Control Class, but haven't found anything so far. Were these simple data types, I might suggest some sort of cast, but I'm not certain anything like that can occur using form elements.

My gut feeling is that the solution is something relatively simple (for me to be so close, but without fruition). My how things have changed since I was writing Assembly code for the 6502 and 6802/6810 8-bit processors back in the early eighties.
Gordon Kushner 24-Jun-10 20:09pm    
Don't give up man! You're nearly there. You're right that the control can be cast as a checkbox (since it is one).

Look into CType(control, CheckBox).Checked. I think that'll do it. I'm a little rusty in VB and the parameters may be switched.

Wow Assembly? Yeah, it's changed a bit, but under the hood, it's all basically the same thing. Except for the cool colorful IDE's
Robert Nicholas 25-Jun-10 8:39am    
Yeah, whenever I point out that my status as a VB programmer is NOVICE, I always seem to get a crate full of condescension delivered in response, as if I'm some 13 year old lie-about too busy surfing porn and drinking Red Bull to investigate some of these programming techniques. Your response was a bit more refreshing and I thank you for that.

I did get that code to work, by the way. The CTYPE() did the trick and the syntax you presented was correct. I've attached the finished code and I think that it is very elegant the way it works. Mark (Answer 1) had pointed out the use of Classes or Structures to build the code infrastructure, but my nascent understanding permits only the idea that this would bloat the code terribly. Perhaps the increase in the bulk of the source code will be countered by a tighter compiled application, I'm not sure. Thoughts on that? Mark had chosen no longer to reply.


Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

'Debug.Print("****** S T A R T ******")

' Create array containing hostnames and values for the 21 associated check boxes
Dim arrAccounts(,) As String = { _
{"HOST-01", True, False, True, True, False, False, False, False, False, True, False, False, True, False, False, False, False, False, False, False}, _
{"HOST-02", True, False, True, True, False, False, False, False, False, True, False, False, True, False, False, False, True, True, False, False}, _
{"HOST-03", True, False, True, True, False, False, False, False, False, True, False, False, True, False, False, False, False, False, False, False}, _
{"HOST-04", True, False, True, True, False, False, False, False, False, True, False, False, True, False, False, False, True, True, False, False}, _
{"HOST-05", True, False, True, True, False, False, False, False, False, True, False, False, True, False, False, False, True, True, False, False} _
}

' Set Tag property of relevant CheckBoxes
ckFirefox.Tag = 1
ckChrome.Tag = 2
ckNovarad.Tag = 3
ck610.Tag = 4
ck1515.Tag = 5
ck161.Tag = 6
ck1060.Tag = 7
ckClocX.Tag = 8
ckJRE.Tag = 9
ckAcrobat.Tag = 10
ckKronos.Tag = 11
ckWinlink.Tag = 12
ckTokairo.Tag = 13
ckCART.Tag = 14
ckBasisAlarm.Tag = 15
ckBasisSys.Tag = 16
ckBasisID.Tag = 17
ckEduTracker.Tag = 18
ckStaffing.Tag = 19
ckOpOutlook.Tag = 20

' Loop through account to set the values and disable afterward
For Each objControl As Control In Me.Controls

' Determine whether control has been tagged and if it has
If objControl.Tag Then
Dim hostIndex = ComboBox1.SelectedIndex
Dim boolChecked = CBool(arrAccounts(hostIndex, objControl.Tag))
Dim objCheckBox = CType(objControl, CheckBox)

'Debug.Print(ctrl.Name & " " & ctrl.Tag & " " & hostIndex)

objControl.Enabled = True
objCheckBox.Checked = boolChecked
objControl.Enabled = Not boolChecked
End If

Next

End Sub
Instead of using an array create the settings as a struct or class, then create a collection of them and bind the controls to the appropriate properties triggered by the combobox selection
 
Share this answer
 
Comments
Robert Nicholas 16-Jun-10 15:58pm    
Since the sets of check boxes that each host name causes to be checked overlap, I don't think I know how that would work. Can multiple bindings occur on a single control or property? If so, do you have a code snippet that might demonstrate it?

I might also add here that I'm more familiar with classes than with structs. and really not familiar with structs at all.
[no name] 16-Jun-10 16:10pm    
Then I would say you need to become familiar with structs and binding to answer your own questions and learn how ot use the proper tool for the task
Robert Nicholas 17-Jun-10 9:15am    
That is self-evident--and my purpose for posing the question. I was asking for your aid in that. There is not a book or class that does not, to some extent, teach by example.
Robert Nicholas 25-Jun-10 16:01pm    
Reason for my vote of 2
Whereas the initial answer may have been appropriate to the task, the response to my admitted limitations was not.

I genuinely would have liked to have inquired more deeply into this technique.
Okay, I've been looking through Classes and Structures in order to see how they might be implemented here. It's tough going because most sources of information seem hell-bent on demonstrating inheritance through the canonical (and a little threadbare) Class Animal and Class Vehicle examples, utterly neglecting real world uses.

By using a STRUCT (for example), it seems as if the code would be much longer, so now I'm wondering what the advantage is of using the STRUCT in the first place. My feeling is that it would somehow capitalize on the OO nature of the language and framework themselves, but I'm not certain how that might be.

Are these accurate observations (much longer; optimized)?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900