?
Solved

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

Posted on 2010-01-12
8
Medium Priority
?
260 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

 
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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

764 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