Setting default button in a user control
-
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
-
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
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
-
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
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 aForm
(it's going to happen eventually) assignForm.AcceptButton
toMyUserControl.AcceptButton
. You could even use a designer to do this automatically. Another one - though I'm not sure it'll work - is to extend theButton
class and override theCreateParams
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 aForm
(a dialog, as it knows it). Good luck!Microsoft MVP, Visual C# My Articles
-
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 aForm
(it's going to happen eventually) assignForm.AcceptButton
toMyUserControl.AcceptButton
. You could even use a designer to do this automatically. Another one - though I'm not sure it'll work - is to extend theButton
class and override theCreateParams
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 aForm
(a dialog, as it knows it). Good luck!Microsoft MVP, Visual C# My Articles
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.