C# struct with enum For Form Position
C# struct with enum For Form Position
我有一个枚举和结构,如下所示。
public enum appedges{Left = 0, Top = 1,Right = 2, Bottom = 3}
public struct edges{ public int X, Y, wide, len;}
此结构已 declared/instantiated 四次(LeftEdge、RightEdge、TopEdge、BottomEdge),并为其所有成员设置了值。基于按钮的 Onclick 事件,将选择枚举的特定值。基于此,我需要选择一个声明的结构实例来设置如下所示的表单属性:
所以如果选择的枚举值是 "Top",那么
if (_side == appedges.Top)
{
this.Location = new Point(TopEdge.X, TopEdge.Y);
this.Height = TopEdge.len;
this.Width = TopEdge.wide;
}
同样,对于 enum(Left, Bottom, Right...) 的其他值,我将不得不用不同的结构实例编写相同的 "IF" 循环。
我认为可能有一种简单的方法可以完成此任务。我的意思是,概括结构实例的使用方式。我不想每次都为每个 "IF" loop 设置 Form 属性。我希望你们明白我的意思。
我是c#的新手。所以,我正在努力解决这个问题。如果你能帮忙,那就太好了!!
谢谢你:)
您可以使用字典,在其中初始化 Edge
每个 AppEdge
:
var positions = new Dictionary<AppEdge, Edge>
{
{ AppEdge.Left, new Edge { X = 0, Y = 0, ... } },
{ AppEdge.Top, new Edge { X = 0, Y = 0, ... } },
{ AppEdge.Right, new Edge { X = 0, Y = 0, ... } },
{ AppEdge.Bottom, new Edge { X = 0, Y = 0, ... } },
};
然后使用 _side
作为索引在该字典中查找 Edge
:
var edge = positions[_side];
this.Location = new Point(edge.X, edge.Y);
this.Height = edge.len;
this.Width = edge.wide;
鉴于您是 C# 新手,请查看 naming guidelines for C#。
我有一个枚举和结构,如下所示。
public enum appedges{Left = 0, Top = 1,Right = 2, Bottom = 3}
public struct edges{ public int X, Y, wide, len;}
此结构已 declared/instantiated 四次(LeftEdge、RightEdge、TopEdge、BottomEdge),并为其所有成员设置了值。基于按钮的 Onclick 事件,将选择枚举的特定值。基于此,我需要选择一个声明的结构实例来设置如下所示的表单属性:
所以如果选择的枚举值是 "Top",那么
if (_side == appedges.Top)
{
this.Location = new Point(TopEdge.X, TopEdge.Y);
this.Height = TopEdge.len;
this.Width = TopEdge.wide;
}
同样,对于 enum(Left, Bottom, Right...) 的其他值,我将不得不用不同的结构实例编写相同的 "IF" 循环。
我认为可能有一种简单的方法可以完成此任务。我的意思是,概括结构实例的使用方式。我不想每次都为每个 "IF" loop 设置 Form 属性。我希望你们明白我的意思。
我是c#的新手。所以,我正在努力解决这个问题。如果你能帮忙,那就太好了!!
谢谢你:)
您可以使用字典,在其中初始化 Edge
每个 AppEdge
:
var positions = new Dictionary<AppEdge, Edge>
{
{ AppEdge.Left, new Edge { X = 0, Y = 0, ... } },
{ AppEdge.Top, new Edge { X = 0, Y = 0, ... } },
{ AppEdge.Right, new Edge { X = 0, Y = 0, ... } },
{ AppEdge.Bottom, new Edge { X = 0, Y = 0, ... } },
};
然后使用 _side
作为索引在该字典中查找 Edge
:
var edge = positions[_side];
this.Location = new Point(edge.X, edge.Y);
this.Height = edge.len;
this.Width = edge.wide;
鉴于您是 C# 新手,请查看 naming guidelines for C#。