Click here to Skip to main content
15,353,676 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi there,

I'm currently writing a code for a calculator meant to work out any value of a physics equation given every other value. While this part appears to be working and I've managed to incorporate TryParse into the conversion so that it will set any non-numerical value to zero, I am having difficulty figuring out how to prevent the entry of symbols or letters.

What I really need is either a code that will send a message if a symbol or letter is inserted into the text box or a text box that will refuse to accept an input other than a numerical value.

Can anyone help me? Any advice would be greatly appreciated.

Below is a sample of my current code.

Thank you.

What I have tried:

public partial class Form1 : Form
{
    double P1, P2, V1, V2, U1, U2, v1, v2, h1, h2, Q, W, Z, g;

   //*******************************************************************
    public Form1()
    {
        InitializeComponent();

        Z = 0;
        P1 = P2 = V1 = V2 = U1 = U2 = v1 = v2 = h1 = h2 = Q = W = Z;
        g = 9.81;
    }

   //*******************************************************************
    private void txtBox_P2In_TextChanged(object sender, EventArgs e)
    {
        double.TryParse(txtBox_P2In.Text, out P2);
    }

   //*******************************************************************
    private void txtBox_P1In_TextChanged(object sender, EventArgs e)
    {
        double.TryParse(txtBox_P1In.Text, out P1);
    }

   //*******************************************************************
    private void CalcButton1_Click(object sender, EventArgs e)
    {
        if (radioButtonP1.Checked == true)
        {
            P1 = (((P2 * V2) + U2 + ((v2 * v2) / 2) + (g * h2) + W - Q - (g * h1) - ((v1 * v1) / 2) - U1) / V1);
            txtBox_P1Out.Text = P1.ToString();
        }
        else if (radioButtonP1.Checked == false)
        {
            txtBox_P1Out.Text = null;
        }
    }
}
Posted
Updated 21-Mar-17 17:58pm
v2
Comments
CHill60 21-Mar-17 19:34pm
   
Have you noticed the list of Related Questions to the right of your question?
[no name] 21-Mar-17 19:52pm
   
I bet there are thousands of google search result for that exact same question....
Member 13068046 21-Mar-17 21:26pm
   
There are, but I'm a beginner at this and most of the code solutions they show baffle me. This site tends to be good for straight answers hence why I put up the question. Plus the answer I keep coming across:

private void txt_P1In_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = !(Char.IsDigit(e.KeyChar) || e.KeyChar == '\b' || e.KeyChar = '.');
}

doesn't work in my code and I have no idea why so an easier or explained solution would be preferable.
Karthik_Mahalingam 21-Mar-17 22:11pm
   
make sure the event is mapped to the control
[no name] 22-Mar-17 13:55pm
   
There are plenty of straight up clearly explained examples out there that answer your question. And the most useless explanation for a problem is "doesn't work". If you call you mechanic on the phone and say "Car doesn't work" would you expect that someone would know exactly what the problem is? We have noidea what you think "doesn't work" means anymore than your mechanic would.
Member 13068046 22-Mar-17 19:56pm
   
Sorry, but if I could discern why the code didn't work I wouldn't be asking for help would I?

With a mechanic you get some sort of feedback from the car that you can describe to them which will help them narrow down the issue (i.e. its making a rattling noise under the bonnet, it keeps stalling, smoke is being ejected out of the back).

I can't possibly do that because I can't even describe the problem other than that the text box still accepts letters and symbols and no message box appears at any time (essentially meaning it has not worked since it has not done anything that I wanted it to do).

By "clearly explained" do you mean clearly explained for you, or clearly explained for me, as I do believe that particular phrasing carries some subjectivity to it. But by all means, please, if you know of a site that explains really well and simply how to code this particular problem, feel free to send me the link. That way I can stop going through the trouble of researching an answer and you can stop going through the trouble of putting antagonizing comments on my question page.

Hope that clarifies things.

Thank you.

You can use below code.

C#
private void txtBox_P1In_KeyPress(object sender, KeyPressEventArgs e)
{
    if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) &&
        (e.KeyChar != '.'))
    {
            e.Handled = true;
    }
}

and though you are working on calculation type application you may would like to allow one .(dot) use below to code in keypress event of txtBox_P1In

C#
if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1))
     {
       e.Handled = true;
     }
   
Hi Member 13068046,

If you still need help:

1) Remove the TextChanged event handler.
2) Add KeyPress event handler.
3) Code as:
private void txtBox_P1In_KeyPress(object sender, KeyPressEventArgs e)
{
    if (!char.IsDigit(e.KeyChar))
    {
        MessageBox.Show("Only numbers please!");
        e.Handled = true;
    }
}
'e.Handled = True' instructs the compiler that you have explicitly taken care of the input, and hereby relieving the compiler of its duty with further actions (displaying the chracter).
   
Comments
Member 13068046 22-Mar-17 9:10am
   
Sorry, but this doesn't work. I tried it and no matter what I did no message box would appear and stop me.

But thank you very much for trying to help. It's greatly appreciated.
Mehedi Shams 22-Mar-17 20:04pm
   
Hi Member 13068046,

I tested it and it worked fine. What really might happen at your end is the event is not firing. I know this might sound odd, but did you create handlers in the right way? (Just a copy and paste of the above code would not work).
Member 13068046 26-Mar-17 11:53am
   
Hi there, I figured out that if I put e.Handled = true before the MessageBox.Show It works brilliantly. I don't know why but it does.

Thank you so much.
Mehedi Shams 26-Mar-17 18:17pm
   
Hi Member 13068046,

Glad that you found a work-around. Either way should be fine, but I don't really know why one worked and the other failed. Please make a clean rebuild and see if it is working either way.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900