Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I must have done something wrong, but what?

 

//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.

//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?

C#
Posted (edited)

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 don't know about?

 

The problem is that I don't know how to make the event raise when it is in another form.

Edited by aewarnick
C#
Posted

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!

C#

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...