My own custom event does not work within another class

aewarnick

Well-known member
Joined
Jan 29, 2003
Messages
1,031
I must have done something wrong, but what?

C#:
//This is my handler and EventArgs.
//They were declared inside the namespace block of my custom control.
public delegate void ItemSelectedEventHandler(object sender, ItemSelectedEventArgs e);

public class ItemSelectedEventArgs : EventArgs
{
	public int Index=-1;
	public string Item="";
	
	public ItemSelectedEventArgs()
	{
		Index= FileBrowserLB.selIndex;
		Item= FileBrowserLB.selFile;
	}
}

//This is the event variable declared inside the controls constructor:
public event ItemSelectedEventHandler ItemSelected;

The rest of the code is from a form class that has the custom control on it.
C#:
//In the forms constructor:
this.fileBrowser.ItemSelected += new ItemSelectedEventHandler(this.ItemSel);

//The method in the form that should capture the event:
void ItemSel(object sender, ItemSelectedEventArgs e)
{
MessageBox.Show(e.Index+"  "+e.Item);
}
The messageBox never pops up indicating that the event does not work. However, it works inside the control but just not outside in another class.

What am I missing?
 
a Method I made inside the control that has nothing in it. I did not know what to put in it. This is the call to it:
this.OnItemSelected(new ItemSelectedEventArgs());

Is there some special code to make event handlers fire that I dont know about?

The problem is that I dont know how to make the event raise when it is in another form.
 
Last edited by a moderator:
Thhhhiiiiiisss was soooo simple!! Why did noone tell me about this. The documentation I found on the internet did not include this!!!!!!!!!!!!!!!!!

this.Invoke(ItemSelected, new object[]{this, new ItemSelectedEventArgs()});

intead of using
this.OnItemSelected(new ItemSelectedEventArgs());

I found this out by curiosity alone!
 

Similar threads

Back
Top