Persistent Button
-
Hi, How do u stop a button that has been pressed from firing again when the enter/return key is pressed? (and the answer does NOT seem to be: Put "none" in the form1 "AcceptButton" property ... cuz once the button has been pressed with the mouse, it seems to automatically become the default button that is fired when the enter key is pressed the next time) Thanks for your time. IceWater42
-
Hi, How do u stop a button that has been pressed from firing again when the enter/return key is pressed? (and the answer does NOT seem to be: Put "none" in the form1 "AcceptButton" property ... cuz once the button has been pressed with the mouse, it seems to automatically become the default button that is fired when the enter key is pressed the next time) Thanks for your time. IceWater42
The main problem you encounter is that while focused the control processes key messages and hadles them (ie - you press space or enter the button is pressed). In order to bypass this you have to inherit the
Button
class and override it'sProcessCmdKey
method. This will allow you to skip some keys you don't want to be processed. Here's an example :namespace MyCustomControls
{
public class SpecialButton : Button
{
ArrayList skipTheseKeys;public SpecialButton() { skipTheseKeys = new ArrayList(); skipTheseKeys.Add(Keys.Space); // ignore space for pressing skipTheseKeys.Add(Keys.Tab); // ignore tab forlosing focus } // override the ProcessCmdKey and handle specific key's yourself protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { if (skipTheseKeys.Contains(keyData)) return true; // we have succsesfully processed this key else return base.ProcessCmdKey(ref msg, keyData); // let the buttonclass handle this key } }
}
protected internal static readonly ... and I wish the list could continue ...
-
The main problem you encounter is that while focused the control processes key messages and hadles them (ie - you press space or enter the button is pressed). In order to bypass this you have to inherit the
Button
class and override it'sProcessCmdKey
method. This will allow you to skip some keys you don't want to be processed. Here's an example :namespace MyCustomControls
{
public class SpecialButton : Button
{
ArrayList skipTheseKeys;public SpecialButton() { skipTheseKeys = new ArrayList(); skipTheseKeys.Add(Keys.Space); // ignore space for pressing skipTheseKeys.Add(Keys.Tab); // ignore tab forlosing focus } // override the ProcessCmdKey and handle specific key's yourself protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { if (skipTheseKeys.Contains(keyData)) return true; // we have succsesfully processed this key else return base.ProcessCmdKey(ref msg, keyData); // let the buttonclass handle this key } }
}
protected internal static readonly ... and I wish the list could continue ...
AHA .. i get the idea ... thank you I added the code in front of my existing namespace but i do not know how to get the compiler to cause the override to be activated. I put a break on the IF statement but it never gets executed, so the problem still exists :(
-
AHA .. i get the idea ... thank you I added the code in front of my existing namespace but i do not know how to get the compiler to cause the override to be activated. I put a break on the IF statement but it never gets executed, so the problem still exists :(
In order to use the custom control I have described earlier you have to Copy/Paste the code I supplied you (ie - the class) and actualy use the control: when you create a button edit the source code and instead of
System.Windows.Forms.Button
put theNamespace.CustomControlName
(int the declaration and initialization of the variables).protected internal static readonly ... and I wish the list could continue ...
-
In order to use the custom control I have described earlier you have to Copy/Paste the code I supplied you (ie - the class) and actualy use the control: when you create a button edit the source code and instead of
System.Windows.Forms.Button
put theNamespace.CustomControlName
(int the declaration and initialization of the variables).protected internal static readonly ... and I wish the list could continue ...
i assume what u say is right ... but it is like mud to me. the only place i can find "System.Windows.Forms.Button" is in the generated code ... surely you are NOT suggesting i go into the GENERATED code and change that. Overrides are VERY confusing to me :(
-
i assume what u say is right ... but it is like mud to me. the only place i can find "System.Windows.Forms.Button" is in the generated code ... surely you are NOT suggesting i go into the GENERATED code and change that. Overrides are VERY confusing to me :(
I don't think that there is a law preventing the user from modifing the "generated code". If you have Visual Studio 2003 the custom control you have made will not apear into the tool box so easy. The simplest thing is to modify what I told you. On the other hand, if you have Visual Studio 2005 the custom control will apear (or if not, just drag it there) into the tool box. In this case just add the control to your form. If you still don't understand then search CodeProject for articles about custom controls and read about how to use them. By the way, overriding has nothing to do with your problem. You override a procedure, a class is inherited.
protected internal static readonly ... and I wish the list could continue ...
-
I don't think that there is a law preventing the user from modifing the "generated code". If you have Visual Studio 2003 the custom control you have made will not apear into the tool box so easy. The simplest thing is to modify what I told you. On the other hand, if you have Visual Studio 2005 the custom control will apear (or if not, just drag it there) into the tool box. In this case just add the control to your form. If you still don't understand then search CodeProject for articles about custom controls and read about how to use them. By the way, overriding has nothing to do with your problem. You override a procedure, a class is inherited.
protected internal static readonly ... and I wish the list could continue ...
thank you, vlad ... it works just as i had hoped i also found out i was wrong about how the code generator works ... i was under the misbelief that the generated code was regenerated every time that the project was "build"ed ... therefore one should NEVER modify the generated code as that action would have to be remembered and redone every time the program was "build"ed ... apparently, that is not true as i have "rebuild"ed the project several times and the modified code remains as i changed it (as you had advised) thanks again, vlad ... i have learned a lot