开发者

passing values present in one user control to another using VB

i have a user control with a text box and i need to access the value entered in th开发者_如何学编程is text box in a label present in another user control. How do i do that in vb.Thanks in advance.


create a shared event in the first user control (UserControl1):

Friend Shared Event GetTextBoxText(ByVal myString As String)

you can then raise this event with a button on (UserControl1)

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

    'raise the event with the text from the text box
    RaiseEvent GetTextBoxText(TextBox1.Text)

End Sub

on your second user control (UserControl2) add a handler to the event in your Constructor :

Public Sub New()

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.

    'this will let us handle the event from (UserControl1)
    AddHandler UserControl1.GetTextBoxText, AddressOf SetLabelText

End Sub

Private Sub SetLabelText(ByVal myString As String)
    Label1.Text = myString
End Sub

now whenever you click the button on (UserControl1) the text will be displayed in the label on UserControl2

you could also add the event handler on any control and respond to GetTextBoxText event

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜