P.Brian.Mackey
P.Brian.Mackey

Reputation: 44275

How to display two Label controls side by side in a panel that is docked top?

I have a Panel object called dockTop. I add two labels to said Panel. I want the labels to to flow from left to right. This is akin to what one expect's out of a div with css set to float: left...only this is winforms.

I have

   //I want DockStyle.Left, but that causes both controls to be of height, width = 0,0
   dockTop.Controls.Add(new Label() { Text = "one", Dock = DockStyle.Top });
   dockTop.Controls.Add(new Label() { Text = "two", Dock = DockStyle.Top }); 

Dock Top works fine, but that's not what I want. How can I display the labels next to eachother from left to right within a Panel that is set to Dock Top?

Upvotes: 1

Views: 5500

Answers (3)

spajce
spajce

Reputation: 7082

i use Resize event to achieve this task. i hope it will helps you.

static class Program
{
    static Label label1;
    static Label label2;
    static Form form1;
    static Rectangle rectForm;
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        form1 = new Form();
        rectForm = form1.ClientRectangle;
        Panel dockTopPanel = new Panel {Height = 100, Dock = DockStyle.Top, BackColor = Color.White };
        label1 = new Label { Text = "Label1", Dock = DockStyle.Left, BackColor = Color.Red, Width = rectForm.Width / 2 };
        label2 = new Label { Text = "Label2", Dock = DockStyle.Right, BackColor = Color.Blue, Width = rectForm.Width / 2 };
         label2.BringToFront();
        Control[] labels= {label1, label2};
        dockTopPanel.Controls.AddRange(labels);
        form1.Controls.Add(dockTopPanel);
        form1.Resize += new EventHandler(form1_Resize);
        Application.Run(form1);
    }

    static void form1_Resize(object sender, EventArgs e)
    {
        rectForm = form1.ClientRectangle;
        label1.Width = (rectForm.Width / 2) + 1;
        label2.Width = (rectForm.Width / 2) + 1;
    }
}

Upvotes: 0

LarsTech
LarsTech

Reputation: 81620

You would have to place them yourself by setting the location. If need be, set the Anchor properties appropriately. Alternatively, you could use a TableLayoutPanel instead of the Panel

Upvotes: 1

sa_ddam213
sa_ddam213

Reputation: 43596

You will have to set Dock to DockStyle.Top and DockStyle.Left

   dockTop.Controls.Add(new Label {Text = "one", Dock = DockStyle.Top | DockStyle.Left });
   dockTop.Controls.Add(new Label { Text = "two", Dock = DockStyle.Top | DockStyle.Left });

enter image description here

or you could use AutoSize with DockStyle.Left

   dockTop.Controls.Add(new Label {Text = "one", Dock = DockStyle.Left, AutoSize=true });
   dockTop.Controls.Add(new Label { Text = "two", Dock =  DockStyle.Left, AutoSize = true});

enter image description here

Upvotes: 0

Related Questions