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)

Recommended Answers

All 4 Replies

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 == '-')

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.