ThienZ Posted May 24, 2005 Posted May 24, 2005 i tried this : oTB.KeyPress += new KeyPressEventHandler(TextBoxNumberOnly_KeyPress); private void TextBoxNumberOnly_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { if(!Char.IsNumber(e.KeyChar)) { e.Handled = true; } } but this way i can't press backspace too.... the cursor arrow and delete buttons work tho... how should i do this a better way? thx in advanced... :) Quote
AlexCode Posted May 24, 2005 Posted May 24, 2005 This will do: http://www.codeproject.com/vb/net/Complete_Textbox.asp Alex :p Quote Software bugs are impossible to detect by anybody except the end user.
techmanbd Posted May 24, 2005 Posted May 24, 2005 this is how I do it, but in VB but maybe help you with your code because it is very similiar Private Sub txtCalledNumber_KeyPress(ByVal sender As System.Object, ByVal e As KeyPressEventArgs) Handles txtCalledNumber.KeyPress If Not Char.IsNumber(e.KeyChar) And Not Asc(e.KeyChar) = 8 Then e.Handled = True End If End Sub This allows only numbers and the backspace Quote Live as if you were to die tomorrow. Learn as if you were to live forever. Gandhi
Administrators PlausiblyDamp Posted May 24, 2005 Administrators Posted May 24, 2005 You probably also want to handle pasting of text into the control as well ;) an easy one to miss. Never underestimate the end user :) Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
ThienZ Posted May 25, 2005 Author Posted May 25, 2005 thx guys :) now the code looks like this : private void TextBoxNumberOnly_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { if(!Char.IsNumber(e.KeyChar) && !( ((int) System.Text.Encoding.ASCII.GetBytes(e.KeyChar.ToString())[0]) == 8)) { e.Handled = true; } } the only problem is that this code is a little slow in the first usage of escape key (it thinks about a second), but after that you notice nothing else... maybe any other suggestions to better the running time of the code? Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.