AdvancedHMI Software
General Category => Support Questions => Topic started by: Jesse on November 19, 2013, 04:25:05 PM
-
Still working on figuring out VB code.. is it possible in VB 2010 to make an array of comboboxes and if so what would that look like code wise
-
Do you mean a control array like the old VB6 to where you can access them in code by an array?
-
Yes I think so.
Right now my code is...
Private Sub Visibility()
For Each ControlOnForm As Control In Me.Controls
If TypeOf ControlOnForm Is ComboBox Then
If ControlOnForm.Text = "END OF CYCLE EVENT (G 192)" Then
EndBox = ControlOnForm.TabIndex
Else
EndBox = 120
End If
End If
Next
For Each ControlOnForm As Control In Me.Controls
If ControlOnForm.TabIndex > EndBox + 1 And ControlOnForm.TabIndex < 120 Then
ControlOnForm.Visible = False
If TypeOf ControlOnForm Is ComboBox Then
ControlOnForm.Text = "NULL OPCODE"
ElseIf TypeOf ControlOnForm Is TextBox Then
ControlOnForm.Text = "0"
End If
End If
Next
End Sub
Basically if any Combo says END OF CYCLE EVENT all labels, combos, and textboxes after that one's row should go invisible.
I first tried using their tag numbers but since tags are an object and not an integer it worked sometimes but had some wackey results sometimes such as allowing just a couple combos that should have gone invisible to stay visible.
Now that I'm using tab index it doesn't seem to work at all because for some reason when one does equal END OF CYCLE EVENT it doesnt change ENDBOX's value.
So instead of using tabindex or tag values I was wondering if an array of comboboxes and textboxes would be possible.
-
It is possible, but requires a little bit of code to setup.
First create a field:
Private ComboBoxArray(10) as ComboxBox
Then in the Load event, point the array elements to the comboBoxes
......FormLoad.........
ComboBoxArray(0)=ComboBox1
ComboBoxArray(1)=ComboBox2
ComboBoxArray(2)=ComboBox3
.
.
.
Method 2:
Using the tag property will also work, but you have to use explicit conversion to avoid it being interpreted as a string.
Cint(ControlOnForm.Tag)
Method 3:
You could also get clever with it by setting the names the same but with the number on the end designating the array element you want. Once again in the form load event:
....FormLoad.....
For Each ControlOnForm As Control In Me.Controls
If TypeOf ControlOnForm Is ComboBox Then
ComboBoxArray(ControlOnForm.Name.Substring(8))=ControlOnForm
End IF
Next
-
I think I'm going to try one of the first two. You lost me on that third one ha.
-
Ok so turns out the tabindex wasn't the problem at all.
I used the Cint method and it still didn't work, but once I took out the line Else EndBox = 120 it worked. But only half way, now if I select END OF CYCLE the rest go invisible but when I change it to something else they stay invisible because EndBox's value hasn't changed
-
Modifying your code to use Method3. This assumes all of your ComboBoxes are named:
ComboBox1
ComboBox2
ComboBox3
.
.
.
In the appropriate order
Private Sub Visibility()
EndBox=120
For Each ControlOnForm As Control In Me.Controls
If (TypeOf ControlOnForm Is ComboBox) AndAlso (ControlOnForm.Text = "END OF CYCLE EVENT (G 192)") Then
'* Find the lowest number ComboBox with "END OF CYCLE"
EndBox = Math.Min(Cint(ControlOnForm.Name.substring(8)),EndBox)
End If
Next
For Each ControlOnForm As Control In Me.Controls
Dim ControlIndex as Integer=Cint(ControlOnForm.Name.substring(8))
If ControlIndex > EndBox + 1 And ControlIndex < 120 Then
ControlOnForm.Visible = False
If TypeOf ControlOnForm Is ComboBox Then
ControlOnForm.Text = "NULL OPCODE"
ElseIf TypeOf ControlOnForm Is TextBox Then
ControlOnForm.Text = "0"
End If
End If
Next
End Sub
NOTE: This is all untested code, so there may be a mistake or two