【发布时间】:2018-01-03 13:09:56
【问题描述】:
我正在做一个服务器管理器,它需要能够添加和删除服务器。我希望它有一个漂亮的界面,所以我决定将服务器显示在一个包含 4 个标签的图片框中,这些标签打印出服务器的不同统计信息。我的问题是,假设程序的用户有 500 台服务器,为了实现它,我唯一知道如何做的就是在代码中创建、定义和填充图片框和标签。 EX:(这是我的标题,但这是我实现服务器“卡片”的方式)
private PictureBox headerArea = new PictureBox();
private PictureBox dropShadow = new PictureBox();
private Label headerText = new Label();
public Suite()
{
InitializeComponent();
}
private void Suite_Load(object sender, EventArgs e)
{
guiInit();
}
private void guiInit()
{
//Header Text
this.headerText.AutoSize = true;
this.headerText.BackColor = System.Drawing.Color.DarkSlateBlue;
this.headerText.Font = new System.Drawing.Font("Roboto Lt", 22F);
this.headerText.ForeColor = System.Drawing.SystemColors.ButtonFace;
this.headerText.Location = new System.Drawing.Point(12, 10);
this.headerText.Name = "headerText";
this.headerText.Size = new System.Drawing.Size(178, 29);
this.headerText.TabIndex = 0;
this.headerText.Text = "Server Manager";
this.Controls.Add(headerText);
//Background GUI
this.headerArea.BackColor = Color.DarkSlateBlue;
this.headerArea.Size = new System.Drawing.Size(1920, 60);
this.headerArea.Location = new System.Drawing.Point(0, 0);
this.Controls.Add(headerArea);
//Drop Shadow
this.dropShadow.Image =
global::ServerManager.Properties.Resources.dropshadow2;
this.dropShadow.BackgroundImageLayout =
System.Windows.Forms.ImageLayout.Stretch;
this.dropShadow.Location = new System.Drawing.Point(0, 47);
this.dropShadow.Name = "dropShadow";
this.dropShadow.Size = new System.Drawing.Size(1920, 65);
this.dropShadow.TabIndex = 0;
this.dropShadow.TabStop = false;
this.Controls.Add(dropShadow);
}
这样做的问题是,如果我需要显示 500 台服务器,我将有 2500 行代码。 有没有一种方法可以制作生成这些表单的方法或更好的方法?
【问题讨论】:
-
你试过什么?一种方法将是一个不错的选择。如果您尝试这样做,SO 社区将更有可能提供帮助。
-
如果我要制定一种方法来做到这一点,我不知道从哪里开始。
-
创建一个方法来执行您想要的操作并使用参数来更改值。
-
我知道一个方法是如何工作的,只是不知道如何编写代码来创建许多(500+)个窗体控件。
-
循环用于执行您所描述的重复性任务。
标签: c# .net windows winforms visual-studio