-5

I'm doing a simple order total calculation function and I'm trying to figure out how to display a custom error message when the user input is the incorrect format. I.e. when they enter a non-integer in txtJava.Text, the message will display "Please enter an integer for Java"

    private void btnSubmit_Click(object sender, EventArgs e)
    {
        
        int Java;
        int Latte;
        int Mocha;
        int Frappe; 
        string Name = txtName.Text;
        double javaPrice = 1.25;
        double mochaPrice = 2.95;
        double lattePrice = 2.75;
        double frappePrice = 3.25;
        double dblTaxRate = .09;
        double dblSubtotal;
        double dblTax;
        double dblTotal;

        txtMessage.Text = "Thank you " + Name + " for your order!";
        
        Java = Convert.ToInt32(txtJava.Text);
        Latte = Convert.ToInt32(txtLatte.Text);
        Mocha = Convert.ToInt32(txtMocha.Text);
        Frappe = Convert.ToInt32(txtFrappe.Text);
        dblSubtotal = Java * javaPrice + Mocha * mochaPrice + Frappe * frappePrice + Latte * lattePrice;
        dblTax = dblSubtotal * dblTaxRate;
        dblTotal = dblSubtotal + dblTax;

        txtSubtotal.Text = dblSubtotal.ToString("C2");
        txtTax.Text = dblTax.ToString("C2");
        txtTotal.Text = dblTotal.ToString("C2");
Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • 1
    Your title suggests there is a `try/catch` block to add a custom error to, but the posted code has no such block. – Scott Hunter Apr 29 '22 at 14:49
  • You could just....add a `try/catch` and (im guessing) write a `txtMessage.Txt` in the catch – JamesS Apr 29 '22 at 14:50
  • You seem to know somehow that you need a try/catch (you don't actually but might use it) for that then what have you tried with it? – Ralf Apr 29 '22 at 14:51
  • 1
    You probably don't need a try/catch and instead want to TryParse the field and then set a label or something to an error. – nullforce Apr 29 '22 at 14:54
  • 1. Don't error out on the user. Make it impossible to enter invalid inputs, by using the appropriate input component. Which in your case (assuming winforms) would be a ["numeric updown"](https://learn.microsoft.com/en-us/dotnet/desktop/winforms/controls/numericupdown-control-windows-forms?view=netframeworkdesktop-4.8). (There is also one in WPF, I believe) 2. Do not use floating point types for monetary amounts. – Fildor Apr 29 '22 at 15:46
  • Please provide enough code so others can better understand or reproduce the problem. – Community Apr 29 '22 at 17:40
  • Welcome to StackOverflow. If an answer solves your problem mark it as an acceptable answer. If it helps you give it an upvote. If the answer is offtopic or doesn’t help at all, downvote or add a comment. See also [stackoverflow.com/help/why-vote](https://stackoverflow.com/help/why-vote) – Jiale Xue - MSFT May 02 '22 at 02:31

2 Answers2

2

Avoid using try Prase or try catch in this scenario. It is better to change the textbox input type to be a number only.

I can't see what type of application you are running, but if its a webForms application you could alter the markup to look something like this:

<input type="number" id="quantity" name="quantity" min="1" max="100">

If its a WPF application: Allow only numeric entry in WPF Text Box

If this doesn't help you may need to provide more code. Give us a bit more context.

Happy coding.

DharmanBot
  • 1,066
  • 2
  • 6
  • 10
Geoffrey Fook
  • 578
  • 3
  • 11
0

This code, instead of Java = Convert.ToInt32(txtJava.Text);

if (!TryParse(txtJava.Text, Java)) {
      //Display the text you want
}

In addition, your variables are not with the same naming convention ;)

Dylan El Bar
  • 783
  • 1
  • 14