Whats your preferred method of sharing information between user controls?
Information stored in variables (strings,arrays, list of etc) .....
Printable View
Whats your preferred method of sharing information between user controls?
Information stored in variables (strings,arrays, list of etc) .....
UserControls are just like any other objects and they should behave like any other objects. Generally speaking, two UserControls should not have any specific dependency on each other. If you need to get data from one UserControl to another then it is the form's responsibility to do so. Each UserControl should be responsible for its own interface only.
For example, let's say that you have two UserControls on a form. One contains a Button and a TextBox and the other contains a Label. When the user clicks the Button, you want to get the text from the TextBox and display it in the Label. Neither of the the two UserControls should even be aware that the other exists. The first UserControl exposes a property for the Text of the TextBox and an event for the click of the Button. The second UserControl exposes a property for the Text of the Label. When the user clicks the Button, the first UserControl handles the Click event and then raises an event of its own. The form handles that event, gets the text from the property of the first UserControl and then assigns it to the property of the second. That is how to work with objects in all situations. UserControls are the same as any other objects.
Hello john. But i'm not sure how to handle it in MainForm.
Example from UserControl
vb Code:
''' <summary> ''' Records inputted data to be used later ''' </summary> ''' <param name="txtField">txtClient, txtDescription</param> ''' <returns>List of string of saved data</returns> ''' <remarks>Move to method</remarks> Private Function UpdateSavedInputInformation(ByVal txtField As String) As List(Of String) ' Declare the new input value and add to new list of string Dim newInput As New List(Of String)(Array.ConvertAll(Of TextBox, String) _ (Me.Controls.OfType(Of TextBox).Where(Function(n) n.Name.Contains(txtField)).Cast(Of TextBox).ToArray, Function(tb) tb.Text)) newInput.RemoveAll(Function(v) v = "") ' Return result Return newInput End Function
This function is being used in UserControl1, Now if MainForm knows whats going on with UC1 then why does not the Intellisense see and Controls? If modifier is set to friend?
Now this information above is saved as list of string. How do i get this information to mainform to then be used in UserControl2
Forget programming for a moment. What does the word "Private" mean to you? Now ask yourself why your Private method might not be able to be seen by anything else.
Private - nothing else has any business knowing what it is/does
But the information that private method stores the information in is _newClient (public)
Or am i still loosing focus here?
I don't see any _newClient in your code. If you don't show us then we don't know what you're talking about. It's time for you to provide a FULL and CLEAR description of exactly what you're trying to achieve and exactly how you're trying to achieve. You've given us generalities and partial information so all we can do is assume and guess, which is unlikely to lead to a satisfactory solution.
You are correct john. I thought i added originally added a missing piece. Sorry about that.
Outline:
MainForm
UserControl1
UserControl2
On a split container. Panel1 Holds UserControlsX, Panel2 Shows Visible MainForm Controls.
http://imgur.com/3nPG5.png
When the information is entered in UserControl1 :
vb Code:
#Region "Instance Variables" Public _newClient As New List(Of String) #End Region
Now i call the previous posted function
vb Code:
_newClient = UpdateSavedInputInformation("Client")
Now when i press next i need to be able to bring that variable along with me to UserControl2
make more sense? Sorry about lack of info.
You're still not providing the whole story, but I think I can read between the lines. You're not actually displaying these UserControls on the form at the same time, right? You have one on the form, you click a Button on the form and then that UserControl is removed and another one added. You want to transfer some data from the first UserControl to the second, correct? Are you starting to understand what I mean when I say "a FULL and CLEAR description"? We shouldn't have to guess or assume this information. You should be providing it.
In that case it is quite simple and, as I suggested, no different to any other controls. The Next button is presumably on the form. As such, the form is instigating the whole thing. In the Click event handler you get the data from one or more properties of the first UC, destroy the UC, create a new UC and set one or more properties. It's very, very simple: get and set properties, just like you do with any other object all the time.
If i want to share certain info amoungst user controls i do not make it required and do it in a property kind of how an imagelist is a property of listview... like this in control1:
vb Code:
Private WithEvents mc_Control2 As Control2 Public Property Control2() As control2 Get Return mc_Control2 End Get Set(ByVal value As control2) mc_Control2 = value End Set End Property
Hope this helps
Kris
I see now. I have run into a little issue. I can pin point the issue but not sure how to resolve it.
MainForm code: Time sheet is the user control
vb Code:
#Region "Instance Variables" Private _ucTimeSheet As TimeSheet #End Region #Region "Constructor" Public Sub New() InitializeComponent() _ucTimeSheet = New TimeSheet() End Sub #End Region
Now when i try to access User control time sheet i get the defaulted values from design time. My guess is it's the word 'new' creating a new instance from when we started.
For example getting 7 label values from user control TS.
vb Code:
' Declare the new input value and add to new list of string Dim newInput As New List(Of String)(Array.ConvertAll(Of Label, String) _ (_ucTimeSheet.Controls.OfType(Of Label).Where(Function(n) n.Name.Contains(lblField)).Cast(Of Label).ToArray, Function(tb) tb.Text)) newInput.RemoveAll(Function(v) v = "") ' Return result Return newInput
vb Code:
Private Sub GenerateNewTimeSheetInformation() ' Get Timesheet date information _newDate = UpdateSavedLabelInformation("lblDate") ' Save new days date to list For Each line In _newDate Debug.WriteLine(line) Next End Sub
Returns default label text and not currently displayed text. Is there an obvious error?
Oppp's now dont i feel silly. I had a cuppa and took a step back and re examined my code thinking about the logic. My original assumption is that MainForm could easily communicate with the Usercontrol.
Things did not work out as expected which lead to this thread. After going around in a loop i took a step back. And low and behold an obvious reason. I was generating Two new instances of User controls which was why it was leading to dodgy results.
Lessons learned. Take a break, and rethink.