Results 1 to 13 of 13

Thread: [RESOLVED] UserControl communication

  1. #1

    Thread Starter
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Resolved [RESOLVED] UserControl communication

    Whats your preferred method of sharing information between user controls?

    Information stored in variables (strings,arrays, list of etc) .....

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

    Re: UserControl communication

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: UserControl communication

    Hello john. But i'm not sure how to handle it in MainForm.

    Example from UserControl

    vb Code:
    1. ''' <summary>
    2.     ''' Records inputted data to be used later
    3.     ''' </summary>
    4.     ''' <param name="txtField">txtClient, txtDescription</param>
    5.     ''' <returns>List of string of saved data</returns>
    6.     ''' <remarks>Move to method</remarks>
    7.     Private Function UpdateSavedInputInformation(ByVal txtField As String) As List(Of String)
    8.         ' Declare the new input value and add to new list of string
    9.         Dim newInput As New List(Of String)(Array.ConvertAll(Of TextBox, String) _
    10.                                             (Me.Controls.OfType(Of TextBox).Where(Function(n) n.Name.Contains(txtField)).Cast(Of TextBox).ToArray, Function(tb) tb.Text))
    11.         newInput.RemoveAll(Function(v) v = "")
    12.  
    13.         ' Return result
    14.         Return newInput
    15.     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

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: UserControl communication

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: UserControl communication

    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?

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: UserControl communication

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: UserControl communication

    Quote Originally Posted by jmcilhinney View Post
    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:
    1. #Region "Instance Variables"
    2.     Public _newClient As New List(Of String)
    3. #End Region

    Now i call the previous posted function

    vb Code:
    1. _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.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: UserControl communication

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: UserControl communication

    Quote Originally Posted by jmcilhinney View Post
    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.
    I'm with you now. I guess more detail is always better then less. But you are correct with what my problem is. Only one control is displayed at a time.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: UserControl communication

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,390

    Re: UserControl communication

    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:
    1. Private WithEvents mc_Control2 As Control2
    2.     Public Property Control2() As control2
    3.         Get
    4.             Return mc_Control2
    5.         End Get
    6.         Set(ByVal value As control2)
    7.             mc_Control2 = value
    8.         End Set
    9.     End Property

    Hope this helps
    Kris

  12. #12

    Thread Starter
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: UserControl communication

    Quote Originally Posted by jmcilhinney View Post
    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.
    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:
    1. #Region "Instance Variables"
    2.     Private _ucTimeSheet As TimeSheet
    3. #End Region
    4.  
    5. #Region "Constructor"
    6.     Public Sub New()
    7.         InitializeComponent()
    8.         _ucTimeSheet = New TimeSheet()
    9.     End Sub
    10. #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:
    1. ' Declare the new input value and add to new list of string
    2.         Dim newInput As New List(Of String)(Array.ConvertAll(Of Label, String) _
    3.                                             (_ucTimeSheet.Controls.OfType(Of Label).Where(Function(n) n.Name.Contains(lblField)).Cast(Of Label).ToArray, Function(tb) tb.Text))
    4.         newInput.RemoveAll(Function(v) v = "")
    5.  
    6.         ' Return result
    7.         Return newInput

    vb Code:
    1. Private Sub GenerateNewTimeSheetInformation()
    2.         ' Get Timesheet date information
    3.         _newDate = UpdateSavedLabelInformation("lblDate") ' Save new days date to list
    4.  
    5.         For Each line In _newDate
    6.             Debug.WriteLine(line)
    7.         Next
    8.     End Sub

    Returns default label text and not currently displayed text. Is there an obvious error?

  13. #13

    Thread Starter
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: UserControl communication

    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.

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