?
Solved

How do I get a FlowControlPanel to recieve messages from UserControls that it contains

Posted on 2010-01-12
8
Medium Priority
?
263 Views
Last Modified: 2013-12-17
I have a FlowLayoutPanel that is to hold a number of UserControls like the image shows each of the UserControls needs to alert the FlowLayoutPanel or the UserControl that holds it that changes in thier state.  I'm new to C Sharp and having a dikens of a time setting up the Delegate, EventArg, and event handler.  Thanks Guys and Gals
BitTwiddle.jpg
0
Comment
Question by:graber
  • 4
  • 4
8 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 26297800
Are they all the same TYPE of UserControl?...or just multiple instances of the same UserControl?
0
 
LVL 2

Author Comment

by:graber
ID: 26298239
The FlowControl is a UserControl with a LayoutFlowControl added from the toolbox the other UserControls, the one contained by the the LayoutFlowControl is an extenstion of UserControl. One of the message will pass will pass key states, another click with the instance that was clicked.  But I would also like to know how to pass messages between two unrealted classes as well.

public partial class SW : UserControl
{}
public partial class FlowPanel : UserControl
{}

Open in new window

BitTwiddle.jpg
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 26299225
I would think a simple custom event would do the job:

    public partial class SW : UserControl
    {
        public delegate void MyEventDelegate(SW source, string something);
        public event MyEventDelegate StatusChanged;

        private void Foo()
        {
            // something has changed...raise our event!
            StatusChanged(this, "Changed @ " + DateTime.Now.ToString());
        }
    }

Then, when you create instances of SW, you just subscribe to the custom event:

    public partial class FlowPanel : UserControl
    {
        public FlowPanel()
        {
            SW sw1 = new SW();
            sw1.StatusChanged += new SW.MyEventDelegate(sw1_StatusChanged);
        }

        void sw1_StatusChanged(SW source, string something)
        {
            // ...do something with "source" or "something"...
        }

    }

Hope that helps...
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 2

Author Comment

by:graber
ID: 26299925
Thanks Idle... will give it a try tomarrow morning.  Just got "C# In Depth" by Jon Skeet in the mail.  I has several sections on this very thing.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 26300148
No problem...take your time and ask questions.  You can pass anything out in your custom event.  Just need to change the "signature" of the delegate.
0
 
LVL 2

Author Comment

by:graber
ID: 26332218
Idle thanks for your patience.  I follow your example. Shouldn't the delegates be declared outside of the UserControl space?  I guess, since it's been declared public it doesn't matter.  In some of my research "they" are telling me to put my arguments into an EventArgs derived class of there own.  Is that necessary?

Thanks Idle
Gregg
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 26332635
I like to put the delegate with the UserControl since that is the "source" of the event.  Technically speaking it can be anywhere...really just a matter of preference here.  The example in EventArgs() has the delegate inside the source class as well:
http://msdn.microsoft.com/en-us/library/system.eventargs.aspx

Using EventArgs isn't necessary, though, it's just the model that Microsoft has decided to use in its own framework.  If you want to follow their lead then be my guest.  The "signature" of your delegate/event would at least look familiar to most people...  =)

0
 
LVL 2

Author Closing Comment

by:graber
ID: 31676334
Thanks again Idle.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Is your organization moving toward a cloud and mobile-first environment? In this transition, your IT department will encounter many challenges, such as navigating how to: Deploy new applications and services to a growing team Accommodate employee…
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question