Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Windows Form Application

How do I create two labels in the flowLayoutPanel?

I want them to be the first and last control in flowLayoutPanel so that any controls added won't affect their positions.

Thanks for your help.
Posted
Updated 21-Dec-10 0:38am
v2
Comments
Sergey Alexandrovich Kryukov 22-Dec-10 0:46am    
You already have one answer, buy I don't think you got any effective help. Why?

Are you sure this is an appropriate question? First, you claim your decision to use flow layout, and then ask how you can controls with labels. Of course you can, but where those labels will "flow" is solely your responsibility. And probably you tried and still unhappy. But what if you decision was wrong in first place (I'm pretty much sure it is: controls with labels and floating format are from very different styles).

This is not the way to get help. You better ought to start with description how your control layout should look and behave (on resize, essentially) and only after that mentioned what were your implementation ideas. This way, we would be able to figure out how to help you.
tuolesi 22-Dec-10 10:25am    
I already have 2 answer,and i don't think that they got no effective help.Why?

Are you sure this is an appropriate comment?First,my question title is flow layout panel,and then ask how to set some of its child control to be able stay at first and the last index position no matter how many new controls added to the flowlayoutpanel.(i'm pretty much sure that:when i said labels in flowlayoutpanel,one should understand it is a child control and any others control are just from very similar styles)

This is not the way to help.You better ought to skip this question and start to help others people.This way,you would be able to figure out how to help people.

You can't, FlowLayoutPanel doesn't work that way.

What you could do is write a custom AddMethod that takes the last item and removes it and readds it, something like this:

C#
private static void AddStuff(FlowLayoutPanel panel, Control control)
{
    Control last = panel.Controls[panel.Controls.Count - 1];
    panel.Controls.Remove(last);
    panel.Controls.Add(control);
    panel.Controls.Add(last);
}


Or something like it.

Hope this helps,
Fredrik Bornander
 
Share this answer
 
Comments
tuolesi 22-Dec-10 10:26am    
Great,simple and works
Try this (SetChildIndex does the trick)

using System;
using System.Windows.Forms;
namespace FlowLayoutPanelTest
{
    static class Program
    {
        static void Main()
        {
            Application.Run(new TestForm());
        }
        class TestForm : Form
        {
            public TestForm()
            {
                FlowLayoutPanel flowlayoutpanel = new FlowLayoutPanel();
                flowlayoutpanel.Dock = DockStyle.Fill;
               
                Button buttonAddControl = new Button();
                buttonAddControl.Text = "Add Control";
                buttonAddControl.Dock = DockStyle.Bottom;
                buttonAddControl.Click += delegate(object obj, EventArgs ea)
                {
                    AddControlToFlowLayoutPanel(flowlayoutpanel, new Button()); // add any control
                };
                Label labelFirst = new Label();
                labelFirst.Text = "First";
                Label labelLast = new Label();
                labelLast.Text = "Last";
                flowlayoutpanel.Controls.Add(labelFirst);
                flowlayoutpanel.Controls.Add(labelLast);
                Controls.Add(flowlayoutpanel);
                Controls.Add(buttonAddControl);
            }
            void AddControlToFlowLayoutPanel(FlowLayoutPanel flowlayoutpanel, Control ctrl)
            {
                flowlayoutpanel.Controls.Add(ctrl);
                // SetChildIndex does the trick
                if (flowlayoutpanel.Controls.Count > 2)
                    flowlayoutpanel.Controls.SetChildIndex(ctrl, flowlayoutpanel.Controls.Count - 2);
            }
        }
    }
}
 
Share this answer
 
Comments
tuolesi 22-Dec-10 10:25am    
Thanks for help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900