Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Persistent Button

Persistent Button

Scheduled Pinned Locked Moved C#
question
7 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    IceWater42
    wrote on last edited by
    #1

    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

    S 1 Reply Last reply
    0
    • I 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

      S Offline
      S Offline
      Stanciu Vlad
      wrote on last edited by
      #2

      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's ProcessCmdKey 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 ...

      I 1 Reply Last reply
      0
      • S Stanciu Vlad

        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's ProcessCmdKey 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 ...

        I Offline
        I Offline
        IceWater42
        wrote on last edited by
        #3

        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 :(

        S 1 Reply Last reply
        0
        • I IceWater42

          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 :(

          S Offline
          S Offline
          Stanciu Vlad
          wrote on last edited by
          #4

          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 the Namespace.CustomControlName (int the declaration and initialization of the variables).

          protected internal static readonly ... and I wish the list could continue ...

          I 1 Reply Last reply
          0
          • S Stanciu Vlad

            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 the Namespace.CustomControlName (int the declaration and initialization of the variables).

            protected internal static readonly ... and I wish the list could continue ...

            I Offline
            I Offline
            IceWater42
            wrote on last edited by
            #5

            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 :(

            S 1 Reply Last reply
            0
            • I IceWater42

              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 :(

              S Offline
              S Offline
              Stanciu Vlad
              wrote on last edited by
              #6

              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 1 Reply Last reply
              0
              • S Stanciu Vlad

                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 Offline
                I Offline
                IceWater42
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups