Question Input String error when Winform Textboxes are blank

agnl225

Member
Joined
Jun 12, 2019
Messages
11
Programming Experience
Beginner
Hi,
I'm brand new to C# and more proficient in VBA but I was designing something basic for work purposes and I'm stuck. The form works well when data is entered, however I'm getting an error when my windows form app is blank and i think the cause is the conversion of the timespan difference to decimal? I used tryparse,parse,convert and it is still not working properly when the form is blank and the button is clicked. Any suggestions would be greatly appreciated! Error message below and code below that.

Thanks!

544




C#:
private void Button1_Click(object sender, EventArgs e)
        {
            if
                   (textBox4.Text == "")
            {
                MessageBox.Show("Please Enter Payment Agreement Amount");
            }

            if
                    (comboBox2.Items.Count == 0)
            {
                MessageBox.Show("Please Select A Payment Type");
            }
            else
            {
                DateTime date1 = dateTimePicker3.Value;
                DateTime date2 = dateTimePicker2.Value;
                TimeSpan difference = date1 - date2;
                decimal TotalWeeks = difference.Days / 7;
                decimal PayTotal = decimal.Parse(textBox4.Text);
                decimal PaymentWeeks = TotalWeeks * PayTotal;
                decimal TotalMonths = difference.Days / 30;
                decimal PaymentMonths = TotalMonths * PayTotal;
                if (comboBox2.SelectedItem == "Months")
                    textBox3.Text = String.Format("$" + "{0:0,0.00}", Convert.ToDecimal(PaymentMonths));
                if (comboBox2.SelectedItem == "Weeks")
                    textBox3.Text = String.Format("$" + "{0:0,0.00}", Convert.ToDecimal(PaymentWeeks));
            }
 

Attachments

  • 1560356956059.png
    1560356956059.png
    21.4 KB · Views: 77
Last edited by a moderator:
C#:
 private void Button1_Click(object sender, EventArgs e)
        {
            if
                   (textBox4.Text == "")
            {
                MessageBox.Show("Please Enter Payment Agreement Amount");
            }
            if
                    (comboBox2.Items.Count == 0)

            {
                MessageBox.Show("Please Select A Payment Type");
            }
            else
            {
                DateTime date1 = dateTimePicker3.Value;
                DateTime date2 = dateTimePicker2.Value;

                TimeSpan difference = date1 - date2;
                decimal TotalWeeks = difference.Days / 7;
                decimal PayTotal = Decimal.Parse(textBox4.Text);
                decimal PaymentWeeks = TotalWeeks * PayTotal;
                decimal TotalMonths = difference.Days / 30;
                decimal PaymentMonths = TotalMonths * PayTotal;
                if (comboBox2.SelectedItem == "Months")
                    textBox3.Text = String.Format("$" + "{0:0,0.00}", Convert.ToDecimal(PaymentMonths));
                
                if (comboBox2.SelectedItem == "Weeks")
                    textBox3.Text = String.Format("$" + "{0:0,0.00}", Convert.ToDecimal(PaymentWeeks));
            }

        }


        private void Button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Label5_Click(object sender, EventArgs e)
        {

        }
        public Point mouselocation;
        private void JUDGPAYCALC_MouseDown(object sender, MouseEventArgs e)
        {
            mouselocation = new Point(-e.X, -e.Y);

        }

        private void JUDGPAYCALC_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Point mousepose = Control.MousePosition;
                mousepose.Offset(mouselocation.X, mouselocation.Y);
                Location = mousepose;
            }
        }

        private void Button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
 
Hi,
I'm brand new to C# and more proficient in VBA but I was designing something basic for work purposes and I'm stuck. The form works well when data is entered, however I'm getting an error when my windows form app is blank and i think the cause is the conversion of the timespan difference to decimal? I used tryparse,parse,convert and it is still not working properly when the form is blank and the button is clicked. Any suggestions would be greatly appreciated! Error message below and code below that.

Thanks!
C#:
private void Button1_Click(object sender, EventArgs e)
        {
            if
                   (textBox4.Text == "")
            {
                MessageBox.Show("Please Enter Payment Agreement Amount");
            }

            if
                    (comboBox2.Items.Count == 0)
            {
                MessageBox.Show("Please Select A Payment Type");
            }
            else
            {
                DateTime date1 = dateTimePicker3.Value;
                DateTime date2 = dateTimePicker2.Value;
                TimeSpan difference = date1 - date2;
                decimal TotalWeeks = difference.Days / 7;
                decimal PayTotal = decimal.Parse(textBox4.Text);
                decimal PaymentWeeks = TotalWeeks * PayTotal;
                decimal TotalMonths = difference.Days / 30;
                decimal PaymentMonths = TotalMonths * PayTotal;
                if (comboBox2.SelectedItem == "Months")
                    textBox3.Text = String.Format("$" + "{0:0,0.00}", Convert.ToDecimal(PaymentMonths));
                if (comboBox2.SelectedItem == "Weeks")
                    textBox3.Text = String.Format("$" + "{0:0,0.00}", Convert.ToDecimal(PaymentWeeks));
            }
To get back to the original post, I do see an obvious oversight with the code, I see you're checking the value of the textbox's Text property and if it's blank (empty string) you give them a messagebox but then the code continues on from there and if an item is selected in the combobox the code (blank value in the textbox or not) will try to use it which is causing the immediate issue.
If you change the "if (comboBox2.Items.Count == 0)" part to "else if (comboBox2.Items.Count == 0)" it will have your code functioning as you probably originally intended.
But I also see a lot of replies on type converting and that's all important things to know how to do, especially when using anything typed into a textbox in a form other than a string (text).
 
i changed the form border style from none to SizableToolWindow and it doesnt seem to be working since but that may not be correlated..?
 
The Windows Forms Designer can be very flakey (especially if you are using version of VS before VS2015). Typical recovery process there is to exit out of Visual Studio, clean out the obj and bin directories. If you have any .suo files blow those away. Then start up VS again and do a clean build.
 
Is post #16 all of your form code? Do you have a Form1_Load() method?
 
Then that is likely what is causing that crash you are currently seeing in the constructor. It's trying to find that method on the line:
C#:
this.Load += new System.EventHandler(this.Form1_Load);
Personally, I would have expected a compiler error, not a runtime exception.
 
The Windows Forms Designer can be very flakey (especially if you are using version of VS before VS2015). Typical recovery process there is to exit out of Visual Studio, clean out the obj and bin directories. If you have any .suo files blow those away. Then start up VS again and do a clean build.
I've actually experienced the opposite, VS2008 & VS2010 were far better IDE's then VS2012, VS2013, and to a minor degree VS2015. I haven't tried VS2017 or VS2019 yet.
Other Visual Studio's compatibility with the Windows OS they haven't added anything significant to it since VS2008, I'd be using that still if it wasn't for me running Windows 10.

VS2015 has been pretty good to me since the last update (Update 3) a couple years ago and it'd be even better if they didn't have the heavy skinned UI they introduced in VS2012 still.
 
Back
Top Bottom