0

I can't seem to have this part of my code to work. The objective for this piece of code is to add a numbered list every time The user presses, "ENTER". Here is and example of what I mean.

0)10100[User presses the ENTER key]

1)(cursor is here)

Here is the code I have. meowbox is a multiline text box.

protected override void OnKeyDown(KeyEventArgs e)
{

    if (e.KeyCode == Keys.Enter)
    {

        meowbox.Text += i + ")";
        ++i;
    }

    base.OnKeyDown(e);
}
Franco Pettigrosso
  • 4,056
  • 2
  • 20
  • 32
  • Try meowbox.Text += "\n" + i.ToString() + ")"; The \n creates a new line before the i.ToString() method. – Ron Beyer Apr 18 '15 at 14:33
  • No good, the extra line is a typo. For some reason it wasn't formatting it right when I was asking the question. The real problem is that the i+")" won't show up in the text box. – Franco Pettigrosso Apr 18 '15 at 14:40
  • What is the issue that you are having? Is anything happening when you press enter? – RussDunn Apr 18 '15 at 14:59
  • Yes, the problem is that I cannot get nothing to happen when I press enter. – Franco Pettigrosso Apr 18 '15 at 15:13
  • it looks like you're not in the KeyDown event for the text box (no `object sender` argument) – Rufus L Apr 18 '15 at 15:14
  • @FrancoPettigrosso I added a paragraph on how to hook up the event through the designer, that might help. – Rufus L Apr 18 '15 at 15:25
  • My crystal ball says that your override applies to the *form's* OnKeyDown() method, not the textbox's. As written, it can only work when you derive your own class from TextBox. Which is quite valid btw. But you probably just want to use the textbox' KeyDown event instead. You also need to think about what's going to happen when the user presses Ctrl+V to paste text. – Hans Passant Apr 18 '15 at 15:25
  • if I use the 'object sender' arg do I have to put in an event handler on the text box then? – Franco Pettigrosso Apr 18 '15 at 15:28
  • @RufusL I am not using the designer – Franco Pettigrosso Apr 18 '15 at 15:36
  • Well you can hook it up through code as well, 'textbox1.KeyDown += someMethod'. The signature has to be: 'void someName(object, KeyEventArgs)' – Rufus L Apr 18 '15 at 15:51

2 Answers2

1

It looks like you may not be putting your code in the correct method. One easy way to do it is to go to your form designer, select the textbox, go to the Properties window, click the lightning-bolt icon (for methods), and then double-click the KeyDown method. This will create an event handler and hook it up to the text box.

Another problem you may have (once you get it hooked up correctly) is that the textbox continues to process the key press even though you are handling it yourself. To get around this, you can set SuppressKeyPress to true.

For example:

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        textBox1.Text += Environment.NewLine + i++ + ") ";
        textBox1.SelectionStart = textBox1.Text.Length;
        e.SuppressKeyPress = true;
    }
}
Rufus L
  • 36,127
  • 5
  • 30
  • 43
0

I got it! thank you guys! what i Had to do was meowbox.KeyDown += new KeyEventHandler. If you were trying to say that to me, thanks!

Franco Pettigrosso
  • 4,056
  • 2
  • 20
  • 32