Neo
Neo

Reputation: 16219

How to create dynamic checkboxes on Winform by passing checkbox name in List C#?

I'm working on excel add-in project c#.

want to create list of checkboxes with name which is passed in List , how can I create it with that pass named?

and want to take that check box selected value also.

Currently I have one button LoadEmployee when I clicked on that all EmployeeNames gets loaded into Excel sheet from Sql table directly

Now I want to do when I clicked on LoadEmployee it will open one popup with all employee names and every emplyee name has one check box , which one i select that employee name is populated only not other.

With the help of answer given by @Vajura I tried like

called popup form using

 new FilterDataForm().ShowDialog(mylist);

on popup form .cs

internal void ShowDialog(MyModel.ListEmployee> mylist)
    {
        GenerateFilterList(mylist);            
    }

 private void GenerateFilterList(List<ListEmployee> mylist)
        {
            System.Windows.Forms.CheckBox box;
            for (int i = 0; i < mylist.Count; i++)
            {
                box = new System.Windows.Forms.CheckBox();
                box.Tag = mylist[i];
                box.Text = mylisti].ToString();
                box.AutoSize = true;
                box.Location = new Point(10, i * 50); //vertical
                //box.Location = new Point(i * 50, 10); //horizontal
                this.Controls.Add(box);
            }
        }

butthen also not getting any popup :(

Check this screenshot i'm looking for output like this on popup Employee names will be displayed which is passed by using LISTenter image description here

Upvotes: 2

Views: 4547

Answers (1)

Vajura
Vajura

Reputation: 1132

You already have a list with the employe names? Then simply do this

private void Form1_Load(object sender, EventArgs e)
{
    this.Size = new Size(200, 200);
    List<string> yourList = new List<string>();
    yourList.Add("A");
    yourList.Add("B");
    yourList.Add("C");
    yourList.Add("D");
    yourList.Add("E");
    yourList.Add("F");
    CheckBox box;
    int innitialOffset = 20;
    int xDistance = 80;
    int yDistance = 50;

    for (int i = 0; i < yourList.Count; i++)
    {
        box = new CheckBox();
        box.Tag = yourList[i];
        box.Text = yourList[i].ToString();
        box.AutoSize = true;
        box.Location = new Point(innitialOffset + i % 2 * xDistance, innitialOffset + i / 2 * yDistance); 

        this.Controls.Add(box);
    }
}

Upvotes: 6

Related Questions