WinForms AcceptButton Extender

By Fons Sonnemans, 1-12-2002

Download AcceptButton.zip

Introduction

The System.Windows.Forms.Form class has an AcceptButton property which can be used to set the button on the form that is clicked when the user presses the ENTER key. The accept (default) button should be the button that represents the action that the user is most likely to perform if that action isn't potentially dangerous. This button has a dark border to let the user know that it is the accept button.

This feature works great only when you have one accept button. Have a look at the following diaglog.

The OK button is in this dialog the accept button. This is the correct behavior when the textbox 'Name' and datepicker 'Date' have the focus. There should not be an accept button when the (multiline) textbox 'Description' has the focus. When listbox 'Avialiable' has the focus the '> Add >' button must be the accept button. And when listbox 'Assigned' has the focus the '> Add >' button must be the accept button.

This can be accomplished by implementing the following Enter and Leave event handlers.


private void textBoxDescription_Enter ( object sender , System.EventArgs e) {
    this.AcceptButton =null;
}

privatevoid textBoxDescription_Leave(objectsender, System.EventArgs e) {
    this.AcceptButton =buttonOK;
}

privatevoid listBoxAvailable_Enter(objectsender, System.EventArgs e) {
    this.AcceptButton =buttonAdd;
}

privatevoid listBoxAvailable_Leave(objectsender, System.EventArgs e) {
    this.AcceptButton =buttonOK;
}

privatevoid listBoxAssigned_Enter(objectsender, System.EventArgs e) {
    this.AcceptButton =buttonRemove;
}

privatevoid listBoxAssigned_Leave(objectsender, System.EventArgs e) {
    this.AcceptButton =buttonOK;
}

But why write code when the same can be accomplished by setting some properties in the designer. The AcceptButton extender class can be used to eliminate this code.

AcceptButton Extender Provider

An extender provider is a component that provides properties to other components. The AcceptButton class implements the IExtenderProvider interface making it an Extender Provider. When you add an AcceptButton control to a Form, all other controls (accept buttons) on the form have the UseDefaultAcceptButton and the AcceptButton property added to their list of properties.

You now only have to set the two properties for each control:

Control UseDefaultAcceptButton property AcceptButton property
textBoxName True (none)
textBoxDescription False (none)
dateTimePickerDate True (none)
listBoxAvailable False buttonAdd
listBoxAssigned False buttonRemove

Conclusion

The AcceptButton class is good example for which you can use Extender Providers. Extender Providers are somewhat strange to write but very powerful. I use them a lot.

Any suggestions and feedback for improving this article is most welcome. Send your suggestions and feedback to Fons.Sonnemans@reflectionit.nl

Leave a Comment

Leave a Comment
Name
Comment
2 + 4 =

0 Comments

All postings/content on this blog are provided "AS IS" with no warranties, and confer no rights. All entries in this blog are my opinion and don't necessarily reflect the opinion of my employer or sponsors. The content on this site is licensed under a Creative Commons Attribution By license.