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();
    }

7 Answers 7

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
3
  • hi Mehrdad, I'm not sure how to do this in my code. Maybe you could help me. Thanks May 23, 2009 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. May 23, 2009 at 18:05
  • Hi Mehrdad this is just a test program for learning. Thanks for the advice May 24, 2009 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.

2
  • this works ck. but the problems is: if I type characters it will not give me any message or there's no error. Is that also possible to integrate here the "try and catch"? May 20, 2009 at 12:35
  • Is it a method? How would it look like? May 23, 2009 at 16:38
1

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

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().

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.

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);
-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);
   }
}
1
  • 1
    thanks Pat, I tried this I'm getting an error: "cant apply operator '==' to operands of type 'string' & 'int' May 20, 2009 at 12:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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