Resolved Access 3 panels in a For Each...Next Statement

benshaws

Hobbyist Programmer
Joined
Sep 19, 2013
Messages
18
Location
Leeds, UK
Programming Experience
5-10
Hi,

I had a panel with buttons on. I need to loop through the buttons and apply settings from a database. This I can do with the one panel.

I now have three panel's. I can do the same by repeating the code for the first panel in another two code blocks. However, this does not seem to be the way it should be done.

VB.NET:
'Read the EPOS buttons from the settings database and apply to the set EPOS buttons.
Dim ds As New DataSet
Dim strQuery As String = "SELECT BUTTONID, LABEL, DESCRIPTION, COLOUR FROM SALESBUTTONS"
FillDataSet(ds, strQuery, "SALESBUTTONS")

'Apply each setting to each button.
For Each Btn As Button In Panel1.Controls.OfType(Of Button)()
    For Each dr As DataRow In ds.Tables(0).Rows
        If dr.Item(0) = Btn.Name Then
            If dr.Item(2).ToString = String.Empty Then
                Btn.Enabled = False
                 Btn.Visible = False
            Else
                Btn.Enabled = True
                 Btn.Visible = True
            End If
        End If
    Next
Next

I have tried a few things in line 7 above, such as

VB.NET:
For Each Btn As Button In Panel1.Controls.OfType(Of Button)() And In Panel2.Controls.OfType(Of Button)()
'etc
Next

But I can't seem to work out the correct syntax. Maybe, I am coming at this wrong - I don't know.

Thanks for looking.
 

benshaws

Hobbyist Programmer
Joined
Sep 19, 2013
Messages
18
Location
Leeds, UK
Programming Experience
5-10
Hi again,

The postman's just arrived with my VB.net Language Reference that I ordered.

As I gather it now, I group the three panels together in a collection then loop through the objects in a collection.
 

benshaws

Hobbyist Programmer
Joined
Sep 19, 2013
Messages
18
Location
Leeds, UK
Programming Experience
5-10
Hi JohnH,

Thanks for coming back to me. I had thought about the separate method. The sub itself, this code is in, is a separate method.

I am attempting to put the buttons on each panel into a collection. And have this:-

VB.NET:
Dim eposbuttons As New Collection From {
    Panel1.Controls,
    Panel2.Controls
}

VB.NET:
For Each Btn As Button In eposbuttons.OfType(Of Button)()
'etc'
Next

But the settings are not being applied to the collection. If anyone has any advice, I would be grateful. :)
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,439
Location
Norway
Programming Experience
10+
That will get you two ControlCollection objects in a collection, you would need a nested loop. (for each collection, for each item in collection)

This should be more like it:
VB.NET:
Dim eposbuttons = Panel1.Controls.OfType(Of Button).Concat(Panel2.Controls.OfType(Of Button))
For Each Btn In eposbuttons
Side note, you should not use the Collection class in new code, it was for classic VB upgrades. Use the generic collections instead, List(Of T) for example.
 

benshaws

Hobbyist Programmer
Joined
Sep 19, 2013
Messages
18
Location
Leeds, UK
Programming Experience
5-10
That will get you two ControlCollection objects in a collection, you would need a nested loop. (for each collection, for each item in collection)

I had sort of started to figure that out. I was using some advice I had read about using a message box to show what was in my collection and it was returning an item count of "2" and "ControlCollection" "stuff" at the first index.

The third panel is now added (and it's 47 buttons) and I have adjusted the above JohnH code to suit.

JohnH thanks again for you help, and have noted your side note as well.:)(y)
 
Top Bottom