string 如何通过“字符串名称”获取控件属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15076975/
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
How To Get Control Property by "String Name"?
提问by Steven Doggart
i have been created buttons and textboxs by codingin next loop, the result
我已经通过在下一个循环中编码创建了按钮和文本框,结果
'T(x).Name = "text_1"
'T(x).Name = "text_2"
'T(x).Name = "text_3"
'....
'B(x).Name = "button_1"
'B(x).Name = "button_2"
'B(x).Name = "button_3"
'...
and i want to get textbox property whene i click the button,
i can get button property when click like button_1.Name.ToString
but i cant get the text_1,2,3 .... property.
我想在单击按钮时获得文本框属性,单击时可以获得按钮属性,button_1.Name.ToString
但我无法获得 text_1,2,3 .... 属性。
i do some trick by split function button_1.Name.ToString and get the last number
and add it to the textbox name like "text_" & button_1.Name.ToString
but i can't convert this string to object.
我通过拆分函数 button_1.Name.ToString 做一些技巧并获取最后一个数字并将其添加到文本框名称中,例如"text_" & button_1.Name.ToString
但我无法将此字符串转换为对象。
Update
更新
Here's the code I'm using to load the controls in the loop:
这是我用来在循环中加载控件的代码:
C_A_TEXT(x) = New TextBox()
C_A_TEXT(x).Dock = System.Windows.Forms.DockStyle.Fill
C_A_TEXT(x).Location = New System.Drawing.Point(270, 5)
C_A_TEXT(x).Margin = New System.Windows.Forms.Padding(0)
C_A_TEXT(x).Size = New System.Drawing.Size(70, 27)
C_A_TEXT(x).TabIndex = 5
C_A_TEXT(x).Name = "NEW_RECHARGE_COUNT_TEXT_" & x
Update 2
更新 2
Here's some more code:
这里还有一些代码:
AddHandler C_A_BUTTONS(x).Click, AddressOf C_A_BUTTON
Private Sub C_A_BUTTON(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim thisButton As Button = sender Dim A = CType(Me.Controls("NEW_RECHARGE_COUNT_TEXT_1"), TextBox)
MsgBox(A.Text.ToString) 'Error!
End Sub
回答by Steven Doggart
You can access the controls by name via the Form.Controls
property, for instance:
您可以通过Form.Controls
属性按名称访问控件,例如:
Dim text1 As TextBox = CType(Me.Controls("text_1"), TextBox)
回答by Timothy
As a quick useful tip to note, you don't seem to have to specify the type of control within the CType statement for purposes of accessing a control on your form. I came across this when trying to access multiple types of form controls, such as buttons and textboxes, all with the same line of code.
作为一个快速有用的提示,您似乎不必为了访问表单上的控件而在 CType 语句中指定控件的类型。我在尝试使用同一行代码访问多种类型的表单控件(例如按钮和文本框)时遇到了这个问题。
CType(Controls("NAME_OF_CONTROL"), Control)
Note that, rather than specifying exactly what type of control, such as 'TextBox' or 'Button', you simply state 'Control'. This allows you to universally change any type of control, without needing to specify its type.
请注意,您无需指定确切的控件类型,例如“TextBox”或“Button”,而只需声明“Control”即可。这允许您普遍更改任何类型的控件,而无需指定其类型。
I couldn't find this anywhere else, so I thought I'd share it!
我在其他地方找不到这个,所以我想我会分享它!
回答by jmvd70
Below is the code.
下面是代码。
Dim oObj As Object = Me.Controls.Find("control name", True).FirstOrDefault()
Obj.Property = Value
I hope it helps.
我希望它有帮助。
回答by Muhammad Saeed
Dim sometext As TextBox = CType(Me.Controls("sometext "), TextBox)
回答by freschx
The title of the thread and your description of the problem at hand seem a little different from each other.
线程的标题和您对手头问题的描述似乎有些不同。
To answer your title(to find a control by its name) use the following:
要回答您的标题(按名称查找控件),请使用以下命令:
Dim myControlToFind = LayoutRoot.FindName("NAMEOFCONTROL")
More information on this method can be found here.
可以在此处找到有关此方法的更多信息。
To answer the descriptionof your issue as (to access a code generated control after it is clicked) do the following:
要回答这个描述你的问题,如(访问被点击后,代码生成的控制),做到以下几点:
In the loop where you are creating the control(s) add the following handler
在您创建控件的循环中添加以下处理程序
Addhandler YOURCONTROL.Clicked, AddressOf Textbox_Clicked
...and then this will handle the click event
...然后这将处理点击事件
Private Sub Textbox_Clicked(sender as object, e as RoutedEventArgs)
Dim tbClicked = Ctype(sender, TextBox)
'You can now access any of the properties of the textbox, for example
Dim txt as String = tbClicked.Text
Dim name as String = tbClicked.Name
Dim height as Double = tbClicked.Height
End Sub