Reputation: 13
I hope someone can help me.
I have doing a Windows Form in C# and I have someproblems with a TextBox. I am new to programming.
///variables
double n, x, i = 15.0, act = 25.0, f, z;
string ac;
int n2, pos, f2;
///Form1 Load
private void Form1_Load(object sender, EventArgs e)
{
///This allow to have decimal with the initial value of 25.0
textBox2.Text = string.Format("{0:0.0}", act);
}
and
///TextBox2
private void textBox2_TextChanged(object sender, EventArgs e)
{
if (textBox2.Text != null )
{
ac = Convert.ToString(textBox2.Text);
z = double.Parse(textBox2.Text);
if (z >= 0.0 && z <= 30.0)
{
///Operations
f = 30.0 - z;
f2 = (int)f * 5;
this.pictureBox4.Size = new System.Drawing.Size(25, f2);
}
if (textBox2.Text == "")
{
///Operations
z = 0.0;
textBox2.Text = "0,0";
this.pictureBox4.Size = new System.Drawing.Size(25, 150);
///Message that say value is not between 0.0 and 30.0
MessageBox.Show("Enter values between 0.0 y 30.0", "Value out of range", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
I want to enter a decimal value in textBox2. But when I press the return key for delete 25.0 it return this error: An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll Additional information: Input string was not in a correct format. I want to fix this error and it was in empty to enter values with keyboard and this values will be between 0.0 and 30.0, if the value on textBox2 is out of range it should show a message that value is not between 0.0 and 30.0. Please enter one value is this range.
I have also this
private void textBox2_MouseUp(object sender, MouseEventArgs e)
{
MessageBox.Show("Enter values between 0.0 and 30.0");
}
Wiht this code when I press the textBox2 with mouse it show me this message.
The operations with code is for move and change size of two PictureBox.
Change size of PictureBox works fine, and I think decimal value works fine also i enter values between 0.0 and 30.0 and it works, theproblem is when it is empty value and negative number.
Thanks for the help
Upvotes: 0
Views: 8618
Reputation: 633
Perhaps you want to use something like:
if(!double.TryParse(textBox2.Text, out z))
MessageBox.Show("The value cannot be parsed");
Upvotes: 5
Reputation: 64
You need to convert it to DOUBLE
double strTxt = Convert.ToDouble(txtBox1.text);
Upvotes: 1