How to override Button Click EventArgs or use another ways to achieve it?
-
How to override Button Click EventArgs or use another ways to achieve it? in the button_Click,there is only two parameters(object sender and EventArgs e), now I want to override the Button_Click add a bool parameter, if the parameter is true then execute this click event, otherwise doesn't execute this click. It can not be overrided the button1_Click by adding a parameter directly, It seems to be feasible by override the EventArgs, But I am a novice, and don't know how to achieve this function, if give me detail code,I'll very appreciate your assitance. Thanks in advance!
-
How to override Button Click EventArgs or use another ways to achieve it? in the button_Click,there is only two parameters(object sender and EventArgs e), now I want to override the Button_Click add a bool parameter, if the parameter is true then execute this click event, otherwise doesn't execute this click. It can not be overrided the button1_Click by adding a parameter directly, It seems to be feasible by override the EventArgs, But I am a novice, and don't know how to achieve this function, if give me detail code,I'll very appreciate your assitance. Thanks in advance!
You must be setting the bool parameter somewhere, right? Won't this be solving your purpose?
private void button1\_Click(object sender, EventArgs e) { if (SomeBoolean) { // // Code here // } }
Here SomeBoolean is a class level variable.
The word "politics" describes the process so well: "Poli" in Latin meaning "many" and "tics" meaning "bloodsucking creatures." जय हिंद
-
You must be setting the bool parameter somewhere, right? Won't this be solving your purpose?
private void button1\_Click(object sender, EventArgs e) { if (SomeBoolean) { // // Code here // } }
Here SomeBoolean is a class level variable.
The word "politics" describes the process so well: "Poli" in Latin meaning "many" and "tics" meaning "bloodsucking creatures." जय हिंद
It maybe can not be solved by this means. my purpose as below: I want to override the button Click event,after override the click event it contains below code default,when I use the button_click,I have to pass a boolean value for it. if (BlnFlag) { MessageBox.Show(""); } else { return ; //do nothing } the BlnFlag is passed from WinForm which used this kind Button,it is not pass from the Button Class. Could you give some suggestion?
-
It maybe can not be solved by this means. my purpose as below: I want to override the button Click event,after override the click event it contains below code default,when I use the button_click,I have to pass a boolean value for it. if (BlnFlag) { MessageBox.Show(""); } else { return ; //do nothing } the BlnFlag is passed from WinForm which used this kind Button,it is not pass from the Button Class. Could you give some suggestion?
Instead of asking about how you think that it should be solved I think that you should ask about what it is that you are trying to accomplish. The event handler is normally called by an event. As you are trying to call it directly suggests that you are using the wrong tools for your solution.
Despite everything, the person most likely to be fooling you next is yourself.
-
How to override Button Click EventArgs or use another ways to achieve it? in the button_Click,there is only two parameters(object sender and EventArgs e), now I want to override the Button_Click add a bool parameter, if the parameter is true then execute this click event, otherwise doesn't execute this click. It can not be overrided the button1_Click by adding a parameter directly, It seems to be feasible by override the EventArgs, But I am a novice, and don't know how to achieve this function, if give me detail code,I'll very appreciate your assitance. Thanks in advance!
You can do something like this and use the MyButton in place of the in built one wherever you need this functionality:
public class MyButton : Button
{
public new event EventHandler<ButtonClickEventArgs> Click;public new void PerformClick() { PerformClick(false); } public void PerformClick(bool value) { OnClick(new ButtonClickEventArgs(value)); } protected override void OnClick(EventArgs e) { OnClick(new ButtonClickEventArgs(false)); } protected virtual void OnClick(ButtonClickEventArgs e) { EventHandler<ButtonClickEventArgs> eh = Click; if (eh != null) eh(this, e); }
}
public class ButtonClickEventArgs : EventArgs
{
private bool m_Value;public ButtonClickEventArgs(bool value) { m\_Value = value; } public bool Value { get { return m\_Value; } }
}
Edited to get generic < > to display!
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)modified on Wednesday, February 18, 2009 7:18 AM
-
It maybe can not be solved by this means. my purpose as below: I want to override the button Click event,after override the click event it contains below code default,when I use the button_click,I have to pass a boolean value for it. if (BlnFlag) { MessageBox.Show(""); } else { return ; //do nothing } the BlnFlag is passed from WinForm which used this kind Button,it is not pass from the Button Class. Could you give some suggestion?
mctramp168 wrote:
the BlnFlag is passed from WinForm which used this kind Button,it is not pass from the Button Class
Cant get this. The button must be on a winform. On the same winform, you will be setting the flag which will decide if the code should be executed or not. I dont see any need to create your own button. You can use the code I had posted. If the boolean is set on some other form, and the button is on a different form, then there are a lot of ways to pass it on. If you can post what exactly are you trying to do then maybe someone can help better.
The word "politics" describes the process so well: "Poli" in Latin meaning "many" and "tics" meaning "bloodsucking creatures." जय हिंद
-
It maybe can not be solved by this means. my purpose as below: I want to override the button Click event,after override the click event it contains below code default,when I use the button_click,I have to pass a boolean value for it. if (BlnFlag) { MessageBox.Show(""); } else { return ; //do nothing } the BlnFlag is passed from WinForm which used this kind Button,it is not pass from the Button Class. Could you give some suggestion?
Hi, if you want to make a button do nothing, then disable it by setting its Enabled property false; that will alter its appearance so the user knows, and it will prevent some events from firing. if you want a button to do alternate things, then I suggest you change its Text property so the user knows what to expect; your clicked handler can check the Text to see what needs to be done (beware internationization though). if you don't want the appearance changed (I strongly object to invisible GUI changes mind you) you can use a flag somewhere and test it in the handler; one easy place to store such a flag would be in the Tag property: each Control has a Tag property (type is Object) that is available for any purpose you choose. I have never done that for disabling buttons, and I never will! :)
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
modified on Sunday, June 12, 2011 8:16 AM
-
mctramp168 wrote:
the BlnFlag is passed from WinForm which used this kind Button,it is not pass from the Button Class
Cant get this. The button must be on a winform. On the same winform, you will be setting the flag which will decide if the code should be executed or not. I dont see any need to create your own button. You can use the code I had posted. If the boolean is set on some other form, and the button is on a different form, then there are a lot of ways to pass it on. If you can post what exactly are you trying to do then maybe someone can help better.
The word "politics" describes the process so well: "Poli" in Latin meaning "many" and "tics" meaning "bloodsucking creatures." जय हिंद
thanks, I know the method you suggested, because many winform will use this kind of button, if I create my own button, then I can write my code code in this event, no need write the code under the others winform click event.so I have to pass a parameter from the current winform to this event decide to how to run it. can you give some comments on it?
-
You can do something like this and use the MyButton in place of the in built one wherever you need this functionality:
public class MyButton : Button
{
public new event EventHandler<ButtonClickEventArgs> Click;public new void PerformClick() { PerformClick(false); } public void PerformClick(bool value) { OnClick(new ButtonClickEventArgs(value)); } protected override void OnClick(EventArgs e) { OnClick(new ButtonClickEventArgs(false)); } protected virtual void OnClick(ButtonClickEventArgs e) { EventHandler<ButtonClickEventArgs> eh = Click; if (eh != null) eh(this, e); }
}
public class ButtonClickEventArgs : EventArgs
{
private bool m_Value;public ButtonClickEventArgs(bool value) { m\_Value = value; } public bool Value { get { return m\_Value; } }
}
Edited to get generic < > to display!
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)modified on Wednesday, February 18, 2009 7:18 AM
Thank Dave , Do as you say ,the issues is solved.