Hello programmers out there :)
Im currently having problems with allowing the "-" in my TextBox.
(Im using Visual Studio 2010)

Im kinda new to programming, thats why this code looks messy.

void Form1::textBox9_KeyPress(System::Object^  sender, System::Windows::Forms::KeyPressEventArgs^  e)
{
	if(!Char::IsDigit(e->KeyChar) && (e->KeyChar != 0x08) && (e->KeyChar != 0x6D))
        e->Handled = true;
}

I tryed that, but this simply doesn't work, it doesn't allows anything.

Thanks for future help :)

(Sorry for my bad english, its not my motherlanguage)

Edited 5 Years Ago by Danny1994: n/a

Are you looking for a specific ordering like 000-111-2222 ? If that's the case you might want to try a masked text box. There is a current thread in C# on this (http://www.daniweb.com/forums/post1440491.html#post1440491) (I know it's not the right language, but it's the same .NET library, so you'd just have to translate it into C++ syntax).

If that's not what you need, post back and we can help you figure out what you need.

0x08 is a backspace
0x6d is the letter 'm'

Don't you want something like
if( Char::IsDigit(e->KeyChar) || e->KeyChar == 0x08 || e->KeyChar == '-')

Edited 5 Years Ago by Ancient Dragon: n/a

Try,

private: System::Void textBox1_KeyPress(System::Object^  sender, System::Windows::Forms::KeyPressEventArgs^  e) 
 {
 if(Char::IsDigit(e->KeyChar))
    return;
 if(e->KeyChar=='\b')
   return;
 if(e->KeyChar=='-')
   return;
 e->Handled=true;
}

Thanks to all.
The code from adatapost works fine.

Edited 5 Years Ago by Danny1994: n/a

This question has already been answered. Start a new discussion instead.