Click here to Skip to main content
15,892,575 members
Home / Discussions / C#
   

C#

 
AnswerRe: POST method Pin
Guffa4-Aug-07 5:48
Guffa4-Aug-07 5:48 
GeneralRe: POST method Pin
suck1234-Aug-07 7:32
suck1234-Aug-07 7:32 
QuestionHow declare event in C# class ? ? ? [modified] Pin
Yanshof30-Jul-07 21:43
Yanshof30-Jul-07 21:43 
AnswerRe: How decline event in C# class ? ? ? Pin
originSH30-Jul-07 21:54
originSH30-Jul-07 21:54 
AnswerYou mean: 'declare'? Re: How decline event in C# class ? ? ? Pin
jhwurmbach30-Jul-07 21:57
jhwurmbach30-Jul-07 21:57 
GeneralRe: You mean: 'declare'? Re: How decline event in C# class ? ? ? Pin
Yanshof30-Jul-07 22:02
Yanshof30-Jul-07 22:02 
GeneralRe: You mean: 'declare'? Re: How decline event in C# class ? ? ? Pin
originSH30-Jul-07 22:06
originSH30-Jul-07 22:06 
AnswerRe: How decline event in C# class ? ? ? [modified] Pin
Martin#30-Jul-07 22:41
Martin#30-Jul-07 22:41 
Hello,


Yanshof wrote:
is to send events from the some of the user control to this simple C#

Your UserControl should implement an event like this:
public event EventHandler SomethingChanged;

and fire it like this:
if (SomethingChanged!= null) 
{
    SomethingChanged(this, EventArgs.Empty); //or implement your own EventArgs
}

Your class, (which holds the instance of the form where the usercontrols are on) should implement a method which returns a list of your UserControl:
private ArrayList GetAllUserControls(Control c, ArrayList allUserControls)
{
    if(allUserControls == null)
    {
	allUserControls = new ArrayList();
    }
    if(c!=null)
    {
	if(c.Controls!=null)
	{
            foreach(Control actControl in c.Controls)
            {
		YourUserControl actYourControl = actControl as YourUserControl;
		if(actYourControl!=null)
		{
                    allUserControls.Add(actYourControl);
		}
		//recursive call
		allUserControls= GetAllUserControls(actControl, allUserControls);
            }
	}
    }
    return allUserControls;
}

And call it like that:
ArrayList allUserControls = GetAllUserControls(yourForm, null);

Then you can register the Events.
I would recommend a method which does also the unregistering for your Dispose method.
private void EditEvent(ArrayList allUserControls, bool register)
{
    foreach(object o in allUserControls)
    {
	YourUserControl actYourControl = o as YourUserControl;
	if(actYourControl!=null)
	{
            if(register)
            {
		actYourControl.SomethingChanged+=new EventHandler(actYourControl_SomethingChanged);
            }
            else
            {
		actYourControl.SomethingChanged-=new EventHandler(actYourControl_SomethingChanged);
            }
        }
    }
}

And call it like that (you have to pass the List of UserControls which you got from the GetAllUserControls method):
EditEvent(allUserControls, true);

and in the dispose method:
EditEvent(allUserControls, false);

To find the correct instance in the handler:
private void actYourControl_SomethingChanged(object sender, EventArgs e)
{
    YourUserControl actYourControl = sender as YourUserControl;
    if(actYourControl!=null)
    {
 
    }
}



Yanshof wrote:
the C# class will also send events to those user control.

Here I would use a static event in the class.

Hope it helps!






-- modified at 5:21 Tuesday 31st July, 2007

All the best,

Martin

GeneralRe: How decline event in C# class ? ? ? Pin
Yanshof30-Jul-07 23:01
Yanshof30-Jul-07 23:01 
GeneralRe: How decline event in C# class ? ? ? Pin
Martin#30-Jul-07 23:05
Martin#30-Jul-07 23:05 
GeneralRe: How decline event in C# class ? ? ? Pin
Martin#30-Jul-07 23:22
Martin#30-Jul-07 23:22 
QuestionPrint the scrolled content of a windows form Pin
pranu_1330-Jul-07 21:26
pranu_1330-Jul-07 21:26 
AnswerRe: Print the scrolled content of a windows form Pin
il_masacratore30-Jul-07 22:29
il_masacratore30-Jul-07 22:29 
AnswerRe: Print the scrolled content of a windows form Pin
Herman<T>.Instance31-Jul-07 3:00
Herman<T>.Instance31-Jul-07 3:00 
Questionaccess modifiers Pin
pavanteja30-Jul-07 21:20
pavanteja30-Jul-07 21:20 
AnswerRe: access modifiers Pin
originSH30-Jul-07 22:04
originSH30-Jul-07 22:04 
GeneralRe: access modifiers Pin
pavanteja30-Jul-07 23:11
pavanteja30-Jul-07 23:11 
GeneralRe: access modifiers Pin
Sonia Gupta30-Jul-07 23:33
Sonia Gupta30-Jul-07 23:33 
GeneralRe: access modifiers Pin
originSH31-Jul-07 0:03
originSH31-Jul-07 0:03 
GeneralRe: access modifiers Pin
pavanteja31-Jul-07 1:00
pavanteja31-Jul-07 1:00 
Generalsatellite assembly Pin
pavanteja31-Jul-07 19:12
pavanteja31-Jul-07 19:12 
QuestionTop C#/.Net sites Pin
koger30-Jul-07 20:36
koger30-Jul-07 20:36 
AnswerRe: Top C#/.Net sites Pin
Colin Angus Mackay30-Jul-07 21:20
Colin Angus Mackay30-Jul-07 21:20 
GeneralRe: Top C#/.Net sites Pin
originSH30-Jul-07 22:10
originSH30-Jul-07 22:10 
QuestionEdit icon is displaying in rowheader for datagrid Pin
Software_Guy_12330-Jul-07 19:57
Software_Guy_12330-Jul-07 19:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.