带有枚举For Form Position的C#结构

本文关键字:结构 Position Form 枚举 For | 更新日期: 2024-10-19 08:23:31

我有一个枚举和结构,如下所示。

public enum appedges{Left = 0, Top = 1,Right = 2, Bottom = 3}

public struct edges{ public int X, Y, wide, len;}

此结构已声明/实例化四次(LeftEdge、RightEdge、TopEdge、BottomEdge),并为其所有成员设置了值。根据按钮单击事件,将选择枚举的某个值。基于此,我需要选择一个声明的结构实例来设置Form属性,如下所示:

因此,如果选择的枚举值为"Top",则

if (_side == appedges.Top)
{
    this.Location = new Point(TopEdge.X, TopEdge.Y);
    this.Height = TopEdge.len;
    this.Width = TopEdge.wide; 
}

类似地,对于枚举的其他值(Left、Bottom、Right…),我必须用不同的结构实例编写相同的"IF"循环。

我认为可能有一个简单的方法来实现这一点。我的意思是,概括结构实例的使用方式。我不想每次都为每个"IF"循环设置Form属性。我希望你们理解我的观点。

我是c#的新手。所以,我正在为此而挣扎。如果你能帮忙,那就太好了!!

谢谢:)

带有枚举For Form Position的C#结构

您可以使用字典来初始化每个AppEdge:的Edge

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#的新手,请查看C#的命名指南。