如何从另一个表单引用变量表单上的变量控件?需要在 运行 程序中重命名 button.Tag
How to reference a variable control on a variable form from another form? Need to get the button.Tag renamed in running program
这是我的问题:
我有一个带有树视图的表单。
该树视图显示了所有内容:
- 我项目中的其他形式 parents
- 所有按钮名称都是子项
- 所有按钮标签都是按钮名称的子元素。
当我 select 树视图中的按钮名称时,我将 selection 显示为
(带有按钮名称的文本框 1)
(带有按钮标签的文本框 2)
(带有表单名称的文本框 3)
我有 1 个空文本框,我想手动填充,以更新树视图中 selected 按钮的按钮标签。
不管怎样,我试过的所有代码都没有更新任何东西。
这是目前为止的代码,但似乎不起作用...
我的代码:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim asm = System.Reflection.Assembly.GetExecutingAssembly
Dim myTypes As Type() = asm.GetTypes()
Dim frm As Form
For Each t As Type In myTypes
If t.IsSubclassOf(GetType(System.Windows.Forms.Form)) AndAlso TextBox1.Text = t.Name Then
frm = CType(Activator.CreateInstance(t), Form)
frm.Hide()
Dim thisButtonName As String = TextBox3.Text ' This is the name of the button I'm looking for
Dim thisButtonName2 As String = TextBox2.Text ' this is the new tag name for that button
' Loop all controls in this form
For Each ctrl As Control In Controls
' Is this control a button
If TypeOf (ctrl) Is Button Then
' Is this the correct button
If CType(ctrl, Button).Name = thisButtonName Then
CType(ctrl, Button).Tag = thisButtonName2
End If
End If
Next
End If
Next
TreeView1.Nodes.Clear()
For Each formprop In My.Forms.GetType.GetProperties
Dim node = Me.TreeView1.Nodes.Add(formprop.Name)
Dim form As Form = CType(formprop.GetValue(My.Forms, Nothing), Form)
ControlsTree(node, form.Controls)
Next
End Sub
我认为这里有两个基本问题:
- 您的循环
For Each ctrl As Control In Controls
没有迭代 frm
变量引用的其他表单对象中的控件。 Controls
属性 将默认为此表单的控件集,而不是 frm.Controls
。
- 您似乎正试图在 运行 时更改 class 定义中的
Tag
。这是不可能的,AFAIK,尤其是在您在这里尝试的情况下。每次创建该表单对象的新实例时,您都将按照编译时的方式初始化该对象。您可以更改对象的 运行ning 实例的 Tag
值,但如果不使用 [=] 之类的技术,则无法更改 class 的 default
值17=].
这是我的问题: 我有一个带有树视图的表单。 该树视图显示了所有内容:
- 我项目中的其他形式 parents
- 所有按钮名称都是子项
- 所有按钮标签都是按钮名称的子元素。
当我 select 树视图中的按钮名称时,我将 selection 显示为 (带有按钮名称的文本框 1) (带有按钮标签的文本框 2) (带有表单名称的文本框 3)
我有 1 个空文本框,我想手动填充,以更新树视图中 selected 按钮的按钮标签。
不管怎样,我试过的所有代码都没有更新任何东西。
这是目前为止的代码,但似乎不起作用...
我的代码:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim asm = System.Reflection.Assembly.GetExecutingAssembly
Dim myTypes As Type() = asm.GetTypes()
Dim frm As Form
For Each t As Type In myTypes
If t.IsSubclassOf(GetType(System.Windows.Forms.Form)) AndAlso TextBox1.Text = t.Name Then
frm = CType(Activator.CreateInstance(t), Form)
frm.Hide()
Dim thisButtonName As String = TextBox3.Text ' This is the name of the button I'm looking for
Dim thisButtonName2 As String = TextBox2.Text ' this is the new tag name for that button
' Loop all controls in this form
For Each ctrl As Control In Controls
' Is this control a button
If TypeOf (ctrl) Is Button Then
' Is this the correct button
If CType(ctrl, Button).Name = thisButtonName Then
CType(ctrl, Button).Tag = thisButtonName2
End If
End If
Next
End If
Next
TreeView1.Nodes.Clear()
For Each formprop In My.Forms.GetType.GetProperties
Dim node = Me.TreeView1.Nodes.Add(formprop.Name)
Dim form As Form = CType(formprop.GetValue(My.Forms, Nothing), Form)
ControlsTree(node, form.Controls)
Next
End Sub
我认为这里有两个基本问题:
- 您的循环
For Each ctrl As Control In Controls
没有迭代frm
变量引用的其他表单对象中的控件。Controls
属性 将默认为此表单的控件集,而不是frm.Controls
。 - 您似乎正试图在 运行 时更改 class 定义中的
Tag
。这是不可能的,AFAIK,尤其是在您在这里尝试的情况下。每次创建该表单对象的新实例时,您都将按照编译时的方式初始化该对象。您可以更改对象的 运行ning 实例的Tag
值,但如果不使用 [=] 之类的技术,则无法更改 class 的default
值17=].