vb.net 动态获取控件(按钮)的名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15266954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the name of controls (buttons) dynamically
提问by Ikong
I have 10 buttons namely button01
, button02
... button10
. What I want is how to manipulate it.
我有10个按钮,即button01
,button02
... button10
。我想要的是如何操纵它。
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.
我需要这样做,因为我有 10 个或更多按钮,我想通过初始化来操纵它。这样我就不用手动一一做了。我不知道该怎么做。我还是 .NET 的新手。
回答by Tim Schmelter
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.
您可以使用一个Panel
或另一个容器控件来对相关控件进行逻辑分组。然后使用MyPanel.Controls.OfType(Of Button)()
过滤并找到那里的所有按钮。
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:
另一种方法是将它们全部放入数组或其他集合类型中,例如List(Of Button)
first 并在之后循环它们:
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:
最后,您可以使用ControlCollection.Find
给定字符串的名称查找控件:
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
回答by Rajaprabhu Aravindasamy
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:
假设您将这些控件放在一个名为 的面板中PnlTest
,那么您必须这样做:
For Each xControls As Control In PnlTest.Controls
If TypeOf xControls Is Button Then
xControls.Text = "blah" 'from database...or something
End If
Next
回答by SysDragon
Simply:
简单地:
For i As Integer = 1 To 10
Me.Controls("button" & i.ToString("00")).Text = "blah"
Next
回答by Bj?rn Roberg
You could try using the method FindControlas such from the WebControl:
您可以尝试使用WebControl 中的FindControl方法:
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.
编辑:我主要使用 C#,而不是 VB,所以它可能需要一些改动。但是,方法与我相信的相同。
回答by fernando yevenes
zeroyevi cubosoft.cl -- DevExpress -- la clave esta (bar.button.item) en Me.RibbonControl.Items("NAME_BUTTON" ).Enabled = True or false
zeroyevi cubosoft.cl -- DevExpress -- la clave esta (bar.button.item) en Me.RibbonControl.Items("NAME_BUTTON" ).Enabled = True 或 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