从主 winForm 按钮设置用户控件的文本框值
Set the textbox value of usercontrol from Main winForm Button
我有一个包含一个按钮 'btnSet' 的主表单 'frmMaster',
在这个主窗体中,我在用户控件上放置了一个面板控件,
在这个用户控件上有一个文本框,
我的问题是当我从主窗体单击按钮 'btnset' 时如何设置此文本框的值,
例如:当我从主窗体单击 'btnset' 时,用户控件上文本框的值将为 "Welcome"
我在用户控件中输入:
Public Property TextBoxTxt () As String
Get
Return txtText1.Text
End Get
Set(value As String)
txtText1.Text = value
End Set
End Property
在我放入按钮的主窗体上:
Dim uc As New ucControl1
uc.txtText1.Text= "Welcome!"
您需要在用户控件中添加 public
属性,例如
Public Property TextBoxMessage As String
Get
Return textbox.Text
End Get
Set(ByVal value As String)
textbox.Text = value
End Set
End Property
然后您可以从您的 frmMaster 中显示一条消息:
usercontrol.TextBoxMessage = "Welcome!"
在用户控件中:
Public Property TextBoxTxt () As String
Get
Return Me.textbox.Text
End Get
Set(value As String)
Me.txtebox.Text = value
End Set
End Property
在您均匀点击按钮 'btnset' :
Private Sub btnset_Click(sender As Object, e As EventArgs) Handles btnset.Click
Dim uc As New MyUserControl
uc.TextBoxTxt ="Welcome!"
End Sub
您的用户控件必须像:
Public Class UserControl1
Private Sub UserControl1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Public Property TextBoxTxt() As String
Get
Return txtText1.Text
End Get
Set(value As String)
txtText1.Text = value
End Set
End Property
End Class
在您的 MainForm 中添加一个按钮 "btnSet" 和一个面板 "Panel1",因此您在 MainForm 中的代码必须像:
Public Class frmMaster
Private Sub btnSet_Click(sender As Object, e As EventArgs) Handles btnSet.Click
Dim uc As New UserControl1
uc.txtText1.Text = "Welcome!"
Panel1.Controls.Add(uc)
End Sub
Private Sub frmMaster_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
End Class
我已经为你创建了一个简单的 exemple
我有一个包含一个按钮 'btnSet' 的主表单 'frmMaster', 在这个主窗体中,我在用户控件上放置了一个面板控件, 在这个用户控件上有一个文本框, 我的问题是当我从主窗体单击按钮 'btnset' 时如何设置此文本框的值, 例如:当我从主窗体单击 'btnset' 时,用户控件上文本框的值将为 "Welcome"
我在用户控件中输入:
Public Property TextBoxTxt () As String
Get
Return txtText1.Text
End Get
Set(value As String)
txtText1.Text = value
End Set
End Property
在我放入按钮的主窗体上:
Dim uc As New ucControl1
uc.txtText1.Text= "Welcome!"
您需要在用户控件中添加 public
属性,例如
Public Property TextBoxMessage As String
Get
Return textbox.Text
End Get
Set(ByVal value As String)
textbox.Text = value
End Set
End Property
然后您可以从您的 frmMaster 中显示一条消息:
usercontrol.TextBoxMessage = "Welcome!"
在用户控件中:
Public Property TextBoxTxt () As String
Get
Return Me.textbox.Text
End Get
Set(value As String)
Me.txtebox.Text = value
End Set
End Property
在您均匀点击按钮 'btnset' :
Private Sub btnset_Click(sender As Object, e As EventArgs) Handles btnset.Click
Dim uc As New MyUserControl
uc.TextBoxTxt ="Welcome!"
End Sub
您的用户控件必须像:
Public Class UserControl1
Private Sub UserControl1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Public Property TextBoxTxt() As String
Get
Return txtText1.Text
End Get
Set(value As String)
txtText1.Text = value
End Set
End Property
End Class
在您的 MainForm 中添加一个按钮 "btnSet" 和一个面板 "Panel1",因此您在 MainForm 中的代码必须像:
Public Class frmMaster
Private Sub btnSet_Click(sender As Object, e As EventArgs) Handles btnSet.Click
Dim uc As New UserControl1
uc.txtText1.Text = "Welcome!"
Panel1.Controls.Add(uc)
End Sub
Private Sub frmMaster_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
End Class
我已经为你创建了一个简单的 exemple