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?