vb.net - 如何循环遍历具有按顺序排列的控件名称的systems.windows.forms.controls?

标签 vb.net

我有按顺序命名的单选按钮,例如“Jbtn1”到“Jbtn20”。我尝试使用 for 循环使用 set toolTip 来命名这些按钮。

 Dim toolTip1 As New ToolTip()
 For j As Integer = 1 To 20
   Dim pinInfo As String = "J2-" & j
   'Tried to convert the buttonName as Control , but got an error as 
   'Value of string cannot be converted to systems.windows.forms.control 
   Dim buttonName As Control = "Jbtn" & j
   toolTip1.SetToolTip(buttonName, pinInfo)
 Next

如有任何建议,我们将不胜感激。

最佳答案

你可以这样做:

Dim toolTip1 As New ToolTip()
For j As Integer = 1 To 20
    Dim pinInfo As String = "J2-" & j
    Dim control As Control = Me.Controls.Item("Jbtn" & j)
    toolTip1.SetToolTip(control, pinInfo)
Next

此代码使用表单的 Controls 属性来访问表单上的控件。您可以使用控件上的索引器按名称查找各个控件。

关于vb.net - 如何循环遍历具有按顺序排列的控件名称的systems.windows.forms.controls?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34798520/

相关文章:

vb.net - 如何使用 youtube API 对视频添加评论???有人可以告诉我怎么做吗?

vb.net - 找不到引用的组件 'Microsoft.VisualBasic'

vb.net - 删除控件集合

html - 如何在运行时更改表中 td 的颜色?网络

windows - 如何在一个语句中评估两个函数?

XML 架构验证 - 行号始终返回 0

mysql - 使用其他 2 个表的计算更新表

mysql - Reader 关闭时读取无效 (VB.Net)

vb.net - 部署到 IIS 后,RDLC 报告没有发布到?

.net - 如何使用VB.Net Winform App知道对话框是警报或确认或提示?