0

I'd like to call a KeyEvent from within an Event. Something like...

private void txtPostcode_TextChanged(object sender, EventArgs e)
    {
        TxtboxNumbersOnly(sender, e);
    }

Where TxtboxNumbersOnly is a KeyEvent. However it isn't allowed. I've also tried casting but it is giving an error. What would be the correct way of doing this?


Thanks, Grant. A snippet follows..

private void TxtboxNumbersOnly(object sender, KeyEventArgs e)
{
    //Allow arrow keys
    switch (e.KeyCode)
    {
        case Keys.Up:
        case Keys.Down:
        case Keys.Left:
        case Keys.Right:
        case Keys.PageUp:
        case Keys.PageDown:
        case Keys.Delete:
            e.SuppressKeyPress = false;
            return;

The error is:

Argument2:cannot convert from 'System.EventArgs' to 'System.Windows.Forms.KeyEventArgs'.

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
ionside
  • 7
  • 5
  • Thanks, Grant. A snippet follows.. `private void TxtboxNumbersOnly(object sender, KeyEventArgs e) { //Allow arrow keys switch (e.KeyCode) { case Keys.Up: case Keys.Down: case Keys.Left: case Keys.Right: case Keys.PageUp: case Keys.PageDown: case Keys.Delete: e.SuppressKeyPress = false; return; ` etc. The error is Argument2:cannot convert from 'System.EventArgs' to 'System.Windows.Forms.KeyEventArgs'. – ionside Nov 07 '15 at 02:40

1 Answers1

1

It appears that you're handling the wrong event in the first place.

You can't just go and call TxtboxNumbersOnly because you don't have the KeyEventArgs to send it. And you can't convert EventArgs as, in this case, they are two different types.

You should be handling the TextBox.KeyDown event. The code should look like this:

    private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        switch (e.KeyCode)
        {
            case Keys.Up:
            case Keys.Down:
            case Keys.Left:
            case Keys.Right:
            case Keys.PageUp:
            case Keys.PageDown:
            case Keys.Delete:
                e.SuppressKeyPress = false;
                return;
        }
    }

Having said that, I don't see what this code is doing...

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • I think I'm beginning to see the error in my ways. The full event code is – ionside Nov 07 '15 at 03:24
  • This is the right answer. The only way to get KeyEventArgs in the first place. – ipavlu Nov 07 '15 at 03:34
  • I'm showing my raw Overflow talents here (lack of). Unable to share the full code due to comment character limits. The full code prevents the user from typing non-number keys into the text box. I'm beginning to see the error in my ways, thank you. But still getting foiled by the code that gets added in Main.Designer.cs – ionside Nov 07 '15 at 03:39
  • 1
    @ionside - You need to just attach the `KeyDown` event from the text box properties.. If you do it by double-clicking the text box then you only get the `TextChanged` event. – Enigmativity Nov 07 '15 at 04:52
  • @ionside - Perhaps you should look at this answer too: http://stackoverflow.com/a/463335/259769. – Enigmativity Nov 07 '15 at 04:54