I am working on a winforms application. The form I am working on inherits a base form. On the base form the AcceptButton
property is set to cmdOK
(basically the save and exit button). This is required behaviour.
However, the form I am working on has a number of dropdownlist and while the focus is on them I need to change the behaviour of the enter key.
For example if the focus is on cboTags
and Enter is press I need to call cmdAddTag_Click
, if shift + Enter is press I need to call cmdRemoveTag_Click
. I have added the following code:
private void cboTags_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter && e.Modifiers == Keys.Shift)
{
cmdRemoveTag_Click(new object(), new EventArgs());
}
else if (e.KeyCode == Keys.Enter)
{
cmdAddTag_Click(new object(), new EventArgs());
}
}
When I tested it and found the window closed I realised the AcceptButton
property had been set on the inherited form. I cannot find a way round it.
EDIT
If the focus is anywhere else on the form the behaviour should remain as it is. It is only while focus is on certain controls I need to change the behaviour of the Enter key.
this.AcceptButton = null;
(or some variation which you control programatically)
EDIT :
By "control programatically", I mean that when you focus on a region you don't want to have the AcceptButton fire on then you run that code and then when you leave it you set AcceptButton back to what it was.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.