Results 1 to 2 of 2

Thread: Showing User Control from another user control....

  1. #1

    Thread Starter
    Banned
    Join Date
    Sep 2010
    Posts
    3

    Showing User Control from another user control....

    I have created three user controls, which will be used on the various forms in my program. However, I need the buttons on one to call the other two. How do I do this? I've been searching everywhere online, and I can't find the answer.

    .

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

    Re: Showing User Control from another user control....

    You don't call a control. The term "call" refers to methods only. You call a method.

    What is it that you are actually trying to do? Are you saying that one UC needs to get data from another? If so, there is a proper way to go about it. Controls on a form shouldn't have to know about each other. The form is the manager of all its child controls so, if data needs to be passed from one control to another, it's the form that should do it. The correct procedure would be:

    1. The UC that needs the data raises an event. It creates an appropriate EventArgs object and passes that to the event.
    2. The form handles that event.
    3. In the event handler, the form gets the data from the second UC, either by getting a property value or calling a method.
    4. The form assigns the data from the second UC to the appropriate property of the EventArgs object in the event handler.
    5. The first UC gets the data from the EventArgs object and uses it appropriately.

    This may sound convoluted but it's really not. It's only a few lines of code in total and keeps everything clearly layered and maintainable. It might not seem like it now but, when you get more experienced, you'll see why this sort of design works.

    I've attached a demo that shows how it all works. It contains a form with two different UCs on it. You can enter text into the TextBox on one and then click the Button on the other and have it display a message box containing the text from the TextBox.
    Attached Files Attached Files
    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

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