Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

if statement - TryParse IfStatment Causing No Return

It's a simple winform that converts weight into another unit, I have a optional box for the user to input the number of decimal places the conversion should do but the issue is if the user enter nothing in the decimal textbox and click the convert button (code below) then nothing happens and nothing is converted. I want there to be a default amount set such as 2. I know this is due to my if statement return but I cannot think of a way to fix it and code in the default amount (I'm new trying to get back into the swings of coding and C# please be nice :) :

private void btnConvert_Click(object sender, EventArgs e)
    {
        string errorMessage = "Please do not enter two identical weight units";

        if (!double.TryParse(txtboxAmount.Text, out double amount) || !int.TryParse(decimelPlacetxtBox.Text, out int decimalPlaces)) return; // Prevents empty string, validation error, return halts the code
        {
            int decimalPlace = int.Parse(decimelPlacetxtBox.Text); // Needs to be here in order of operation
            double userInput = Convert.ToDouble(txtboxAmount.Text);


            // Fail safe

            if (comboBoxFrom.SelectedItem.ToString() == "ST" && comboBoxTo.SelectedItem.ToString() == "ST")
            {
                MessageBox.Show(errorMessage, "Error in converting", MessageBoxButtons.OK);
            }

            if (comboBoxFrom.SelectedItem.ToString() == "LB" && comboBoxTo.SelectedItem.ToString() == "LB")
            {
                MessageBox.Show(errorMessage, "Error in converting", MessageBoxButtons.OK);
            }

            if (comboBoxFrom.SelectedItem.ToString() == "KG" && comboBoxTo.SelectedItem.ToString() == "KG")
            {
                MessageBox.Show(errorMessage, "Error in converting", MessageBoxButtons.OK);
            }

            // Killogram convertion


            if (comboBoxFrom.SelectedItem.ToString() == "KG" && comboBoxTo.SelectedItem.ToString() == "LB")
            {
                double conver = userInput * 2.20462262185;
                txtBoxResult.Text = "Converted Amount : " + conver.ToString("N" + decimalPlace);
                lblUnit.Text = "LB".ToString();
            }

            if (comboBoxFrom.SelectedItem.ToString() == "LB" && comboBoxTo.SelectedItem.ToString() == "KG")
            {
                double conver = userInput / 2.2046;
                txtBoxResult.Text = "Converted Amount : " + conver.ToString("N" + decimalPlace);
                lblUnit.Text = "LG".ToString();
            }

            if (comboBoxFrom.SelectedItem.ToString() == "ST" && comboBoxTo.SelectedItem.ToString() == "KG")
            {
                double conver = userInput * 6.35;
                txtBoxResult.Text = "Converted Amount : " + conver.ToString("N" + decimalPlace);
                lblUnit.Text = "KG".ToString();
            }

            // Pound convertion

            if (comboBoxFrom.SelectedItem.ToString() == "LB" && comboBoxTo.SelectedItem.ToString() == "ST")
            {
                double conver = userInput / 14;
                txtBoxResult.Text = "Converted Amount : " + conver.ToString("N" + decimalPlace);
                lblUnit.Text = "ST".ToString();
            }

            if (comboBoxFrom.SelectedItem.ToString() == "ST" && comboBoxTo.SelectedItem.ToString() == "LB")
            {
                double conver = userInput / 14;
                txtBoxResult.Text = "Converted Amount : " + conver.ToString("N" + decimalPlace);
                lblUnit.Text = "LB".ToString();
            }

            if (comboBoxFrom.SelectedItem.ToString() == "LB" && comboBoxTo.SelectedItem.ToString() == "KG")
            {
                double conver = userInput / 2.205;
                txtBoxResult.Text = "Converted Amount : " + conver.ToString("N" + decimalPlace);
                lblUnit.Text = "KG".ToString();
            }

            {
                comboBoxFrom.SelectedItem = -1;
            }

            // Stone convertion

            if (comboBoxFrom.SelectedItem.ToString() == "ST" && comboBoxTo.SelectedItem.ToString() == "LB")
            {
                double conver = userInput * 14;
                txtBoxResult.Text = "Converted Amount : " + conver.ToString("N" + decimalPlace);
                lblUnit.Text = "LB".ToString();
            }

            if (comboBoxFrom.SelectedItem.ToString() == "LB" && comboBoxTo.SelectedItem.ToString() == "ST")
            {
                double conver = userInput / 14;
                txtBoxResult.Text = "Converted Amount : " + conver.ToString("N" + decimalPlace);
                lblUnit.Text = "ST".ToString();
            }

            if (comboBoxFrom.SelectedItem.ToString() == "LB" && comboBoxTo.SelectedItem.ToString() == "KG")
            {
                double conver = userInput / 2.205;
                txtBoxResult.Text = "Converted Amount : " + conver.ToString("N" + decimalPlace);
                lblUnit.Text = "KG".ToString();


            }

        }
    }
question from:https://stackoverflow.com/questions/65832226/tryparse-ifstatment-causing-no-return

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...