Results 1 to 3 of 3

Thread: [vb.net]Share data between usercontrols.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2013
    Posts
    108

    [vb.net]Share data between usercontrols.

    Hi, how can I pass data between user controls?

    form1 contains Usercontrol1 and form2 contains usercontrol2.

    Both usercontrols have the size of their respective forms.

    With usercontrol1 I extract a link with the webbrowser object that I would like to transfer to usercontrol2.

    I was thinking of creating public functions in Usercontrol1 which I can access from Usercontrol2.

    But this is easy to say in theory and little in practice. At least as far as my "experience" is concerned ...

    For example, this code is in usercontrol2, create a qr image. But the problem lies in the fact that the link I need to transform into qr is extracted from usercontrol1, so I need to "communicate" between the usercontrols.

    Code:
    Sub getqr
             Dim sitoGoogleQrCode As String = "http://chart.googleapis.com/chart?chs={WIDTH}x{HEIGHT}&cht=qr&chl={TESTO}"
                sitoGoogleQrCode = sitoGoogleQrCode.Replace("{WIDTH}", PictureBox1.Width.ToString()).Replace("{HEIGHT}", PictureBox1.Height.ToString()).Replace("{TESTO}", WebUtility.UrlEncode(usercontrol1.link to extract..))
                Dim client As WebClient = New WebClient()
    
                Dim bytes As Byte() = client.DownloadData(sitoGoogleQrCode)
                client.Dispose()
                Dim memStream As MemoryStream = New MemoryStream(bytes)
                Dim bmpQrCode As Bitmap = New Bitmap(memStream)
                PictureBox1.Image = bmpQrCode
        End Sub
    Or for example In usercontrol1 I have a sub that extracts with a webclient the page title of a site and I want this string to be added to the listbox in Usercontrol2. Would it be feasible?

    Some doubts..

    Should I use functions to return the value of what happens inside the subs? If so, what should I do?

    Also .. would it be possible to communicate data between usercontrols even when one of them is closed?
    For example, if usercontrol1 pulls 100 titles to theoretically be inserted as new items in the listbox in usercontrol2, but the latter hasn't been opened yet, should I actually leave usercontrol2 always open on display ??

    Thank you in advance

    Mattia

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,296

    Re: [vb.net]Share data between usercontrols.

    The user controls should not even know that each other exist. You should pretty much always work with a relationship between a caller and a callee where the caller knows about the callee but not vice versa. In this case uc1 is the caller and form2 the callee in one relationship and form2 the caller and uc2 the callee in a second relationship.

    uc1 creates form2 and pretty much all communication is initiated and performed by uc1. If data needs to be passed from uc1 into form2 then uc1 either sets a property or calls a method and passes an argument. If data needs to be passed from form2 out to uc1 then uc1 gets a property or calls a function and gets the return value. If form2 needs to notify uc1 that there is data available to get then form2 raises an event that uc1 handles and then uc1 gets that data as mentioned previously. This way, form2 is never even aware of the existence of uc1. That is loose-coupling.

    The situation is similar between form2 and uc2. Form2 does all the pushing and pulling of data and uc2 raises an event if it needs to indicate that data is available. If you need to move data from uc1 to uc2 then it's a two step process, i.e. uc1 moves data to form2 and form2 moves that data to uc2. Similarly, if uc2 needs to notify uc1 that data is available, uc2 raises an event that form2 handles and then form2 raises an event that uc1 handles.

    I suggest that you follow the Blog link in my signature below and read my three-part post on Data Among Multiple Forms and also my post on Custom Events.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,296

    Re: [vb.net]Share data between usercontrols.

    Here's a simple example of what I described above that will keep two TextBoxes on two different user controls on two different forms in sync:

    UserControl2 needs a TextBox added:
    vb.net Code:
    1. Public Class UserControl2
    2.  
    3.     'Create a pass-through property for the Text property of the TextBox.
    4.     Public Property TextBoxText As String
    5.         Get
    6.             Return TextBox1.Text
    7.         End Get
    8.         Set
    9.             TextBox1.Text = Value
    10.         End Set
    11.     End Property
    12.  
    13.     Public Event TextBoxTextChanged As EventHandler
    14.  
    15.     Protected Overridable Sub OnTextBoxTextChanged(e As EventArgs)
    16.         RaiseEvent TextBoxTextChanged(Me, e)
    17.     End Sub
    18.  
    19.     Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    20.         'Notify an listeners that the data has changed.
    21.         OnTextBoxTextChanged(EventArgs.Empty)
    22.     End Sub
    23.  
    24. End Class
    Form2 needs a UserControl2 added:
    vb.net Code:
    1. Public Class Form2
    2.  
    3.     Public Property UserControlTextBoxText As String
    4.         Get
    5.             Return UserControl21.TextBoxText
    6.         End Get
    7.         Set
    8.             UserControl21.TextBoxText = Value
    9.         End Set
    10.     End Property
    11.  
    12.     Public Event UserControlTextBoxTextChanged As EventHandler
    13.  
    14.     Protected Overridable Sub OnUserControlTextBoxTextChanged(e As EventArgs)
    15.         RaiseEvent UserControlTextBoxTextChanged(Me, e)
    16.     End Sub
    17.  
    18.     Private Sub UserControl21_TextBoxTextChanged(sender As Object, e As EventArgs) Handles UserControl21.TextBoxTextChanged
    19.         OnUserControlTextBoxTextChanged(EventArgs.Empty)
    20.     End Sub
    21.  
    22. End Class
    UserControl1 needs a TextBox and a Button added:
    vb.net Code:
    1. Public Class UserControl1
    2.  
    3.     Private WithEvents f2 As Form2
    4.  
    5.     Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    6.         If f2 IsNot Nothing Then
    7.             'Push data when a change occurs at this end.
    8.             f2.UserControlTextBoxText = TextBox1.Text
    9.         End If
    10.     End Sub
    11.  
    12.     Private Sub f2_UserControlTextBoxTextChanged(sender As Object, e As EventArgs) Handles f2.UserControlTextBoxTextChanged
    13.         'Pull data when a change occurs at the other end.
    14.         TextBox1.Text = f2.UserControlTextBoxText
    15.     End Sub
    16.  
    17.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    18.         'Make sure the default instance of Form2 is displayed and focused.
    19.         f2 = Form2
    20.         f2.Show()
    21.         f2.Activate()
    22.     End Sub
    23.  
    24. End Class
    Form1 needs a UserControl1 added. Run the project and then click the Button on Form1. You can then type into either TextBox and the other will update to remain in sync.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width