【问题标题】:Adding windows form controls through code通过代码添加windows窗体控件
【发布时间】: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


【解决方案1】:

如果您希望减少您拥有的代码行数,您应该使用一种方法来接收您希望更改的参数。这里有一些示例代码可以帮助您入门。

private void DoThis(string a, string b, string c, string d)
{
    //Create your labels
    //Set the label texts equal to each string.
}

构造函数中的变量(括号)也必须包含在您的调用中。您可以使用

来调用它
DoThis("text1", "text2", "text3", "text4")

【讨论】:

    【解决方案2】:

    我不太明白你的话。我不擅长英语。 您想仅在一个中创建 500 个控件吗?

        //set the control's initial location
        const int HEADERTEXTLOCATIONX = 0;
        const int HEADERTEXTLOCATIONY = 0;
        //set the control's offset value
        const int OFFSETX = 30;
        const int OFFSETY = 20;
    
        List<Label> headerTextList = new List<Label>();
        private void creatSubControls(int controlsCount)
        {
            for (int item = 0; item < controlsCount; item++)
            {
                guiInit(item);
            }
        }
    
        private void guiInit(int iControl)
        {
            Label headerText = new Label();
            int offsetX = iControl * OFFSETX;
            int offsetY = iControl * OFFSETY;
            //Header Text
            headerText.AutoSize = true;
            headerText.BackColor = System.Drawing.Color.DarkSlateBlue;
            headerText.Font = new System.Drawing.Font("Roboto Lt", 22F);
            headerText.ForeColor = System.Drawing.SystemColors.ButtonFace;
            headerText.Location = new System.Drawing.Point(HEADERTEXTLOCATIONX + offsetX, HEADERTEXTLOCATIONY + offsetY);
            headerText.Name = "headerText";
            headerText.Size = new System.Drawing.Size(178, 29);
            headerText.TabIndex = 0;
            headerText.Text = "Server Manager" + (iControl + 1).ToString();
            Controls.Add(headerText);
        }
    

    【讨论】:

      【解决方案3】:

      感谢所有帮助,由于我对 Winforms 缺乏了解,我真的不知道该输入什么。 这或多或少是我想要的:

      for (int i = 0; i < 10; i++)
              {
                  Button button = new Button();
                  button.Left = left;
                  button.Top = top;
                  this.Controls.Add(button);
                  top += button.Height + 2;
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-12-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-08
        相关资源
        最近更新 更多