Search code examples
c#winformsjson.netflowlayoutpanel

How can I add FlowLayoutPanel and PictureBox in TabPage?


How do I add flowlayout panel and picturebox to tab page? I want to add all image urls to picturebox and picturebox list added to flow layout and also added to tabpage:

enter image description here

This my code

List<FlowLayoutPanel> flows = new List<FlowLayoutPanel>();
FlowLayoutPanel flow = new FlowLayoutPanel();
List<TabPage> tp = new List<TabPage>();
foreach (var item in edu.data)
{
    flow.BackColor = Color.DarkGray;
    flow.Dock = DockStyle.Fill;
    flow.Tag = item.content;
    flow.AutoScroll = true;
    flow.Name = item.category;
    foreach (var item2 in item.content)
    {
        PictureBox pb = new PictureBox();
        pb.Size = new Size(170, 130);
        pb.SizeMode = PictureBoxSizeMode.Zoom;
        pb.LoadAsync("https://ytimg.googleusercontent.com/vi/" + item2.urlYutub.Split('=')[1] + "/hqdefault.jpg");
        flow.Controls.Add(pb);
    }
    flows.Add(flow);
}
foreach (var flo in flows)
{
    TabPage tp2 = new TabPage();
    tp2.Text = flo.Name;
    tp2.Controls.Add(flo);
    tp.Add(tp2);
}
for (int index = 0; index < tp.Count; index++)
{
    tabEducation.TabPages.Add(tp[index]);
}

Solution

  • Try this

    foreach (var item in edu.data)
    {
        FlowLayoutPanel flow = new FlowLayoutPanel();
        flow.BackColor = Color.DarkGray;
        flow.Dock = DockStyle.Fill;
        flow.Tag = item.content;
        flow.AutoScroll = true;
        flow.Name = item.category;
        foreach (var item2 in item.content)
        {
            if (item.category == flow.Name)
            {
                PictureBox pb = new PictureBox();
                pb.Size = new Size(170, 130);
                pb.SizeMode = PictureBoxSizeMode.Zoom;
                pb.LoadAsync("https://ytimg.googleusercontent.com/vi/" + item2.urlYutub.Split('=')[1] + "/hqdefault.jpg");
                flow.Controls.Add(pb);
            }
        }
        TabPage tp = new TabPage();
        tp.Text = flow.Name;
        tp.Controls.Add(flow);
        tabEducation.TabPages.Add(tp);
    }