Eventhandler in C# - How to send parameter
-
Hi, My question is "How to send a user defined variable value while calling eventhandler. Pls go thru the below example: 1. I have an array of Pictureboxes arranged on a Panel 2. I have handled the Click event on PictureBoxes as below but dont know: how to pass "string FilePath" as part of "Object sender"
/* I have a string value (FilePath) parameter which I want to send to the function ClickHandler which gets invoked whenerever someone clicks on a PictureBox */ string FilePath; //Has file path info picBoxes[index].Click += new System.EventHandler(ClickHandler); ... ... // Signature of ClickHandler method is as follows public void ClickHandler(Object sender, System.EventArgs e) { // I can get the handle to the PictureBox which was clicked by the user // as follows PictureBox pbx = new PictureBox(); pbx = (PictureBox) ((System.Windows.Forms.PictureBox)sender); // How do I access FilePath ? }
Thanks -
Hi, My question is "How to send a user defined variable value while calling eventhandler. Pls go thru the below example: 1. I have an array of Pictureboxes arranged on a Panel 2. I have handled the Click event on PictureBoxes as below but dont know: how to pass "string FilePath" as part of "Object sender"
/* I have a string value (FilePath) parameter which I want to send to the function ClickHandler which gets invoked whenerever someone clicks on a PictureBox */ string FilePath; //Has file path info picBoxes[index].Click += new System.EventHandler(ClickHandler); ... ... // Signature of ClickHandler method is as follows public void ClickHandler(Object sender, System.EventArgs e) { // I can get the handle to the PictureBox which was clicked by the user // as follows PictureBox pbx = new PictureBox(); pbx = (PictureBox) ((System.Windows.Forms.PictureBox)sender); // How do I access FilePath ? }
ThanksHello, I think what you want to do is only possible with an Inherited class and the usage of Custom Eventargs. 1) Inherit a class from PictureBox
public class SpecialPictureBox : System.Windows.Forms.PictureBox
- Add a string property FilePath
private string _filepath = "";
[System.ComponentModel.DefaultValue("")]
public string FilePath
{
get
{
return _filepath;
}
set
{
_filepath = value;
}
}3)Inherit a class from System.EventArgs with a string member
public class SpecialEventArgs : System.EventArgs
{
public string filepath;public SpecialEventArgs(string \_filepath) { filepath= \_filepath; }
}
4)Create a delegate for your special event in your SpecialPictureBox class, which uses the SpecialEventArgs.
public delegate void SpecialEvent(object sender, SpecialEventArgs specialEA);
5)Create an event which uses the delegate
public event SpecialEvent SpecialChanged;
6)override the OnClick event of your PictureBox and fire the event, with the Filepath property.
if(SpecialChanged!=null)
{
SpecialChanged(this, new SpecialEventArgs(this.FilePath));
}7)In your Form Handle the SpecialChanged event and set the FilePath property
yourSpecialPictureBox.FilePath = "yourpath";
yourSpecialPictureBox.SpecialChanged+= new YourNamespace.SpecialEvent(yourSpecialPictureBox_SpecialChanged);private void yourSpecialPictureBox_SpecialChanged(object sender, SpecialEventArgs specialEA)
{
//specialEA.filepath
}Hope it helps! -- modified at 5:35 Friday 27th July, 2007
All the best, Martin
-
Hello, I think what you want to do is only possible with an Inherited class and the usage of Custom Eventargs. 1) Inherit a class from PictureBox
public class SpecialPictureBox : System.Windows.Forms.PictureBox
- Add a string property FilePath
private string _filepath = "";
[System.ComponentModel.DefaultValue("")]
public string FilePath
{
get
{
return _filepath;
}
set
{
_filepath = value;
}
}3)Inherit a class from System.EventArgs with a string member
public class SpecialEventArgs : System.EventArgs
{
public string filepath;public SpecialEventArgs(string \_filepath) { filepath= \_filepath; }
}
4)Create a delegate for your special event in your SpecialPictureBox class, which uses the SpecialEventArgs.
public delegate void SpecialEvent(object sender, SpecialEventArgs specialEA);
5)Create an event which uses the delegate
public event SpecialEvent SpecialChanged;
6)override the OnClick event of your PictureBox and fire the event, with the Filepath property.
if(SpecialChanged!=null)
{
SpecialChanged(this, new SpecialEventArgs(this.FilePath));
}7)In your Form Handle the SpecialChanged event and set the FilePath property
yourSpecialPictureBox.FilePath = "yourpath";
yourSpecialPictureBox.SpecialChanged+= new YourNamespace.SpecialEvent(yourSpecialPictureBox_SpecialChanged);private void yourSpecialPictureBox_SpecialChanged(object sender, SpecialEventArgs specialEA)
{
//specialEA.filepath
}Hope it helps! -- modified at 5:35 Friday 27th July, 2007
All the best, Martin
-
Hello, I think what you want to do is only possible with an Inherited class and the usage of Custom Eventargs. 1) Inherit a class from PictureBox
public class SpecialPictureBox : System.Windows.Forms.PictureBox
- Add a string property FilePath
private string _filepath = "";
[System.ComponentModel.DefaultValue("")]
public string FilePath
{
get
{
return _filepath;
}
set
{
_filepath = value;
}
}3)Inherit a class from System.EventArgs with a string member
public class SpecialEventArgs : System.EventArgs
{
public string filepath;public SpecialEventArgs(string \_filepath) { filepath= \_filepath; }
}
4)Create a delegate for your special event in your SpecialPictureBox class, which uses the SpecialEventArgs.
public delegate void SpecialEvent(object sender, SpecialEventArgs specialEA);
5)Create an event which uses the delegate
public event SpecialEvent SpecialChanged;
6)override the OnClick event of your PictureBox and fire the event, with the Filepath property.
if(SpecialChanged!=null)
{
SpecialChanged(this, new SpecialEventArgs(this.FilePath));
}7)In your Form Handle the SpecialChanged event and set the FilePath property
yourSpecialPictureBox.FilePath = "yourpath";
yourSpecialPictureBox.SpecialChanged+= new YourNamespace.SpecialEvent(yourSpecialPictureBox_SpecialChanged);private void yourSpecialPictureBox_SpecialChanged(object sender, SpecialEventArgs specialEA)
{
//specialEA.filepath
}Hope it helps! -- modified at 5:35 Friday 27th July, 2007
All the best, Martin
In .NET Framework 2.0, if you're following the
EventArgs
pattern, you don't need to declare your own delegate, you can use the genericEventHandler<T>
delegate. Saves time and metadata space. So you'd havepublic event EventHandler<SpecialEventArgs> SpecialChanged;
yourSpecialPictureBox.SpecialChanged +=
new EventHandler<SpecialEventArgs>(yourSpecialPictureBox_SpecialChanged);I'm finding this pretty handy.
Stability. What an interesting concept. -- Chris Maunder
-
In .NET Framework 2.0, if you're following the
EventArgs
pattern, you don't need to declare your own delegate, you can use the genericEventHandler<T>
delegate. Saves time and metadata space. So you'd havepublic event EventHandler<SpecialEventArgs> SpecialChanged;
yourSpecialPictureBox.SpecialChanged +=
new EventHandler<SpecialEventArgs>(yourSpecialPictureBox_SpecialChanged);I'm finding this pretty handy.
Stability. What an interesting concept. -- Chris Maunder
-
Hello, I think what you want to do is only possible with an Inherited class and the usage of Custom Eventargs. 1) Inherit a class from PictureBox
public class SpecialPictureBox : System.Windows.Forms.PictureBox
- Add a string property FilePath
private string _filepath = "";
[System.ComponentModel.DefaultValue("")]
public string FilePath
{
get
{
return _filepath;
}
set
{
_filepath = value;
}
}3)Inherit a class from System.EventArgs with a string member
public class SpecialEventArgs : System.EventArgs
{
public string filepath;public SpecialEventArgs(string \_filepath) { filepath= \_filepath; }
}
4)Create a delegate for your special event in your SpecialPictureBox class, which uses the SpecialEventArgs.
public delegate void SpecialEvent(object sender, SpecialEventArgs specialEA);
5)Create an event which uses the delegate
public event SpecialEvent SpecialChanged;
6)override the OnClick event of your PictureBox and fire the event, with the Filepath property.
if(SpecialChanged!=null)
{
SpecialChanged(this, new SpecialEventArgs(this.FilePath));
}7)In your Form Handle the SpecialChanged event and set the FilePath property
yourSpecialPictureBox.FilePath = "yourpath";
yourSpecialPictureBox.SpecialChanged+= new YourNamespace.SpecialEvent(yourSpecialPictureBox_SpecialChanged);private void yourSpecialPictureBox_SpecialChanged(object sender, SpecialEventArgs specialEA)
{
//specialEA.filepath
}Hope it helps! -- modified at 5:35 Friday 27th July, 2007
All the best, Martin