I am writing code for a school exercise and I have the following code:
private void button1_Click(object sender, EventArgs e)
{
List<int> lista = new List<int>();
int delivi = 0;
int brojac = 0;
listBox1.BeginUpdate();
foreach (string s in listBox1.Items)
{
int broj = int.Parse(s);
int delenje_so = int.Parse(textBox1.Text);
if ((broj % delenje_so) == 0)
{
lista.Add(brojac);
delivi++;
}
brojac++;
}
for (int i = 0; i < lista.Count; i++)
{
MessageBox.Show(lista[i].ToString());
}
listBox1.EndUpdate();
label1.Text = delivi.ToString();
}
Basically, I have a ListBox, Button, TextBoxand a Label. I have some elements in ListBox, and I have to check if these elements ( ints) can be divisible by number in TextBox. Then select all the elements that can be divided by ListBoxand print the total number of numbers that can be divided by Label.
Everything works in my code, except that ListBoxit will not select shared elements.
foreach, , , . , - .