-
Oct 14th, 2020, 06:25 PM
#1
Thread Starter
Lively Member
[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
-
Oct 14th, 2020, 06:44 PM
#2
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.
-
Oct 14th, 2020, 07:05 PM
#3
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:
Public Class UserControl2
'Create a pass-through property for the Text property of the TextBox.
Public Property TextBoxText As String
Get
Return TextBox1.Text
End Get
Set
TextBox1.Text = Value
End Set
End Property
Public Event TextBoxTextChanged As EventHandler
Protected Overridable Sub OnTextBoxTextChanged(e As EventArgs)
RaiseEvent TextBoxTextChanged(Me, e)
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
'Notify an listeners that the data has changed.
OnTextBoxTextChanged(EventArgs.Empty)
End Sub
End Class
Form2 needs a UserControl2 added:
vb.net Code:
Public Class Form2
Public Property UserControlTextBoxText As String
Get
Return UserControl21.TextBoxText
End Get
Set
UserControl21.TextBoxText = Value
End Set
End Property
Public Event UserControlTextBoxTextChanged As EventHandler
Protected Overridable Sub OnUserControlTextBoxTextChanged(e As EventArgs)
RaiseEvent UserControlTextBoxTextChanged(Me, e)
End Sub
Private Sub UserControl21_TextBoxTextChanged(sender As Object, e As EventArgs) Handles UserControl21.TextBoxTextChanged
OnUserControlTextBoxTextChanged(EventArgs.Empty)
End Sub
End Class
UserControl1 needs a TextBox and a Button added:
vb.net Code:
Public Class UserControl1
Private WithEvents f2 As Form2
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
If f2 IsNot Nothing Then
'Push data when a change occurs at this end.
f2.UserControlTextBoxText = TextBox1.Text
End If
End Sub
Private Sub f2_UserControlTextBoxTextChanged(sender As Object, e As EventArgs) Handles f2.UserControlTextBoxTextChanged
'Pull data when a change occurs at the other end.
TextBox1.Text = f2.UserControlTextBoxText
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Make sure the default instance of Form2 is displayed and focused.
f2 = Form2
f2.Show()
f2.Activate()
End Sub
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|