3

I need help with my code. I would like to write only numbers/integers in my textbox and would like to display that in my listbox.

Is my code below in order? This seems to give an error.

    int yourInteger;
    string newItem;

    newItem = textBox1.Text.Trim();

    if (newItem == Convert.ToInt32(textBox1.Text))
    {
        listBox1.Items.Add(newItem);
    }

==== Update:

This is how my code looks like now. My question is, can listBox handle the data type "long"? Because when I entered the number 20,000,000 I just got an hour glass for 20 minutes. But when I tried this one with the console, I got the answer. So I'm not sure what kind of element can handle data type "long".

    string newItem;
    newItem = textBox1.Text.Trim();

    Int64 num = 0;
    if(Int64.TryParse(textBox1.Text, out num))
    {
        for (long i = 2; i <= num; i++)
        {
            //Controls if i is prime or not
            if ((i % 2 != 0) || (i == 2))
            {
                listBox1.Items.Add(i.ToString());
            }

        }
    }


    private void btnClear_Click(object sender, EventArgs e)
    {
        listBox1.Items.Clear();
    }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
tintincutes
  • 5,618
  • 25
  • 67
  • 86

7 Answers7

14
int result = int.Parse(textBox1.Text.Trim());

If you want to check for validity:

int result;
if (int.TryParse(textBox1.Text.Trim(), out result)) // it's valid integer...
   // int is stored in `result` variable.
else
   // not a valid integer
Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
  • hi Mehrdad, I'm not sure how to do this in my code. Maybe you could help me. Thanks – tintincutes May 23 '09 at 16:26
  • It's not an issue with long. You are doing a very time and memory consuming operation. Writing to console doesn't require updating a GUI object with lots of elements. Basically, you are displaying millions of elements in a listbox which doesn't have any real use (who can scroll through such a long list?) and consumes lots of resources. – Mehrdad Afshari May 23 '09 at 18:05
  • Hi Mehrdad this is just a test program for learning. Thanks for the advice – tintincutes May 24 '09 at 20:17
3

Use this:

    int yourInteger;
    string newItem;

    newItem = textBox1.Text.Trim();
    Int32 num = 0;
    if ( Int32.TryParse(textBox1.Text, out num))
    {
        listBox1.Items.Add(newItem);
    }
    else
    {
        customValidator.IsValid = false;
        customValidator.Text = "You have not specified a correct number";
    }

This assumes you have a customValidator.

cjk
  • 45,739
  • 9
  • 81
  • 112
1

Use int.TryParse() to check if string contains integer value.

smok1
  • 2,940
  • 26
  • 35
0

textBox1.Text may not contain a valid string representation of an integer (or is just an empty string). To work around that, use Int32.TryParse().

Anton Gogolev
  • 113,561
  • 39
  • 200
  • 288
0

You can do:

Convert.ToInt32(input);

For a longer function using this you can look at: http://msdn.microsoft.com/en-us/library/bb397679.aspx

Basically it checks if the string is null, then it will call int.Parse. This will work under WindowsCE also, which doesn't have int.TryParse.

James Black
  • 41,583
  • 10
  • 86
  • 166
0

To be specific as to why your code fails to compile it is because you are comparing a string (newItem) against the result of Convert.ToInt32, which is an integer, which it wont let you do. Also Convert.ToInt32 will raise an exception it the string passed in is not a number.

You can try using int.TryParse, or alternatively write a simple regular expression to validate your input:

int i;
bool isInteger = int.TryParse(textBox1.Text,out i);

or

bool isInteger = System.Text.RegularExpressions.Regex.IsMatch("^\d+$", textBox1.Text);
samjudson
  • 56,243
  • 7
  • 59
  • 69
-1

Are you checking for an empty string?

int yourInteger;
string newItem;
newItem = textBox1.Text.Trim();

if(newItem != string.Empty)
{
   if ( newItem == Convert.ToInt32(textBox1.Text))
   {
      listBox1.Items.Add(newItem);
   }
}
Pat
  • 5,263
  • 1
  • 36
  • 53
  • 1
    thanks Pat, I tried this I'm getting an error: "cant apply operator '==' to operands of type 'string' & 'int' – tintincutes May 20 '09 at 12:34