0

I have made a form which works perfectly fine when the fields are filled in. If you click the "convert" button with a blank textbox, it throws an error due to parsing a null value.

Obviously this means that I've declared my variable upon the button click.

I would also like a message box to pop up if the field is empty, to prompt the user to enter data.

Here is the code I have for the convert button:

private void exitButton_Click(object sender, EventArgs e)
    {
        //closes the form
        this.Close();
    }

private void convertButton_Click(object sender, EventArgs e)
    {
        decimal measurementDecimal = decimal.Parse(enterTextBox.Text);          
        //if else arguments for radio buttons
        if (string.IsNullOrWhiteSpace(enterTextBox.Text))
        {
            MessageBox.Show("Please enter a value");
        }             

        else if (inchesFromRadioButton.Checked && (inchesToRadioButton.Checked))
        {
            convertedTextBox.Text = measurementDecimal.ToString();
        }
        else if (inchesFromRadioButton.Checked && (feetToRadioButton.Checked))
        {
            convertedTextBox.Text = (measurementDecimal / 12).ToString();
        }
        else if (inchesFromRadioButton.Checked && (yardsToRadioButton.Checked))
        {
            convertedTextBox.Text = (measurementDecimal / 36).ToString();
        }
        else if (feetFromRadioButton.Checked && (inchesToRadioButton.Checked))
        {
            convertedTextBox.Text = (measurementDecimal * 12).ToString();
        }
        else if (feetFromRadioButton.Checked && (feetToRadioButton.Checked))
        {
            convertedTextBox.Text = measurementDecimal.ToString();
        }
        else if (feetFromRadioButton.Checked && (yardsToRadioButton.Checked))
        {
            convertedTextBox.Text = (measurementDecimal / 3).ToString();
        }
        else if (yardsFromRadioButton.Checked && (inchesToRadioButton.Checked))
        {
            convertedTextBox.Text = (measurementDecimal * 36).ToString();
        }
        else if (yardsFromRadioButton.Checked && (feetToRadioButton.Checked))
        {
            convertedTextBox.Text = (measurementDecimal * 3).ToString();
        }
        else if (yardsFromRadioButton.Checked && (yardsToRadioButton.Checked))
        {
            convertedTextBox.Text = measurementDecimal.ToString();
        }
        else
        { 
            MessageBox.Show("Parameters not set.  Please select a 'From' and 'To'"); 
        }

1 Answer 1

2

Solution 1 : You can perform null or empty check before parsing the input value.and if it is invalid display warning and return from the method.

Try This:

private void convertButton_Click(object sender, EventArgs e)
{
   //if else arguments for radio buttons
    if (string.IsNullOrWhiteSpace(enterTextBox.Text))
    {
        MessageBox.Show("Please enter a value");
        return;
    }       

     /*Your remaining code here*/
    decimal measurementDecimal = decimal.Parse(enterTextBox.Text);    

Solution 2: You can use decimal.TryParse() method for checking the valid decimal value.

From MSDN:

Converts the string representation of a number to its Decimal equivalent. A return value indicates whether the conversion succeeded or failed.

private void convertButton_Click(object sender, EventArgs e)
{

    decimal measurementDecimal ;

    if (!decimal.TryParse(enterTextBox.Text,out measurementDecimal))
    {
        MessageBox.Show("Please enter a valid value");
        return;
    } 
   else
   {

     /*Your remaining code here*/

   }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.