C# 如何正确对齐 StatusStrip 中的控件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/509508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 06:12:23  来源:igfitidea点击:

How do I right align controls in a StatusStrip?

c#winformsstatusstrip

提问by Eric Schoonover

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

我正在尝试在StatusStrip. 我怎样才能做到这一点?

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

我没有看到要在ToolStripItem控件上设置的属性来指定它们在 parent 上的物理对齐方式StatusStrip

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

如何让消息下拉以正确对齐?http://i.friendfeed.com/ed90b205f64099687db30553daa79d075f280b90

采纳答案by Eric Schoonover

Found it via MSDN forums almost immediately after posting :)

发布后几乎立即通过 MSDN 论坛找到它:)

You can use a ToolStripLabelto pseudo right align controls by setting the Textproperty to string.Emptyand setting the Springproperty to true. This will cause it to fill all of the available space and push all the controls to the right of the ToolStripLabelover.

您可以使用ToolStripLabel通过设置模拟右对齐控件Text属性string.Empty和设置Spring属性true。这将导致它填满所有可用空间并将所有控件推到右侧ToolStripLabel

回答by Martijn Laarman

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

作为补充说明,这是因为在 Win32 API 中,单元格是固定宽度或填充剩余空间 -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.

如果没记错的话,每个状态栏只能有一个填充单元格 (-1)。

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

您还可以添加第三个中间单元格并为其指定填充属性以获得外观更加一致的状态栏。一致,因为 Messages 在您期望它的左侧右侧有一个插图。有点像在MSDN 页面上为 StatusBars找到的 mspaint 截图

I like the creative appreach though :D

我喜欢创造性的方法:D

回答by Chandran R

You can display the Buttonat the end of the StatusStripby using the logic below.

您可以使用以下逻辑Button在 的末尾显示StatusStrip

  1. Add a ToolstripLabelto the StatusStrip
  2. Set text as string.Empty
  3. Set Paddingfor the ToolstripLabel
  1. 一个添加ToolstripLabelStatusStrip
  2. 将文本设置为 string.Empty
  3. 设置PaddingToolstripLabel

For example:

例如:

this.toolStripStatusLabel1.Padding = new Padding((int)(this.Size.Width - 75), 0, 0, 0);

回答by TravisHendrickson

Set the RightToLeft tool strip property to True.

将 RightToLeft 工具条属性设置为 True。

回答by Visakh V A

Keep a Toolstrip label , set Spring property as true and for label align text in BottomLeft

保留工具条标签,将 Spring 属性设置为 true 并在底部左对齐标签文本

回答by Alan Ackart

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

我找到了一种在StatusStrip. 您可以StatusStrip使用以下步骤在 中的任何位置显示按钮。

  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.

  1. 将工具条标签添加到状态条
  2. 将文本设置为合适的空间量,例如

    toolStripStatusLabel1.Text =“ ”;

  3. 如果布局不是您想要的,请转到第 2 步以更改 中的空间量toolStripStatusLabel1.Text,否则工作已完成。

回答by VBobCat

For me it took two simple steps:

对我来说,它只需要两个简单的步骤:

  1. Set MyRightIntendedToolStripItem.Alignmentto Right
  2. Set MyStatusStrip.LayoutStyleto HorizontalStackWithOverflow
  1. 设置MyRightIntendedToolStripItem.AlignmentRight
  2. 设置MyStatusStrip.LayoutStyleHorizontalStackWithOverflow

回答by knightgambit

I found that you can set the StatusStrip Layout to HorizontalStackWithOverflow. Then, for each control on the StatusStrip that you want on the right side, set the control Alignment to Right.

我发现您可以将 StatusStrip 布局设置为 Horizo​​ntalStackWithOverflow。然后,对于需要在右侧的 StatusStrip 上的每个控件,将控件对齐设置为右。

I like this better since you don't need any extra or dummy controls to align.

我更喜欢这个,因为你不需要任何额外的或虚拟的控件来对齐。