0

This might be a duplicate but i haven't found anyone with the exact same issue as me so thats why i made a post. I get the 'Input string was not in a correct format' error when ever i try to convert textbox text to int. To get the text from input textbox:

public void textBoxValue1_TextChanged(object sender, EventArgs e)
    {
        var textBoxValue1Text = sender as TextBox;
        string textBoxValue1ConvertedText = 
        System.Convert.ToString(textBoxValue1Text);
        value1txt = textBoxValue1ConvertedText;
    }

And to convert it:

string search1value = FormParameters.value1txt;
int search1ValueInt = int.Parse(FormParameters.value1txt); // Error occurs here

What am i doing wrong? Thanks in advance

  • 1
    What is the contet of `FormParameters.value1txt` when you debug? Not an int I guess. – Mighty Badaboom Apr 04 '19 at 12:41
  • Ive only allowed numbers to be entered in the textbox, so ive enter for example "1" and I get "System.Windows.Forms.TextBox, Text: 1". –  Apr 04 '19 at 12:44
  • What is the value of `search1value` during runtime? – Oshi Apr 04 '19 at 12:44
  • Before giving it a value its 0 –  Apr 04 '19 at 12:46
  • Possible duplicate of [How do I make a textbox that only accepts numbers?](https://stackoverflow.com/questions/463299/how-do-i-make-a-textbox-that-only-accepts-numbers) – Owen Pauling Apr 04 '19 at 12:50

1 Answers1

0

The problem is in this method

public void textBoxValue1_TextChanged(object sender, EventArgs e)
{
    var textBoxValue1Text = sender as TextBox;
    string textBoxValue1ConvertedText = System.Convert.ToString(textBoxValue1Text);
    value1txt = textBoxValue1ConvertedText;
}

textBoxValue1Text is not the text of the TextBox; it is the Textbox itself.

Use

string textBoxValue1ConvertedText = System.Convert.ToString(textBoxValue1Text.Text);

instead.

When you convert the TextBox to string you're calling the .ToString() of the TextBox and not the value of the Text property.

Mighty Badaboom
  • 6,067
  • 5
  • 34
  • 51