String to int, Visual C# 2005

Hey,

How do you make the value of this:

pictureBox1.Width = (maskedTextBox1.Text);

pictureBox1.Height = (maskedTextBox2.Text);

an integer

becuase I get an error saying can not convert string to int...

 

 

Thanks :)




Answer this question

String to int, Visual C# 2005

  • abhas

    i was just going to point that one out - good job.

    everytime you have a checkbox ticked - the event gets fired from which you are ticking another box - that event gets fired setting the value of another checkbox...going back to the first checkbox tick event...and so on - causing a loop of ticking going on overflowing the stack.

    logic is incorrect



  • patricktuan

    ok here it is:

    private void radioButton1_CheckedChanged(object sender, EventArgs e)

    {

    pictureBox1.Width = 16;

    pictureBox1.Height = 16;

    }

    private void radioButton2_CheckedChanged(object sender, EventArgs e)

    {

    pictureBox1.Width = 20;

    pictureBox1.Height = 20;

    }

    private void radioButton3_CheckedChanged(object sender, EventArgs e)

    {

    pictureBox1.Width = 32;

    pictureBox1.Height = 32;

    }

    private void radioButton4_CheckedChanged(object sender, EventArgs e)

    {

    pictureBox1.Width = 64;

    pictureBox1.Height = 64;

    }

    private void radioButton5_CheckedChanged(object sender, EventArgs e)

    {

    pictureBox1.Width = Convert.ToInt32(maskedTextBox1.Text);

    maskedTextBox1.ReadOnly = false;

    maskedTextBox2.ReadOnly = false;

    pictureBox1.Height = Convert.ToInt32(maskedTextBox2.Text);

    }

    private void timer1_Tick(object sender, EventArgs e)

    {

    if (radioButton5.Checked == false)

    {

    if (maskedTextBox1.Text == null)

    {

    maskedTextBox1.Text = "100";

    }

    if (maskedTextBox2.Text == null)

    {

    maskedTextBox2.Text = "100";

    }

    maskedTextBox1.ReadOnly = true;

    maskedTextBox2.ReadOnly = true;

    }

    }

    }



  • TimJ186

    Hi,
    Yeah, I can reproduce the problem and it does say that it's FormatException problem. The reason you got the error is that you forgot to use the try... catch ... in the radiobutton5_checkedchanged event handler. Besides, this event fires when you CHECK the radiobutton5 and UNCHECK it. That is to say, when you check the radiobutton4, the radiobutton5_checkedchanged event fires as well as the readiobutton4_checkedchanged event fires. Therefore you should check if the current state is checked or not. It should look something like this:


    private void radioButton5_CheckedChanged(object sender, EventArgs e)
    {
    if (radioButton5.Checked == true)
    {
    try
    {
    pictureBox1.Width = Convert.ToInt32(maskedTextBox1.Text);
    maskedTextBox1.ReadOnly = false;
    maskedTextBox2.ReadOnly = false;
    pictureBox1.Height = Convert.ToInt32(maskedTextBox2.Text);
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message);
    }
    }
    }





  • DaveKStl

    Hi,
    If you missed the first chance(one tick) to write the number in the two mask text box, you'll have to check the radiobutton5(throws format exception), input the number in mask text box and re-check the radiobutton5.


  • THE RAZI

    lol,

    ok guys, ya I kinda forgot that when using radio buttons only one can be selected

     

     

     

    Thanks guys :)

     

    Edit: There seems to be another odd problem I dotn know about...I tryed doing a few things to try and fix it but nothing different happens...When I test out the Custom radio button, the masked textbox buttons become active and I when I swich to another radio button when there is no text int he masked textbox, then I get a runtim error with the program.cs with this code:

    Application.Run(new Form1());

    and it says input string was not the correct format...

     

     

    Thanks :)



  • Thomas Greenleaf

    Glad to know you worked it out!

  • Kybalion

    ok, thanks :)

    It works, so aparently if there are no numbers in the textbox then it will give that message, but it looks good now that it does the error message box thing :)

    Thanks :)



  • moff

    When I test out the Custom radio button, the masked textbox buttons become active and I when I swich to another radio button when there is no text int he masked textbox, then I get a runtim error with the program.cs with this code:
    Can you show the code related to the problem The only statement Application.Run is of course not the core of the problem.


  • TopDean

    Hi,
    To be more specific, the problem lies in the fact you manually set the radioButton's state to true or false. Actually you don't need to do that. Several radioButtons in a container only allows just one being selected. Therefore your code should be something like this:


    private void radioButton3_CheckedChanged(object sender, EventArgs e)
    {

    if (radioButton3.Checked == true)
    {
    label1.Text = "three";
    }
    }

    private void radioButton4_CheckedChanged(object sender, EventArgs e)
    {

    if (radioButton4.Checked == true)
    {
    label1.Text = "four";
    }
    }

    private void radioButton5_CheckedChanged(object sender, EventArgs e)
    {

    if (radioButton5.Checked == true)
    {
    label1.Text = "five";
    }
    }

     

    Change the label.Text to pictureBox statement, or whatever you want.


  • Olavo

    Hi,
    It doesn't matter which line is highlighted when the stack is overflowed. It just happens to be this line of code. The problem is that your way of processing is wrong. The event CheckedChanged applies not only to the mouse select but also the programmatical set. Therefore when radioButton1 is selected, you set the radioButton2 to false. As the same time, the event CheckedChanged of radioButton2 is triggered, and it tries to set the radioButton2 to false. ... That's why you see they're selected back and forth. One solution is you can use just one event handler to handle the 5 events. And in this handler you check which radioButton is selected and do the proper work.


  • MattWoberts

    this.pictureBox1.Width = Convert.ToInt32(maskedTextBox1.Text);

    this.pictureBox1.Width = Convert.ToInt32(maskedTextBox2.Text);

    best practice:

    be sure to check the values in the textbox are actually a numeric value otherwise you will get an argumentexception of some sort.



  • Amjath

    Ok,

    thanks it worked...but now there is another problem...that has to o with the imagebox...

    So there are 5 radio buttons, and this is their code:

    private void radioButton1_CheckedChanged(object sender, EventArgs e)

    {

    if (radioButton1.Checked == false)

    {

    radioButton1.Checked = true;

    pictureBox1.Width = 16;

    pictureBox1.Height = 16;

    if (radioButton2.Checked == true)

    {

    radioButton2.Checked = false;

    }

    else

    if (radioButton3.Checked == true)

    {

    radioButton3.Checked = false;

    }

    else

    if (radioButton4.Checked == true)

    {

    radioButton4.Checked = false;

    }

    else

    if (radioButton5.Checked == true)

    {

    radioButton5.Checked = false;

    }

    }

    }

    private void radioButton2_CheckedChanged(object sender, EventArgs e)

    {

    if (radioButton2.Checked == false)

    {

    radioButton2.Checked = true;

    pictureBox1.Width = 20;

    pictureBox1.Height = 20;

    if (radioButton1.Checked == true)

    {

    radioButton1.Checked = false;

    }

    else

    if (radioButton3.Checked == true)

    {

    radioButton3.Checked = false;

    }

    else

    if (radioButton4.Checked == true)

    {

    radioButton4.Checked = false;

    }

    else

    if (radioButton5.Checked == true)

    {

    radioButton5.Checked = false;

    }

    }

    }

    private void radioButton3_CheckedChanged(object sender, EventArgs e)

    {

    if (radioButton3.Checked == false)

    {

    radioButton3.Checked = true;

    pictureBox1.Width = 32;

    pictureBox1.Height = 32;

    if (radioButton1.Checked == true)

    {

    radioButton1.Checked = false;

    }

    else

    if (radioButton2.Checked == true)

    {

    radioButton2.Checked = false;

    }

    else

    if (radioButton4.Checked == true)

    {

    radioButton4.Checked = false;

    }

    else

    if (radioButton5.Checked == true)

    {

    radioButton5.Checked = false;

    }

    }

    }

    private void radioButton4_CheckedChanged(object sender, EventArgs e)

    {

    if (radioButton4.Checked == false)

    {

    radioButton4.Checked = true;

    pictureBox1.Width = 32;

    pictureBox1.Height = 32;

    if (radioButton1.Checked == true)

    {

    radioButton1.Checked = false;

    }

    else

    if (radioButton2.Checked == true)

    {

    radioButton2.Checked = false;

    }

    else

    if (radioButton3.Checked == true)

    {

    radioButton3.Checked = false;

    }

    else

    if (radioButton5.Checked == true)

    {

    radioButton5.Checked = false;

    }

    }

    }

    private void radioButton5_CheckedChanged(object sender, EventArgs e)

    {

    if (radioButton5.Checked == false)

    {

    radioButton5.Checked = true;

    pictureBox1.Width = Convert.ToInt32(maskedTextBox1.Text);

    pictureBox1.Height = Convert.ToInt32(maskedTextBox2.Text);

    if (radioButton1.Checked == true)

    {

    radioButton1.Checked = false;

    }

    else

    if (radioButton2.Checked == true)

    {

    radioButton2.Checked = false;

    }

    else

    if (radioButton4.Checked == true)

    {

    radioButton4.Checked = false;

    }

    else

    if (radioButton3.Checked == true)

    {

    radioButton3.Checked = false;

    }

    }

    }

    So when I run the program, and press a radio button then the one that is alreay checked goes to unchecked, good, and then the one that is now selected goes to checked, good, but then the program freezes and the 2 radio buttons start going crazy and the image box keeps swiching the sizes back an forth to..., they both start swiching back and foth, uncheck and checked, and then it gives me a runtime error saying:

    An unhandled exception of type 'System.StackOverflowException' occurred in System.Windows.Forms.dll

    and this line of code is highlighted in green:

    radioButton1.Checked = true;

    under the private void radioButton1_CheckedChanged(object sender, EventArgs e) event...

    Thanks :)



  • String to int, Visual C# 2005