Author Topic: Array Question  (Read 2198 times)

Jesse

  • Jr. Member
  • **
  • Posts: 50
    • View Profile
Array Question
« 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

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5262
    • View Profile
    • AdvancedHMI
Re: Array Question
« Reply #1 on: November 19, 2013, 04:29:39 PM »
Do you mean a control array like the old VB6 to where you can access them in code by an array?

Jesse

  • Jr. Member
  • **
  • Posts: 50
    • View Profile
Re: Array Question
« Reply #2 on: November 19, 2013, 07:09:29 PM »
Yes I think so.

Right now my code is...
Code: [Select]
       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.



Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5262
    • View Profile
    • AdvancedHMI
Re: Array Question
« Reply #3 on: November 19, 2013, 08:05:09 PM »
It is possible, but requires a little bit of code to setup.

First create a field:

Code: [Select]
Private ComboBoxArray(10) as ComboxBox
Then in the Load event, point the array elements to the comboBoxes
Code: [Select]
......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.
Code: [Select]
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:
Code: [Select]
....FormLoad.....
For Each ControlOnForm As Control In Me.Controls
     If TypeOf ControlOnForm Is ComboBox Then
           ComboBoxArray(ControlOnForm.Name.Substring(8))=ControlOnForm
     End IF
Next

Jesse

  • Jr. Member
  • **
  • Posts: 50
    • View Profile
Re: Array Question
« Reply #4 on: November 19, 2013, 08:16:24 PM »
I think I'm going to try one of the first two. You lost me on that third one ha.

Jesse

  • Jr. Member
  • **
  • Posts: 50
    • View Profile
Re: Array Question
« Reply #5 on: November 19, 2013, 08:25:05 PM »
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

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5262
    • View Profile
    • AdvancedHMI
Re: Array Question
« Reply #6 on: November 19, 2013, 08:36:21 PM »
Modifying your code to use Method3. This assumes all of your ComboBoxes are named:

ComboBox1
ComboBox2
ComboBox3
.
.
.

In the appropriate order

Code: [Select]
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