0

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

4
  • 6
    You need to make more efforts to explain your issue, I could not quite understand the question
    – BlueTrin
    Jul 28, 2014 at 16:48
  • 2
    @BlueTrin You are fortunate, at least you see the question, I cannot even see the question
    – Steve
    Jul 28, 2014 at 16:55
  • 1
    @BlueTrin I don't had finish the question haha. All the question is done and explained.
    – Arkaedus
    Jul 28, 2014 at 17:19
  • @Steve I don't had finish the question haha. All the question is done and explained.
    – Arkaedus
    Jul 28, 2014 at 17:20

2 Answers 2

5

Perhaps you want to use something like:

if(!double.TryParse(textBox2.Text, out z))
    MessageBox.Show("The value cannot be parsed");
1
  • 1
    I can't vote you because i don't have reputation.Thanks a lot you help me.
    – Arkaedus
    Jul 28, 2014 at 17:43
1

You need to convert it to DOUBLE

double strTxt = Convert.ToDouble(txtBox1.text);
1
  • @Akemi_Chou Thanks for your response :)
    – Arkaedus
    Jul 28, 2014 at 17:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.