3 Replies - 2740 Views - Last Post: 06 January 2010 - 08:49 AM Rate Topic: -----

#1 Ahmedn1  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 24
  • View blog
  • Posts: 564
  • Joined: 04-August 09

How to check for user input

Posted 06 January 2010 - 04:00 AM

I'm creating a number system convert program

there is a text box

I need the textbox to sendkeys backspace if:

1- the combobox = decimal and the user entered other than a number or a minus sign

2- the combo = binary and the input is other than minus or numbers from 0-1

3- the combo = octal and the input is other than minus or numbers from 0-7

4- the combo = hexadecimal and the input is other than minus or numbers from 0-9 and letters A-F

P.S. : I know how to sendkeys
tell me only how to check these conditions



Another thing is :

Can I call a method its name stored in a string?

I mean if i have a string x="Binary" can I call the method Binary using that string without the need to write its name?

This post has been edited by Ahmedn1: 06 January 2010 - 04:08 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How to check for user input

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1533
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: How to check for user input

Posted 06 January 2010 - 05:39 AM

You only want to allow certain characters in a textbox, correct? If so, why are you allowing them to type it in then doing a sendkey to remove the bad character? Why don't you just limit the textbox to only allow the characters you want to allow?
Was This Post Helpful? 0
  • +
  • -

#3 Ahmedn1  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 24
  • View blog
  • Posts: 564
  • Joined: 04-August 09

Re: How to check for user input

Posted 06 January 2010 - 05:41 AM

View Posteclipsed4utoo, on 6 Jan, 2010 - 04:39 AM, said:

You only want to allow certain characters in a textbox, correct? If so, why are you allowing them to type it in then doing a sendkey to remove the bad character? Why don't you just limit the textbox to only allow the characters you want to allow?



And how is that?
Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1533
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: How to check for user input

Posted 06 January 2010 - 08:49 AM

View PostAhmedn1, on 6 Jan, 2010 - 08:41 AM, said:

View Posteclipsed4utoo, on 6 Jan, 2010 - 04:39 AM, said:

You only want to allow certain characters in a textbox, correct? If so, why are you allowing them to type it in then doing a sendkey to remove the bad character? Why don't you just limit the textbox to only allow the characters you want to allow?



And how is that?


in the KeyPress event for the textbox, check the ASCII code for the character that was entered

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
	 // ASCII code 48-57 is the numbers 0-9
	 // ASCII code 8 is backspace
	 // ASCII code 45 is the dash
	 if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != 45) 
		e.Handled = true;
}



The "e.Handled = true" is telling the application that you have already handled adding the keystroke. Since you actually have not, this means that the keystroke will not occur and will not be added to the textbox.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1