Same behavior for all controls

  • Thread starter Thread starter chook.harel
  • Start date Start date
C

chook.harel

Hi, I want all my text boxes in a certain form to act the same way
for a special key combination (F1 in my case)
but each of them should react diffrently to other keys.
Now, of caurse i can do this by simply add the KeyDown to all of them
but is there something above that?
That handles the key hooks of all the controls in the form?
 
This is where the OOD should help you. You can create a base textbox class by
inheriting from the System.Windows.Forms.TextBox, override the OnKeyDown
method and implement the code that is the same for all textboxes. Then create
all other texboxes by deriving from your base class and provide the OnKeyDown
implementation in each of them. Don't forget to call base.OnKeyDown though.
 
I thought about that,
but what if I want ALL the controls to have the same onKeyDown?

Isn't that starting to complicate things?
(textboxes, datagrids, comboboxes.... everything.)
 
I'd recommend to use composition rather than (implementation)
inheritance.

i.e. factory methods:

static TextBox NewTextBox () {
TextBox tb = new TextBox();
// add your handlers, set your own "default" property values
return tb;
}

or a "decorator" function:

static Control Decorate (Control c) {
// add your handlers, set your own "default" property values
return c;
}
 
Back
Top