Reputation: 2570
I have 10 buttons namely button01
, button02
... button10
. What I want is how to manipulate it.
For x=1 to 10
button(x).text = "blah" 'from database...or something
next
I need to do this because I have 10 buttons or more and I want to manipulate it through initialization. So that I don't do it manually one by one. I don't know how to do this. I'm still new in .NET.
Upvotes: 4
Views: 27162
Reputation: 460058
You should not need to do it in this error-prone way just to save you some lines of code. But if you really want....
You can use a Panel
or another container control that groups the related controls logically. Then use MyPanel.Controls.OfType(Of Button)()
to filter and find all the buttons there.
For Each btn As Button In MyPanel.Controls.OfType(Of Button)()
btn.Text = "blah" 'from database...or something
Next
Another way is to put them all in an array or other collection type like List(Of Button)
first and loop over them afterwards:
Dim myButtons = {button1, button2, button3, button4, button5, button6}
For Each btn In myButtons
btn.Text = "blah" 'from database...or something
Next
Last you could use ControlCollection.Find
to find controls with a given string for its name:
For i As Int32 = 1 To 10
Dim btns = Me.Controls.Find("button" & i, True)
If btns.Length > 0 Then
btns(0).Text = "blah" 'from database...or something
End If
Next
Upvotes: 4
Reputation: 67207
You have to iterate through the parent container of these buttons.
Say you are holding these controls inside a panel called PnlTest
, then you have to do it like this:
For Each xControls As Control In PnlTest.Controls
If TypeOf xControls Is Button Then
xControls.Text = "blah" 'from database...or something
End If
Next
Upvotes: 1
Reputation: 152
zeroyevi cubosoft.cl -- DevExpress -- la clave esta (bar.button.item) en Me.RibbonControl.Items("NAME_BUTTON" ).Enabled = True or false
Private Sub GetSearchPerfilModulosBotones(ByVal _id_perfil As String)
Dim dt As New DataTable
Dim _act_btns As Boolean
Dim _name_btns As String
Dim _name_module As String = Me.Name.ToString()
Try
Dim _ControlDatosSistema As New ControlDatosSistema()
With _ControlDatosSistema
dt = .GetSearchPerfilModulosBotones(_id_perfil, _name_module )'SQL QUERY
If (dt.Rows.Count >= 1) Then
For Each row As DataRow In dt.Rows
_act_btns = row("ACT_BTNS") 'BOTONES PERFIL True or False
_name_btns = row("NAME_BTNS").ToString()'NOMBRE BOTONES TABLA
Me.RibbonControl.Items(_name_btns ).Enabled = _act_btns
Next
End If
End With
_ControlDatosSistema = Nothing
dt.Dispose()
Catch ex As Exception
End Try
End Sub
Upvotes: -2
Reputation: 9888
Simply:
For i As Integer = 1 To 10
Me.Controls("button" & i.ToString("00")).Text = "blah"
Next
Upvotes: 1
Reputation: 2366
You could try using the method FindControl as such from the WebControl:
For x=1 to 10
FindControl("button" & if(x < 10, "0" & x, x) = "blah" 'from database...or something
next
EDIT: I primarily use C#, not VB, so it may need some alteration. However, the approach is the same I would believe.
Upvotes: 0