顶部、底部、左侧、右侧的 C# 标准类(枚举?)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/531545/
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
C# standard class (enumeration?) for Top, Bottom, Left, Right
提问by TK.
Is there a standard c# class that defines a notional Left, Right, Top and Bottom?
是否有标准的 c# 类定义了概念上的左、右、上和下?
Should I just use my own?
我应该只使用我自己的吗?
enum controlAlignment
{
left = 1,
top,
right,
bottom,
none = 0
}
采纳答案by Micha?l Carpentier
Perhaps System.Windows.Forms.AnchorStyles or System.Windows.Forms.DockStyles could do the job.
也许 System.Windows.Forms.AnchorStyles 或 System.Windows.Forms.DockStyles 可以完成这项工作。
回答by Peter
Read up on the FlagAttribute
回答by Marc Gravell
Not unless you could the anchor styles (which has more). I'd roll my own for this. In the standard winforms library there are separateVerticalAlignment
and HorizontalAlignment
that might be useful.
除非你可以锚样式(有更多)。我会为此自己动手。在标准的 winforms 库中有单独的VerticalAlignment
,HorizontalAlignment
这可能很有用。
回答by Cerebrus
A quick search revealed that the following Framework Enumerations already have these members (some have other additional members) :
快速搜索显示以下框架枚举已经有这些成员(有些还有其他成员):
- AnchorStyles - System.Windows.Forms
- Border3DSide - System.Windows.Forms
- DockStyle - System.Windows.Forms
- Edges - System.Windows.Forms.VisualStyles
- TabAlignment - System.Windows.Forms
- ToolStripStatusLabelBorderSides - System.Windows.Forms
- VerticalAlignment - System.Windows.Forms.VisualStyles
- AnchorStyles - System.Windows.Forms
- Border3DSide - System.Windows.Forms
- DockStyle - System.Windows.Forms
- 边缘 - System.Windows.Forms.VisualStyles
- TabAlignment - System.Windows.Forms
- ToolStripStatusLabelBorderSides - System.Windows.Forms
- 垂直对齐 - System.Windows.Forms.VisualStyles
回答by Josh Lee
Well, both AnchorStyles
and DockStyles
have additional values besides the four you need; AnchorStyles
additionally has FlagAttribute
turned on which won't necessarily make sense (What would Top Left mean? What about Left Right?)
同时,双方AnchorStyles
并DockStyles
有除了你所需要的四个附加价值; AnchorStyles
另外已经FlagAttribute
打开,这不一定有意义(左上是什么意思?左右呢?)
Since I can't think of any built-in functions that could generically take advantage of the standard Anchor- and DockStyles data types in any meaningful way, writing your own enumeration seems like a much saner alternative to linking against Windows.Forms
just for the sake of an enum.
由于我想不出任何可以以任何有意义的方式普遍利用标准 Anchor- 和 DockStyles 数据类型的内置函数,编写自己的枚举似乎是一种更明智的替代方法,而不是Windows.Forms
仅仅为了枚举。
Unless, of course, you're already inside Windows.Forms, and one of @Cerebrus's suggestions actually makes sense in your context.
当然,除非您已经在 Windows.Forms 中,并且 @Cerebrus 的建议之一在您的上下文中实际上是有意义的。
回答by Dave Cousineau
I often use System.Windows.Forms.ArrowDirectionfor this, since it doesn't imply any particular intent.
为此,我经常使用System.Windows.Forms.ArrowDirection,因为它并不暗示任何特定的意图。
回答by Knasterbax
A nice enum might also be:
一个不错的枚举也可能是:
System.Drawing.ContentAlignment(in System.Drawing.dll)
系统图。内容对齐(在 System.Drawing.dll 中)
These are its members:
这些是它的成员:
public enum ContentAlignment
{
TopLeft = 1,
TopCenter = 2,
TopRight = 4,
MiddleLeft = 16,
MiddleCenter = 32,
MiddleRight = 64,
BottomLeft = 256,
BottomCenter = 512,
BottomRight = 1024,
}
回答by crypto_rsa
Since there is no standard enum in .NET exactly matching your requirements, I suggest creating your own (as I have done):
由于 .NET 中没有完全符合您要求的标准枚举,我建议创建您自己的(正如我所做的那样):
[Flags]
private enum Borders
{
None = 0,
Left = 1,
Top = 2,
Right = 4,
Bottom = 8,
All = Left | Top | Right | Bottom,
}
You can then query the individual edges by calling the HasFlag
method. I agree with @dbkk, reusing enums with similar values but a different meaning can be confusing for anyone reading the code.
然后,您可以通过调用该HasFlag
方法来查询各个边。我同意@dbkk,重用具有相似值但不同含义的枚举可能会让任何阅读代码的人感到困惑。