从Main winForm Button

时间:2016-05-11 12:18:30

标签: vb.net winforms user-controls

我有一个主要的'frmMaster'形式,它包含一个按钮'btnSet', 在这个主窗体里面我把一个面板控件包含在usercontrol上, 在这个用户控件上有一个文本框, 我的问题是当我从主窗体单击“btnset”按钮时如何设置此文本框的值, 例如:当我从主窗体中单击“btnset”时,usercontrol上的textbox值将为“Welcome”

在userControl中我放:

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!"

3 个答案:

答案 0 :(得分:0)

您需要向用户控件添加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!"

答案 1 :(得分:0)

在用户控件中:

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

答案 2 :(得分:0)

您的UserControl必须如下:

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"和Panel" 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