hkguile
hkguile

Reputation: 4359

Are there any ways to align a textbox vertically middle within a panel?

I have a panel, i want to align it vertically middle within a panel, the pic shows what i want

enter image description here

Any one know how to do that?

Upvotes: 0

Views: 651

Answers (2)

Sean
Sean

Reputation: 398

Align a control in parent vertically:

InsidePanel.Location = new Point(
    0, (OutsidePanel.Height - InsidePanel.Height) / 2
);

Align a control in parent horizontally:

InsidePanel.Location = new Point(
    (OutsidePanel.Width - InsidePanel.Width) / 2, 0
);

If you do have form resizing, you will need to make sure this is added to you resize event handler to make sure the controls stay centered.

Upvotes: 1

CodingYoshi
CodingYoshi

Reputation: 27009

You can use a TableLayoutPanel control.

  1. Drag a TableLayoutPanel onto your form
  2. Set the Dock property to Fill
  3. Remove one of the columns from the Columns collection because by default you will get 2 columns.
  4. Add one more Row to your panel because by default you will get 2.

Then:

  1. Put the textbox into the second row.
  2. Set the Dock property to Fill

enter image description here

Set the sizes for the rows for the TableLayoutPanel as shown in the following screenshot:

enter image description here

Upvotes: 0

Related Questions