How can I share control-specific event handlers project-wide?

I can code an event to limit textbox entries to decimal values like so:

private void txtbxPlatypus_KeyPress(object sender, KeyPressEventArgs args)
{
    const int BACKSPACE = 8;
    const int DECIMAL_POINT = 46;
    const int ZERO = 48;
    const int NINE = 57;
    const int NOT_FOUND = -1;

    int keyvalue = (int)args.KeyChar; // not really necessary to cast to int

    if ((keyvalue == BACKSPACE) || ((keyvalue >= ZERO) && (keyvalue <= NINE))) return;
    // Allow the first (but only the first) decimal point
    if ((keyvalue == DECIMAL_POINT) && ((sender as TextBox).Text.IndexOf(".") == NOT_FOUND)) return;
    // Allow nothing else
    args.Handled = true;
}

...and then have other TextBoxes on the page with the same entry-filtering requirement attach to that event handler:

txtbxSeat.KeyPress += txtbxPlatypus_KeyPress;
txtbxLunch.KeyPress += txtbxPlatypus_KeyPress;

However, what if I want to share such a handler project-wide, rather than have to reproduce on it on each page that has TextBoxes whose input needs to be restricted in this way?

Is there a way to set up "global" (project-wide) event handler delegates that can be used from any form in the project?

UPDATE

This does work:

public static class SandBoxUtils
{
    public static void DecimalsOnlyKeyPress(object sender, KeyPressEventArgs args)
    {
        // same code as above in txtbxPlatypus_KeyPress()
    }
}

public Form1()
{
    InitializeComponent();
    textBoxBaroqueMelodies.KeyPress += new 
        System.Windows.Forms.KeyPressEventHandler(SandBoxUtils.DecimalsOnlyKeyPress);
}

...but it smells a bit fishy to me. Is this anything "wrong" (or dangerous) about doing this?

Upvotes: 0

Views: 122

Answers (1)

Fabio
Fabio

Reputation: 32443

Create own class derived from System.Windows.Forms.TextBox
Add your event handler for KeyPress event in the constructor
After building project new control must appear in the controls toolbox in Visual Studio,
from there you can just drag it to the form and use...

class TextBoxDecimal : System.Windows.Forms.TextBox
{
    const int BACKSPACE = 8;
    const int DECIMAL_POINT = 46;
    const int ZERO = 48;
    const int NINE = 57;
    const int NOT_FOUND = -1;
    const char DECIMALSEPARATOR = '.';     

    public TextBoxDecimal() :base()
    {
        this.KeyPress += TextBoxDecimal_KeyPress;
    }

    void TextBoxDecimal_KeyPress(object sender, 
                                 System.Windows.Forms.KeyPressEventArgs e)
    {
        int keyvalue = (int)e.KeyChar; // not really necessary to cast to int

        if ((keyvalue == TextBoxDecimal.BACKSPACE) || ((keyvalue >= TextBoxDecimal.ZERO) && (keyvalue <= TextBoxDecimal.NINE))) return;
        // Allow the first (but only the first) decimal point
        if ((keyvalue == TextBoxDecimal.DECIMAL_POINT) && ((sender as TextBoxDecimal).Text.IndexOf(TextBoxDecimal.DECIMALSEPARATOR) == NOT_FOUND)) return;
        // Allow nothing else
        e.Handled = true;
    }
}

Upvotes: 1

Related Questions