Does a UserControl have something similar to the ‘AcceptButton’ property of a Form?
I have a UserControl that contains lots of TextBoxes, ComboBoxes, Buttons, etc.
The UserControl has one specific button that I want to be clicked whenever the user presses 'Enter' on any of the controls contained in the UserControl.
If I were dealing with a Form, I would use its ‘AcceptButton’ property.
What do I do when dealing with a UserControl?
One way is to implement the ‘KeyDown’ event of each control, check if the ‘Enter’ key was pressed, and if so call the ‘Click’ event of that button.
But, is there another solution?
Re: Does a UserControl have something similar to the ‘AcceptButton’ property of a Form?
I am working on a Windows Form application written in C-Sharp.
Should have mentioned that before. Sorry.
(By the way, wasn’t my first post clear? I said there that if I were dealing with a Form, I would use its ‘AcceptButton’ property. But, as it is, I am dealing with a UserControl that doesn’t have this property. Therefore, I was wondering whether a UserControl has something similar. Just like I say in the titile: Does a UserControl have something similar to the ‘AcceptButton’ property of a Form?)
Re: Does a UserControl have something similar to the ‘AcceptButton’ property of a Form?
I tried doing as you suggested, but it doesn't seem to work.
This is what I did:
- I've created a MyButton class derived from the Button class, that implements the IsDefault property. (Just as you wrote).
- I've added an instance of MyButton to my UserControl.
- I've set the IsDefault property to true.
- I've implemented the Click event.
When the application is activated, it looks as if this button has focus, but clicking on the 'Enter' key does not fire the Click event.
Did I do something wrong?
Re: Does a UserControl have something similar to the ‘AcceptButton’ property of a Form?
There is actually several methods, luckily not so complicated
The easiest method to use is to use the UserControl's TopLevelControl property and determine if the usercontrol is placed on a Windows Form. Then, if it is placed on a Form, that means that the UserControl's TopLevelControl needs to be cast into a Form, then, we can utilise the AcceptButton property.
I'm attaching a sample here for you.
On the sample, I've placed 2 buttons on the usercontrol, one will be used as the AcceptButton.
In the UserControl's Load event, I did this :
Code:
private void UserControl1_Load(object sender, System.EventArgs e)
{
if (this.TopLevelControl is Form)
{
((Form)this.TopLevelControl).AcceptButton = MyAcceptButton;
}
}
Then, the button I want to use as the AcceptButton, I did this in it's Click event :
Re: Does a UserControl have something similar to the ‘AcceptButton’ property of a Form?
I tried what you suggested, and it works. Thanks!
The only thing is that I have a few UserControls on my main form, and I would like the specific button to act as an ‘AcceptButton’ only when the UserControl that owns this button, is active. (Meaning, that if another UserContol is active, and the user clicks ‘Enter’, the ‘Click’ event of the button won’t be fired).
Is the way to do so is to implement the ‘Enter’ & ‘Leave’ events of the UserControl?
(In the ‘Enter’ event set the ‘AcceptButton’ property to this button, and in the ‘Leave’ event set the ‘AcceptButton’ to null)
Re: Does a UserControl have something similar to the ‘AcceptButton’ property of a Form?
I don’t understand how what you wrote helps me.
Can you explain, please?
But, first let me try to explain my problem again. (Hopefully, more clearly …)
Let’s say I have 3 UserControls on my form: MyUserCtrl, UserCtrl2, & UserCtrl3. All of them contain buttons, text-boxes, etc.
When MyUserCtrl is in focus, I would like pressing ‘Enter’ to fire the ‘Click’ event of a specific button on this UserControl.
If MyUserCtrl is not in focus, pressing ‘Enter’ should not do so.
Re: Does a UserControl have something similar to the ‘AcceptButton’ property of a Form?
At the beginning, when I click ‘Enter’ the 'MyAcceptButton_Click' event is fired, and I see the message box. (So far so good).
But, then the message box is never shown again because of the ‘if sentence’.
I took your sample, and added another UserControl called DummyUserControl to it.
This is what I want to happen:
- When ‘AcceptButUC’ is active, clicking ‘Enter’ fires ‘MyAcceptButton_Click’ and displays the message box.
- When ‘DummyUserControl’ is active, clicking ‘Enter’ does not fire ‘MyAcceptButton_Click’
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.