如何通过“字符串名称"获取控件属性? [英] How To Get Control Property by "String Name"?

查看:30
本文介绍了如何通过“字符串名称"获取控件属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下一个循环中通过编码创建了按钮和文本框,结果

i have been created buttons and textboxs by coding in 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"
'...

我想在单击按钮时获得文本框属性,当单击 button_1.Name.ToString 时,我可以获得按钮属性但我无法获得 text_1,2,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_" &button_1.Name.ToString 但我无法将此字符串转换为对象.

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.

更新

这是我用来在循环中加载控件的代码:

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

更新 2

这里还有一些代码:

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

推荐答案

您可以通过 Form.Controls 属性按名称访问控件,例如:

You can access the controls by name via the Form.Controls property, for instance:

Dim text1 As TextBox = CType(Me.Controls("text_1"), TextBox)

这篇关于如何通过“字符串名称"获取控件属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆