jeudi 27 septembre 2018

Why does this delete every other checkbox?

Rank noob again learning C# with Winforms. I was practicing adding checkboxes to a panel, and then removing them. The "start_button" button adds checkboxes. This works. The "remove_button" button is supposed to delete all of them. But it doesn't. By playing around with the # of checkboxes, I figured out that it removes every other checkbox. Another click and it removes every other of the remaining ones, and so on until they are all gone.

Why?

Thanks, Aram

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace playingWithPanels
{
public partial class Form1 : Form
{
    CheckBox chkBox;
    TextBox txtBox;
    public Form1()
    {
        InitializeComponent();
    }


    private void start_button_Click(object sender, EventArgs e)
    {
        txtBox = new TextBox();
        txtBox.BringToFront();
        txtBox.Text = "Textbox";
        txtBox.Location = new Point(30, 10);
        panel1.Controls.Add(txtBox);
        for (int i =0;i<20;i++)
        {
        chkBox = new CheckBox();
        chkBox.BringToFront();
        chkBox.Text = "Checkbox_" + i.ToString();
        chkBox.Name = "Checkbox_" + i.ToString();
            chkBox.AutoSize = true;
        chkBox.Location = new Point(30, 40 + 25 * i);
        panel1.Controls.Add(chkBox);
        }
    }

    private void remove_button_Click(object sender, EventArgs e)
    {
        foreach (var ctrl in panel1.Controls.OfType<CheckBox>())
        {
            panel1.Controls.Remove(ctrl);
        }
    }
}
}




Aucun commentaire:

Enregistrer un commentaire