0

I'm trying to populate some labels and trackbars inside a groupBox. I'm using a List to define the count of them. But i'm having trouble to placing them correctly. This form will be a simple poll about personnel and when i select a personel from comboBox, questions will be pulled from a csv file and stored in a List to use in several parts of program.

Here's my current code:

private void InitializeUI()
    {
        pList = Database_IO.ParsePersonelData();
        fList = Database_IO.ParseFactorData();

        foreach (Personel p in pList)
        {
            personelCB.Items.Add(p.personelName + " " + p.personelSurname);
        }

        List<Label> labels = new List<Label>();
        List<TrackBar> tBars = new List<TrackBar>();
        foreach (Factor f in fList)
        {
            Label l = new Label
            {
                Text = f.factorName
            };
            labels.Add(l);
            this.Controls.Add(l);
            l.Parent = evalGroup;


            TrackBar t = new TrackBar();
            tBars.Add(t);
            this.Controls.Add(t);
            t.Parent = evalGroup;
        }
    }

And this is what i'm trying to do. Dynamically. How can i achieve this?

And this is what i'm trying to do. Dynamically.

1 Answer 1

1

this.Controls.Add(t) adds trackbar to Form container. You should rather add it to groupBox container:

evalGroup.Controls.Add(t)

You also have to place them accordingly using Location property and DockStyle

2
  • Ah thank you! As you can see, i was desperately trying to make those children of groupBox (but failed terribly). But i still need to fix Dock and Location. There was Grid Layout in Java, is there anything similar in c#? So i can easily place them.
    – Baycosinus
    Commented Sep 9, 2017 at 17:41
  • I suggest TableLayoutPanel, it's a bit tricky in the beginning to use it, but it gives all the positioning control you need. Commented Sep 9, 2017 at 17:57

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.