Pejman Poh
Pejman Poh

Reputation: 503

Dynamically Building Buttons

I am using VB.Net. I have a list of 13 buttons. The names of these buttons are constructed through a combination of 3 different variables.

I need to disable all these buttons. I do not want to iterate through each button to disable them. I would like to dynamically call these buttons in a loop and then disable them. Something like this;

For Count = 1 To 13
    PeriodRoomID = ("R" & ds.Tables("sqlSpecRoomRequest").Rows(i).Item(3) & Count)
    PeriodRoomID.Enabled = False
    PeriodRoomID.Backcolor = Color.Gray
Next

The problem is that once creating the variable PeriodRoomID, I can't address it as a button because;

How do I get around this?

Upvotes: 3

Views: 118

Answers (1)

Mark Hall
Mark Hall

Reputation: 54532

You can try the Controls.Find method.

dim cntrl() as Control
For Count = 1 To 13 
    PeriodRoomID = ("R" & ds.Tables("sqlSpecRoomRequest").Rows(i).Item(3) & Count) 
    cntrl = Me.Controls.Find(PeriodRoomID,True)
    cntrl(0).Enabled = False
    cntrl(0).Backcolor = Color.Gray
Next 

Upvotes: 3

Related Questions