vb.net,程序运行过程中通过代码添加了button2,button1则是一开始就有,如何点击button1更改button2的属性
时间: 2024-03-12 15:44:54 浏览: 55
你可以使用以下代码来实现在程序运行过程中添加Button2,并在点击Button1时更改它的属性:
```
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'在程序运行过程中添加Button2
Dim btn2 As New Button
btn2.Name = "Button2"
btn2.Text = "Button2"
btn2.Location = New Point(100, 100)
Me.Controls.Add(btn2)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'更改Button2的属性
Dim btn2 As Button = Me.Controls("Button2")
btn2.BackColor = Color.Red '更改背景颜色为红色
btn2.Text = "New Text" '更改文字内容为"New Text"
btn2.Enabled = False '禁用Button2
End Sub
```
在这个代码中,我们首先在Form_Load事件中添加了Button2并指定了它的属性。然后在Button1的Click事件中,我们通过控件名Button2来访问和修改Button2的属性。你可以根据需要来更改Button2的属性,例如背景颜色、文字内容、是否启用等等。
阅读全文