PDA

Click to See Complete Forum and Search --> : Only numbers available to type + Reset Program


Fromethius
Mar 7th, 2006, 07:28 PM
For a program I am making, I want to have it so you can only type numbers 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, and a decimal point. Also, I would appreciate it if someone could supply me ( if it exists ) with a code to stop the .Net error message. I have it so that if you have nothing in a textbox, a messagebox pops up. Then you click ok and an error message comes up. You know, the details, continue, quit, unhandled exception. I want to just have the message box come up and then you click ok and it just reverts the program to its normal self. As if it was just loaded.

PLEASE answer. I have been searching for about an hour for this stuff and haven't found a thing. I signed up to make this topic so please don't turn me down! :D

jmcilhinney
Mar 7th, 2006, 11:10 PM
That messae you are talking about is the Unhandled Exception dialogue and it is dispayed when your code throws an exception that is not handled. That means your code is broken. You need to fix the issue that is causing the exception to be thrown, or if it is a legitimate exception then you need to catch it and process it appropriately.

As for typing the numbers, I assume that you mean into a TextBox. The general advice is to handle the KeyPress event and reject any characters that you don't want. Here's some code to do what you want: private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!(char.IsControl(e.KeyChar) || char.IsDigit(e.KeyChar) || e.KeyChar == '.'))
{
e.Handled = true;
}
}The 'IsControl' has been included so that keys like BackSpace and Delete still be have as desired. Remember though that more than one decimal point in a number is not valid, which that code will still allow. There are also other issues, like the user pasting invalid characters from the clipboard. If you can use third-party controls then the WFC library (see my signature for a link) includes a NumberBox that takes all these things into account without you having to intervene. Not so good as a learning experience though. It was created for .NET 1.1. A 2.0 versin is coming but I don't know when. I have tested the 1.1 version in 2.0 a bit and it seems to work but I make no guarantees.

Fromethius
Mar 8th, 2006, 02:47 PM
Uhh yea, exactly... An unhandled exception.. This is when nothing is in the textbox field and the click the button.. I want a messagebox to come up though instead of the unhandled exception box when they make this error

jmcilhinney
Mar 8th, 2006, 02:56 PM
The most likely reason that you are getting an unhandled exception is that you are trying to convert the contents of the TextBox to a number, which is not possible if it contains an empty string. You need to test the contents of the TextBox first, then display an error message if it is empty or try to convert it to a number otherwise. Note that you should still use a TryParse method to convert it because you still are not guaranteed that the TextBox contains a valid number. In fact, I'd just use TryParse in the first place and then Tell them to enter a valid number if it fails, regardless of what they've entered.

Fromethius
Mar 8th, 2006, 03:02 PM
Look. You do not understand anything what I am saying. I DON'T GET UNHANDLED EXCEPTIONS. You put in numbers, click convert it works! However!! When there is nothing, yes NOTHING in the textbox and you click convert you get the unhandled exception. Yes I know why, you cannot divide nothing by 25.4. I just want to replace the unhandled exception with a message box such as this:


MessageBox.Show("Empty Text Field", "Metric Converter",
MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);


If you have aim or msn, it may be easier for us to discuss this as I have other questions

Kasracer
Mar 8th, 2006, 07:05 PM
Just put an If statement to see if the Textbox is empty and if it is, pop up your error.