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. Setting default button in a user control

Setting default button in a user control

Scheduled Pinned Locked Moved C#
csharphelpquestionwinformsalgorithms
4 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.
  • U Offline
    U Offline
    User 1172745
    wrote on last edited by
    #1

    Hi all. Making my first "big" C# WinForms app. Basically, I have a WinForm (called MainForm) that has a menu, and a tab control docked to take up the full area. Eventually the number of tabs will be dynamic based on how many "workspaces" a user has open but for now is static. Within the first tab page, I am inserting a custom control -- called TypeAContentControl. There may be several of these on different tab pages (depending if the user called for multiple instances). That control actually contains, you guessed it, yet another control, and also contains a treeview and a panel which will house a future control. The control that is housed within TypeAContentControl is called SearchControl. It's pretty basic, contains a radio set, a text box, and a single button (which is at the heart of my question). I can't seem to figure out how to make this the "default" button. I only need it default while the focus is within other fields of the SearchControl (ie, I want to type in my textbox, or be on my radio buttons and hit Enter to perform an action). The only time I seem to hit the code behind the button is if I tab enough times so that the button has the focus, and then hit spacebar or enter. If I'm on another field within SearchControl, I have to use the keyboard accelerator to hit my button. I know on a standard WinForm with buttons, I can just set that Form's AcceptButton accordingly, but I do not see that as the solution for this problem. Anyone have any clues as to what might help me out here? I've been Googling and searching the MSDN for a while now trying to figure this out and just not finding anything helpful. Any help would be appreciated. Thanks, -John

    U H 2 Replies Last reply
    0
    • U User 1172745

      Hi all. Making my first "big" C# WinForms app. Basically, I have a WinForm (called MainForm) that has a menu, and a tab control docked to take up the full area. Eventually the number of tabs will be dynamic based on how many "workspaces" a user has open but for now is static. Within the first tab page, I am inserting a custom control -- called TypeAContentControl. There may be several of these on different tab pages (depending if the user called for multiple instances). That control actually contains, you guessed it, yet another control, and also contains a treeview and a panel which will house a future control. The control that is housed within TypeAContentControl is called SearchControl. It's pretty basic, contains a radio set, a text box, and a single button (which is at the heart of my question). I can't seem to figure out how to make this the "default" button. I only need it default while the focus is within other fields of the SearchControl (ie, I want to type in my textbox, or be on my radio buttons and hit Enter to perform an action). The only time I seem to hit the code behind the button is if I tab enough times so that the button has the focus, and then hit spacebar or enter. If I'm on another field within SearchControl, I have to use the keyboard accelerator to hit my button. I know on a standard WinForm with buttons, I can just set that Form's AcceptButton accordingly, but I do not see that as the solution for this problem. Anyone have any clues as to what might help me out here? I've been Googling and searching the MSDN for a while now trying to figure this out and just not finding anything helpful. Any help would be appreciated. Thanks, -John

      U Offline
      U Offline
      User 1172745
      wrote on last edited by
      #2

      Ok, clarification of some of the above: "I know on a standard WinForm with buttons, I can just set that Form's AcceptButton accordingly, but I do not see that as the solution for this problem." -- I can't even find an "AcceptButton" property except on my *Form*. It appears that the UserControls don't expose any of the behavior needed to make a default button work (someone please tell me I'm wrong =>). So what I did for now is made a event handler for KeyDown on each of the fields within my SearchControl user control, and any time an Enter is detected, I send the button a PressButton command. This seems to work, however, I would like it to show the button as a default button (extra border basically). Am I going to have to owner draw the button? I'm not opposed to that, it just seems like this is an awful lot of monkeying for something I could do in ATL very easily. Or does someone have the "correct" solution to this whole default button & user control problem? Maybe the solution involves "Man you are totally designing/coding that wrong, go grab this book on WinForm development." :) Thanks, -John

      1 Reply Last reply
      0
      • U User 1172745

        Hi all. Making my first "big" C# WinForms app. Basically, I have a WinForm (called MainForm) that has a menu, and a tab control docked to take up the full area. Eventually the number of tabs will be dynamic based on how many "workspaces" a user has open but for now is static. Within the first tab page, I am inserting a custom control -- called TypeAContentControl. There may be several of these on different tab pages (depending if the user called for multiple instances). That control actually contains, you guessed it, yet another control, and also contains a treeview and a panel which will house a future control. The control that is housed within TypeAContentControl is called SearchControl. It's pretty basic, contains a radio set, a text box, and a single button (which is at the heart of my question). I can't seem to figure out how to make this the "default" button. I only need it default while the focus is within other fields of the SearchControl (ie, I want to type in my textbox, or be on my radio buttons and hit Enter to perform an action). The only time I seem to hit the code behind the button is if I tab enough times so that the button has the focus, and then hit spacebar or enter. If I'm on another field within SearchControl, I have to use the keyboard accelerator to hit my button. I know on a standard WinForm with buttons, I can just set that Form's AcceptButton accordingly, but I do not see that as the solution for this problem. Anyone have any clues as to what might help me out here? I've been Googling and searching the MSDN for a while now trying to figure this out and just not finding anything helpful. Any help would be appreciated. Thanks, -John

        H Offline
        H Offline
        Heath Stewart
        wrote on last edited by
        #3

        There's a couple different things you could do. One would be to define those properties on your UserControl derivative, and then when you add that to a Form (it's going to happen eventually) assign Form.AcceptButton to MyUserControl.AcceptButton. You could even use a designer to do this automatically. Another one - though I'm not sure it'll work - is to extend the Button class and override the CreateParams property like so:

        // Define an alias for easy use since the class and property name cause problems
        using CP = System.Windows.Forms.CreateParams;
        // ...
        const int BS_DEFPUSHBUTTON = 0x01;
        protected override CP CreateParams
        {
        get
        {
        CP cp = base.CreateParams;
        cp.Style |= BS_DEFPUSHBUTTON;
        return cp;
        }
        }

        If you wanted, you could encapsulate this behavior into a property that conditionally sets the BS_DEFPUSHBUTTON window style. The questionable part is whether the Windows Management APIs will really care if this button is not in a Form (a dialog, as it knows it). Good luck!

        Microsoft MVP, Visual C# My Articles

        U 1 Reply Last reply
        0
        • H Heath Stewart

          There's a couple different things you could do. One would be to define those properties on your UserControl derivative, and then when you add that to a Form (it's going to happen eventually) assign Form.AcceptButton to MyUserControl.AcceptButton. You could even use a designer to do this automatically. Another one - though I'm not sure it'll work - is to extend the Button class and override the CreateParams property like so:

          // Define an alias for easy use since the class and property name cause problems
          using CP = System.Windows.Forms.CreateParams;
          // ...
          const int BS_DEFPUSHBUTTON = 0x01;
          protected override CP CreateParams
          {
          get
          {
          CP cp = base.CreateParams;
          cp.Style |= BS_DEFPUSHBUTTON;
          return cp;
          }
          }

          If you wanted, you could encapsulate this behavior into a property that conditionally sets the BS_DEFPUSHBUTTON window style. The questionable part is whether the Windows Management APIs will really care if this button is not in a Form (a dialog, as it knows it). Good luck!

          Microsoft MVP, Visual C# My Articles

          U Offline
          U Offline
          User 1172745
          wrote on last edited by
          #4

          Thank you for the reply. The first solution sounds extremely elegant, but I'll need to make sure on entry of any UserControl that I reset Form.AcceptButton to be the valid default button for whatever control I'm on. Not a big deal but seems like the maintenance of the field could get buggy in complex situations. The second solution seems like it will be what I need to try, and the ability to set the style conditionally, although I can't think of an instance where I'll need to use this now, seems like something I should try to add. Either way, thank you for your suggestions, much better than checking for Enter on each keypress of each field.

          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