How do I right align controls in a StatusStrip?

2019-01-31 21:38发布

I am trying to right align a control in a StatusStrip. How can I do that?

I don't see a property to set on ToolStripItem controls that specifies their physical alignment on the parent StatusStrip.

How do I get Messages drop down to be right aligned? http://i.friendfeed.com/ed90b205f64099687db30553daa79d075f280b90

8条回答
forever°为你锁心
2楼-- · 2019-01-31 22:10

You can display the Button at the end of the StatusStrip by using the logic below.

  1. Add a ToolstripLabel to the StatusStrip
  2. Set text as string.Empty
  3. Set Padding for the ToolstripLabel

For example:

this.toolStripStatusLabel1.Padding = new Padding((int)(this.Size.Width - 75), 0, 0, 0);
查看更多
我命由我不由天
3楼-- · 2019-01-31 22:17

I find a general way to set a control's location in StatusStrip. You can display the Button at any position in the StatusStrip by using the steps below.

  1. Add a ToolstripLabel to the StatusStrip
  2. Set text as a suitable amount of space like

    toolStripStatusLabel1.Text = "   ";

  3. If layout is not what you want, got to step 2 to change the amount of space in toolStripStatusLabel1.Text, else work has been done.

查看更多
神经病院院长
4楼-- · 2019-01-31 22:18

Found it via MSDN forums almost immediately after posting :)

You can use a ToolStripLabel to pseudo right align controls by setting the Text property to string.Empty and setting the Spring property to true. This will cause it to fill all of the available space and push all the controls to the right of the ToolStripLabel over.

查看更多
干净又极端
5楼-- · 2019-01-31 22:18

Set the RightToLeft tool strip property to True.

查看更多
贼婆χ
6楼-- · 2019-01-31 22:24

For me it took two simple steps:

  1. Set MyRightIntendedToolStripItem.Alignment to Right
  2. Set MyStatusStrip.LayoutStyle to HorizontalStackWithOverflow
查看更多
祖国的老花朵
7楼-- · 2019-01-31 22:27

As an added note this is due to the fact that in the Win32 API a cell is either fixed width or fills the remaining space -1

int statwidths[] = {100, -1};

SendMessage(hStatus, SB_SETPARTS, sizeof(statwidths)/sizeof(int), (LPARAM)statwidths);
SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Hi there :)");

If memory serves me correctly you can have only one fill cell (-1) per statusbar.

You could also add a third middle cell and give this the fill property to get a more concistent looking StatusBar. Consistent because Messages has an inset to its left right where you'd expect it. A bit like the mspaint shot found on the MSDN page for StatusBars

I like the creative appreach though :D

查看更多
登录 后发表回答