2017-07-27 6 views
0

私はサーバーマネージャを作成しています。サーバーを追加したり、サーバーを削除する必要があります。私はそれがかなりのインターフェイスを持っているので、サーバーのさまざまな統計情報を印刷する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行を持っているだろうということです。 これらのフォームを生成する方法やこれを行うためのより良い方法を作る方法はありますか?

+0

何を試しましたか?方法はこれに適しています。あなたがそれを試してみると、SOのコミュニティがより助けになるでしょう。 –

+0

これを行う方法を作るつもりなら、どこから始めたらいいか分からない。 – Jumbofile

+0

必要なアクションを行い、変更する値にパラメーターを使用するメソッドを作成します。 –

答えて

0

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; 
     } 
0

コードの行数を減らす場合は、変更するパラメータを取り込む方法を使用する必要があります。ここにあなたを始めるためのサンプルコードがあります。

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") 
0

私は本当にあなたの言葉を理解していません。私は英語が上手ではありません。 500個のコントロールを1つだけから作成しますか?

//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); 
    } 
関連する問題