'Override TextChanged event for a custom control not working

I tried this code in my custom user control in a C# Windows application:

public partial class HtextBox : DevExpress.XtraEditors.TextEdit
{
    protected override void OnTextChanged(KeyEventArgs kpe)
    {
        if (kpe.KeyCode == Keys.D1 ||
            kpe.KeyCode == Keys.D2 ||
            kpe.KeyCode == Keys.D3 ||
            kpe.KeyCode == Keys.D4 ||
            kpe.KeyCode == Keys.D5 ||
            kpe.KeyCode == Keys.D6 ||
            kpe.KeyCode == Keys.D7 ||
            kpe.KeyCode == Keys.D8 ||
            kpe.KeyCode == Keys.D9 ||
            kpe.KeyCode == Keys.D0
            ) {
                base.Text += kpe.KeyValue;
        }
    }
}

I got this error:

Error 1 'myproject.HtextBox.OnTextChanged(object, System.Windows.Forms.KeyEventArgs)': no suitable method found to override E:\my project\myproject\HtextBox.cs

I want create a custom textbox. Here I want this textbox just to get numbers as input. What would an example be?



Solution 1:[1]

Even though KeyEventArgs is a subclass of System.EventArgs, C# does not let you override a method with a subclass parameter. You need to override the method with the signature from the base class, and then cast to KeyEventArgs inside your function:

protected override void OnTextChanged(System.EventArgs args) {
    KeyEventArgs kpe = (KeyEventArgs)args;
    ...
}

Edit: Since OnTextChanged does not provide KeyEventArgs and it looks like you need them, try overriding a different method:

protected override void OnKeyDown(KeyEventArgs kpe) {
    ...
}

Solution 2:[2]

It looks like you forgot the first parameter in your event handler.

Change it to this:

protected override void OnTextChanged(Object sender, KeyEventArgs kpe) 
    {
        if (kpe.KeyCode == Keys.D1 ||
            kpe.KeyCode == Keys.D2 ||
            kpe.KeyCode == Keys.D3 ||
            kpe.KeyCode == Keys.D4 ||
            kpe.KeyCode == Keys.D5 ||
            kpe.KeyCode == Keys.D6 ||
            kpe.KeyCode == Keys.D7 ||
            kpe.KeyCode == Keys.D8 ||
            kpe.KeyCode == Keys.D9 ||
            kpe.KeyCode == Keys.D0
            ) {
                base.Text += kpe.KeyValue;
        }
    }

Solution 3:[3]

@dasblinkenlight has given the correct answer to your question.

However, method Form.OnTextChanged is an event raising method, and should hardly ever be overridden. You might want to create an event handler, possibly in your derived class constructor:

this.TextChanged += new EventHandler(OnTextChanged);

BTW, in Visual Studio, pressing the tab key once you have typed += would generate an event handler for you.

Solution 4:[4]

Simple code for input number only and support Delete and BackSpace keys.

protected override void OnKeyPress(KeyPressEventArgs e)
{
    if (numberMode)
    {
        if (Char.IsDigit(e.KeyChar) || e.KeyChar == (char)Keys.Back || e.KeyChar == (char)Keys.Delete) { }
        else
        {
            e.Handled = true;
        }
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 orandov
Solution 3 Peter Mortensen
Solution 4