-4

I am using textbox and here's my problem...

I already have a handler where program will not allow anymore inputs when defined Maximum text length is reached. Here's the code:

private void txtText_KeyPress(object sender, KeyPressEventArgs e)
{
    if (txtText.Text.Length > MaxLengthAllowed - 1 && e.KeyChar != 8)
    {
        e.Handled = true;
        Console.Beep(2000, 90);
        return;
    }
}

Now my problem is when I press and HOLD a key (for example the letter A), it exceeds the "MaxLengthAllowed" then beeps.

Is this a fault or something like a limitation of the textbox? Or am I missing something?

This code is working fine when you are not HOLDING a key. Hence, that's my problem.

Jack Frost
  • 318
  • 4
  • 13

3 Answers3

1

Just set the MaxLength property of the textbox. It's as simple as

textboxVar.MaxLength = maxLengthAllowed;

Hope that helps.

You could always subscribe a PropertyChanged event, which you can rig to fire whenever the Text property of the textbox changes. Have a read of how to raise an event on Property Change

Community
  • 1
  • 1
Daniel Lane
  • 2,575
  • 2
  • 18
  • 33
  • Thank you for this but I'm acctually using a multiline textbox and I cant do that. I just made my question like that to make it simpler. here's my original post way back: http://stackoverflow.com/questions/17079046/is-there-a-way-to-catch-maximum-length-per-line-and-not-allow-user-to-input-more – Jack Frost Jul 24 '13 at 09:11
  • @JackFrost after reading your original post i can say that your this question conveys entirely different meaning – Ehsan Jul 24 '13 at 09:19
  • You could say that. That's why I made a new post. Coz here, my problem is Text length is exceeding MaxLengthAllowed when a user HOLDS a key... – Jack Frost Jul 24 '13 at 09:27
  • @JackFrost if you set the above property it won't – Ehsan Jul 24 '13 at 09:47
0

You could use the KeyUp event instead. That only fires once when you release the key. KeyPress will be fired multiple times for as long as you hold down the key.

FreddieH
  • 773
  • 8
  • 18
  • You say "That only fires once when you release the key", but my code can ALMOST handle it? for example, MaxLengthAllowed = 15; now if i HOLD a key like in my post, text length exceeds to 17 and sometimes 19 ONLY and not to infinity. – Jack Frost Jul 24 '13 at 09:25
  • As far as i can see, your code will run indefinitely until you release the key. It will continue to beep as long as you text length will be equal or greater then the max length allowed. – FreddieH Jul 24 '13 at 09:53
0

The TextBox.Text is not updated when TextBox.KeyPress is fired. For example, if the Text before KeyPress is A, When user types B, the Text is still A (not AB) in the KeyPress event handler. So you have to define your MaxLengthAllowed to be 1 less than the desired value. The code should look like this:

private void txtText_KeyPress(object sender, KeyPressEventArgs e)
{
  if ((txtText.Text.Length > MaxLengthAllowed - 1) && e.KeyChar != 8)
  {
    e.Handled = true;
    Console.Beep(2000, 90);      
  }
}

I think you should use TextBox.MaxLength property instead, it also supports Beep, the Beep sound is even nicer than the Beep generated by Console.Beep().

King King
  • 61,710
  • 16
  • 105
  • 130