Click here to Skip to main content
14,267,090 members
Rate this:
Please Sign up or sign in to vote.
Hi there,

I'm currently trying to write a calculator program and upon asking for feedback it was pointed out to me that it wasn't clear that only one input box should be empty while the rest require a number. I've tried the below code but am struggling to solve the problem.

Does anyone have any advice they could offer?

I appreciate any suggestions you might have with regards to this topic.

Thank you very much and many kind regards.

What I have tried:

 public Form1()
        {
            InitializeComponent();

            txt_P1In.Text = string.Empty;
            txt_P2In.Text = string.Empty;
            txt_Vol1In.Text = string.Empty;
            txt_Vol2In.Text = string.Empty;
            txt_U1In.Text = string.Empty;
            txt_U2In.Text = string.Empty;
            txt_vel1In.Text = string.Empty;
            txt_vel2In.Text = string.Empty;
            txt_h1In.Text = string.Empty;
            txt_h2In.Text = string.Empty;
            txt_QIn.Text = string.Empty;
            txt_WIn.Text = string.Empty;
            Z = 0;
            P1 = P2 = V1 = V2 = U1 = U2 = v1 = v2 = h1 = h2 = Q = W = Z;
            g = 9.81;
        }

private void CalcBtnOut_Click(object sender, EventArgs e)
        {
            if (radioBtnP1.Checked == true)
            {
                if (txt_P2In.Text == null || txt_Vol1In.Text == null || txt_Vol2In.Text == null || txt_U1In.Text == null || txt_U2In.Text == null || txt_vel1In.Text == null || txt_vel2In.Text == null || txt_h1In.Text == null || txt_h2In.Text == null || txt_QIn.Text == null || txt_WIn.Text == null)
                {
                    MessageBox.Show("You have a missing input");
                }
                else
                P1 = (((P2 * V2) + U2 + ((v2 * v2) / 2) + (g * h2) + W - Q - (g * h1) - ((v1 * v1) / 2) - U1) / V1);
                txt_P1Out.Text = P1.ToString();
                txt_P1In.Text = txt_P1Out.Text;
            }
            else
            if (CalcP1.Checked == false)
            {
                txt_P1Out.Text = null;
            }
Posted
Updated 26-Mar-17 5:40am
Comments
NotPolitcallyCorrect 26-Mar-17 11:36am
   
" advice they could offer", yeah do some research on validation.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

The Text property of a TextBox will never be null - it can be the empty string, but it's never null.
So this test:
if (txt_P2In.Text == null || txt_Vol1In.Text == null || txt_Vol2In.Text == null || txt_U1In.Text == null || txt_U2In.Text == null || txt_vel1In.Text == null || txt_vel2In.Text == null || txt_h1In.Text == null || txt_h2In.Text == null || txt_QIn.Text == null || txt_WIn.Text == null)
will never succeed.
Instead check each textbox with:
if (string.IsNullOrWhitespace(txt_P2In.Text) || string.IsNullorWhiteSpace(...

Personally, I'd use a method:
private bool IsAnyFieldEmpty(params TextBox[] boxes)
    {
    foreach (TextBox box in boxes)
        {
        if (string.IsNullOrWhiteSpace(box.Text)) return true;
        }
    return false;
    }
And call it like this:
if (IsAnyFieldEmpty(txt_P2In, txt_Vol1In, txt_Vol2In, txt_U1In, 
                    txt_U2In, txt_vel1In, txt_vel2In, txt_h1In, 
                    txt_h2In, txt_QIn, txt_WIn.Text))
   
Comments
Member 13068046 26-Mar-17 12:03pm
   
Your an absolute star! Thank you so much! That works absolutely brilliantly.
OriginalGriff 26-Mar-17 12:25pm
   
You're welcome!

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100