0

I have a problem with my textbox. I wanted that one can manually set the interval of the x- and y-axis for a chart in the GUI over two textboxes. That works but when I type a char in or when I typed an int in and delete it, the program crashes immediately and I get a System.FormatException (without clicking the button to accept the changes). How can I solve it that one can just type in different signs without immediately crashing the program? My code below:

public void textBox2_TextChanged(object sender, EventArgs e)
{
     x_axis_num = Convert.ToInt32(xAxisBox.Text, usC);
}

private void yAxisBox_TextChanged(object sender, EventArgs e)
{
    y_axis_num = Convert.ToInt32(yAxisBox.Text);
} 

That gets passed to another event:

chart1.ChartAreas[0].AxisX.Interval = x_axis_num;
chart1.ChartAreas[0].AxisY.Interval = y_axis_num;
İsmet Alkan
  • 5,361
  • 3
  • 41
  • 64
uzi42tmp
  • 271
  • 2
  • 9
  • 22
  • 1
    possible duplicate of [How do I make a textbox that only accepts numbers?](http://stackoverflow.com/questions/463299/how-do-i-make-a-textbox-that-only-accepts-numbers) – Shaharyar Nov 05 '14 at 06:50
  • show what event assigns the chart intervals. @Shaharyar link will help you for the character part but since you are concerned with backspaces as well you have to validate that on your assignment event – TheProvost Nov 05 '14 at 06:50
  • Its the event which occurs when I click the button to draw the graph. So when I change the axis intervals and click the button it works fine. How do I validate that? – uzi42tmp Nov 05 '14 at 06:53
  • @uzi42tmp Problem is you enter something other than `int` which through an unhanded `FormatException` which means it can not parse that value to `int`. – Shaharyar Nov 05 '14 at 06:55
  • You said the program crashes immediately after typing a character so i thought the event was running always. Then shaharyar is right you can get your answer in his link. – TheProvost Nov 05 '14 at 06:56

1 Answers1

3

In the line x_axis_num = Convert.ToInt32(xAxisBox.Text, usC);, you are taking whatever is in the text box and try to convert it to an integer value.

What do you think the conversion of "Hey, I'm not a number!" will do? It will crash horribly, basically because that text is not, and never will be, a number.

Instead, you can use the Int.TryParse method which will take any text and TRY to convert it to a number.

If the conversion is successful, then no problem. If it was not successful, then you get a false value on a flag indicating the text could not be converted.

Example:

int number;

bool result = Int32.TryParse(YourTextBox.Text, out number);

If the conversion is successful, then number has the value, otherwise, result is false so do something like this then:

if(result)
{
    xAxisBox.Text = number.ToString();
    x_axis_num = number;
}
else
{
    xAxisBox.Text = string.Empty;

    // Be careful here with what you set. 
    // This is the value you will set when the Text box has a non numeric value!
    x_axis_num = 0;    
}
Nahuel Ianni
  • 3,177
  • 4
  • 23
  • 30
  • He tells me that the TryParse method has not legal values when I write it that way - EDIT: sry had an error in it – uzi42tmp Nov 05 '14 at 07:02
  • In which event do you propose he place this? – TheProvost Nov 05 '14 at 07:06
  • I would say into the the both x-axis and y-axis events? or am I wrong? I get another error in "xAxisBox.Text = number;" - he tells me that a convertion of int to string isn't possible. – uzi42tmp Nov 05 '14 at 07:07
  • @uzi42tmp You are right and I was missing a ToString on the number when assigning it to the text property. I updated the answer. – Nahuel Ianni Nov 05 '14 at 07:08
  • Wait a minute! Now he doesn't load the values in! When I click the button to change the axis he does nothing :/ – uzi42tmp Nov 05 '14 at 07:51
  • Did you assign the number value to your x_axis_num and y_axis_num variables after setting it to the textbox? – Nahuel Ianni Nov 05 '14 at 07:53
  • How to do that exactly? now I changed num to num_x and num_y and when I try to assign he sais I can't convert bool to string even when I try it - EDIT: Ok it was my fault! Now it works correctly! Thanks again :) – uzi42tmp Nov 05 '14 at 08:00
  • Updated the answer to show you the full correct code :) – Nahuel Ianni Nov 05 '14 at 08:10