I want to implement a code that take my inserted values from a listBox and show the as Prime numbers in textbox. Unfortunately, my textbox appear only the index of my prime number.
private void primnr()
{
int n = listBox1.Items.Count;
for(int i=2; i<=n; i++)
{
bool prim = true;
for (int j=2; j <i/2; j++)
{
if (i % j == 0)
{
prim = false;
break;
}
}
if (prim)
textBox2.Text = textBox2.Text + "Numerele prime:" +listBox1.Items[i].ToString() + Environment.NewLine ;
}
}
Assuming that you have numbers in your Combo items, you should access the item in that index position, in your case “i”
if (prim) textBox2.Text = textBox2.Text + "Numerele prime:" + listBox1.Items[i].ToString() + Environment.NewLine;
when I put this line textBox2.Text = textBox2.Text + "Numerele prime:" + listBox1.Items[i] + Environment.NewLine;
it shows this error:System.ArgumentOutOfRangeException: 'InvalidArgument=Value of '2' is not valid for 'index'. Parameter name: index'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.