1

I have a project for which I want to register which members of a group has attended a meeting on a certain date.

The functionality I want is:

  • You choose which group had a meet in a listbox.
  • All the members of the group show in another listbox and they each have a checkbox.
  • You mark each member who attended by clicking in the checkbox.
  • You select a date from calendar-view.
  • I then want to send each ID of the people attended to a SQL-query loop that inserts this into the DB.

My problem is that I'm stuck trying to create checkboxes dynamically for each object "member" in my List. Is there a smart way to do this?

I figure that I will then loop through each selected checkbox in the group of checkboxes and select the member-id from each member checkbox.checked and pass this to the SQL-query.

All input is welcome as I'm kinda stuck here. :-)

3
  • 4
    For us to help you we'll need some more informations : What code have you already tried ? Provide a Minimal, Complete, and Verifiable example. Which technology are you using (WinForms, WPF, ASP.NET, another one ?) ? Commented Jun 16, 2016 at 8:31
  • Sorry forgot to mention I use Win forms for this. :-) I Am stuck after getting all "member" objects of a group to a listbox. So I need to figure out some kind of method that adds a checkbox for each item added in that listbox. Commented Jun 16, 2016 at 9:02
  • Why not use a CheckedListBox or a ListView with CheckBoxes? Commented Jun 16, 2016 at 9:07

2 Answers 2

3

I solved this by using a CheckedListBox like Sidewinder94 suggested, thanks!

First method to fill the checkbox with objects from my people list:

    public void FillCheckBox(List<person> listan)
    {
        checkedListBox1.Items.Clear();
        foreach (person item in listan)
        {
            checkedListBox1.Items.Add(item, true);
        }
    }

I then iterate through all the Checked items and Query my DB with insert for all checked items by using the "checkedListBox1.CheckedItems" collection.

    private void button1_Click(object sender, EventArgs e)
    {
        postgresConnection _con = new postgresConnection();

        group va = (group)comboGrupper.SelectedItem;

        int index = va.gruppid;

        foreach (person item in checkedListBox1.CheckedItems)
            {
                _con.AddPeopleAttendance(item.personid, index);
            }
    }

Thanks for the help! :-)

Sign up to request clarification or add additional context in comments.

2 Comments

That's nice of you to post the solution to your problem :) That may help someone else in the future.
Hopefully it will! Thanks for the help, it was just the push I needed to go forward!= )
0

Assuming you are using WPF you could make UserControls for each member of that meeting. If you do so, a simple checkbox inside the UserControl should be enough. If you store the UCs in a List or an Array you can circle through the collection and if the checkbox is checked add it to a secondary List/Array that stores the attendet persons.

public class PersonUC : UserControl
{
   public Person;
   public PersonUC(Person p)
   {
    Person = p;
   }
}

public class MainWindow
{
    private List<PersonUC> personUCs;

    private void MethodCalledAfterEventSelected(List<Person> p)
    {
        //circle through the collection to add the UCs to your listbox
        personUCs = new List<PersonUC>();
        foreach(var item in p)
        {
            personUCs.Add(new PersonUC(item));
        }
        yourListBox.Items = personsUC;
    }

    private List<Person> GetAttendet()
    {
        List<Person> attendet = new List<Person>();
        foreach (PersonUC item in personsUC) 
        {
            if(item.youCheckboxName.Checked)
                attendet.Add(item.Person);
        }
        return attendet;
    }
}

The code is not tested, so there might be a few errors, but i hope i could help.

2 Comments

Sorry forgot to mention I use Win forms for this. :-) I Am stuck after getting all "member" objects of a group to a listbox. So I need to figure out some kind of method that adds a checkbox for each item added in that listbox.
You are overcomplicating things. Try to read about ItemTemplate in a ListBox, it exists for these kinds or purpose (you should always try to have as few code-behind as possible in WPF).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.