Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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... :)

Posted

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

Live as if you were to die tomorrow. Learn as if you were to live forever.
Gandhi
Posted

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?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...