views:

957

answers:

7

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

Could someone please check my code below. 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();
    }

I need some feedback. Is my code OK?

+14  A: 
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
hi Mehrdad, I'm not sure how to do this in my code. Maybe you could help me. Thanks
tintincute
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
Hi Mehrdad this is just a test program for learning. Thanks for the advice
tintincute
+1  A: 

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

smok1
+3  A: 

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.

ck
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"?
tintincute
Edited to include feedback
ck
what's a customValidator?
tintincute
Is it a method? How would it look like?
tintincute
A: 

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
tintincute
A: 

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
A: 

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
A: 

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