2013-02-21 10 views
7

たとえば、「表示」ボタンをクリックした後に「n」の値がわかっている場合、対応するテキストボックスで「n」ラベルを動的に作成して表示する方法はありますか?整数変数の値に応じて多くのラベルとテキストボックスを動的に作成する方法は?

私の質問を理解できないものがあれば教えてください。ありがとうございました!

私はVS C#Express 2010 Windowsフォームで作業しています。ここで

+2

あなたのスペルはクリエイティブな信じられないほどです。 –

+1

可能な複製[動的にテキストボックスとラベルwinformsの配列を作成](http://stackoverflow.com/questions/11157369/dynamically-create-array-of-textboxes-and-labels-winforms) – mbeckish

+2

@UweKeim My bad Eng、私はそれが本当に信じられないほど創造的だったと思うLOL –

答えて

15

私はラベルとテキストボックスを保持し、そのユーザーコントロールのインスタンスを単に作成するユーザーコントロールを作成します。より良い方法を知り、プロパティを使用してユーザーコントロールからLabelとText Boxの値にアクセスする方法を知りたい場合は、教えてください。それを行うには

簡単な方法は、次のようになります。

int n = 4; // Or whatever value - n has to be global so that the event handler can access it 

private void btnDisplay_Click(object sender, EventArgs e) 
{ 
    TextBox[] textBoxes = new TextBox[n]; 
    Label[] labels = new Label[n]; 

    for (int i = 0; i < n; i++) 
    { 
     textBoxes[i] = new TextBox(); 
     // Here you can modify the value of the textbox which is at textBoxes[i] 

     labels[i] = new Label(); 
     // Here you can modify the value of the label which is at labels[i] 
    } 

    // This adds the controls to the form (you will need to specify thier co-ordinates etc. first) 
    for (int i = 0; i < n; i++) 
    { 
     this.Controls.Add(textBoxes[i]); 
     this.Controls.Add(labels[i]); 
    } 
} 

上記のコードは、ボタンbtnDisplayを持っており、それがbtnDisplay_Clickイベントハンドラに割り当てられたonClickイベントを持っていることを前提としています。また、nの値を知る必要があり、すべてのコントロールを配置する場所を把握する方法が必要です。コントロールの幅と高さも指定する必要があります。

ユーザーコントロールを使用するには、これを行うだけです。

まず、新しいユーザーコントロールを作成し、テキストボックスとラベルを貼り付けます。

txtSomeTextBoxlblSomeLabelと呼ばれています。背後にあるコードでは、このコードを追加します。

public string GetTextBoxValue() 
{ 
    return this.txtSomeTextBox.Text; 
} 

public string GetLabelValue() 
{ 
    return this.lblSomeLabel.Text; 
} 

public void SetTextBoxValue(string newText) 
{ 
    this.txtSomeTextBox.Text = newText; 
} 

public void SetLabelValue(string newText) 
{ 
    this.lblSomeLabel.Text = newText; 
} 

ユーザーコントロールを生成するコードは(MyUserControlはあなたのユーザーコントロールに与えた名前です)、次のようになります。もちろん

private void btnDisplay_Click(object sender, EventArgs e) 
{ 
    MyUserControl[] controls = new MyUserControl[n]; 

    for (int i = 0; i < n; i++) 
    { 
     controls[i] = new MyUserControl(); 

     controls[i].setTextBoxValue("some value to display in text"); 
     controls[i].setLabelValue("some value to display in label"); 
     // Now if you write controls[i].getTextBoxValue() it will return "some value to display in text" and controls[i].getLabelValue() will return "some value to display in label". These value will also be displayed in the user control. 
    } 

    // This adds the controls to the form (you will need to specify thier co-ordinates etc. first) 
    for (int i = 0; i < n; i++) 
    { 
     this.Controls.Add(controls[i]); 
    } 
} 

をプロパティにアクセスして設定するために、より多くのメソッドをusercontrolに作成できます。それともあなたはたくさんにアクセスする必要があるだけであれば、単にこれら二つの変数に入れて、あなたがテキストボックスにアクセスし、直接ラベルを付けることができます。

myTextBox = this.txtSomeTextBox; 
myLabel = this.lblSomeLabel; 

ユーザーコントロールのコンストラクタで
public TextBox myTextBox; 
public Label myLabel; 

は、これを行いますあなたのプログラムでは、どちらかのテキスト値を変更したい場合は、これを行うだけです。

control[i].myTextBox.Text = "some random text"; // Same applies to myLabel 

は、それが:)

PSを助け願っています。次は、サンプルプロジェクトへのリンクです。

Dynamic Control Creation

+0

こんにちは、はい私はラベルとテキストボックスの値にアクセスしたい、アドバイスをお願いしますか? –

+0

新しいサンプルコードであなたの答えを追加することができます。 –

+1

ここであなたが仲間に行く、あなたがそれを明確にするか、実際のプロジェクトサンプルが必要な場合は、私に知らせてください –

4

TableLayoutPanel

なり、その後、ちょうど

for (int i = 0; i < COUNT; i++) { 


    Label lblTitle = new Label(); 
    lblTitle.Text = i+"Your Text"; 
    youlayOut.Controls.Add(lblTitle, 0, i); 

    TextBox txtValue = new TextBox(); 
    youlayOut.Controls.Add(txtValue, 2, i); 
} 
+0

この回答は半動的です – YEH

2

それにコントロールを追加することができ、あなたのWinフォームへのプレースホルダとして作用するsomethinkを追加し続ける聞かせなければならない簡単な例であるあなたが持っていると仮定ボタンを押すとnを5に設定すると、フォームにラベルやテキストボックスを生成することができます。

var n = 5; 
for (int i = 0; i < n; i++) 
{ 
    //Create label 
    Label label = new Label(); 
    label.Text = String.Format("Label {0}", i); 
    //Position label on screen 
    label.Left = 10; 
    label.Top = (i + 1) * 20; 
    //Create textbox 
    TextBox textBox = new TextBox(); 
    //Position textbox on screen 
    textBox.Left = 120; 
    textBox.Top = (i + 1) * 20; 
    //Add controls to form 
    this.Controls.Add(label); 
    this.Controls.Add(textBox); 
} 

これはフォームに追加するだけでなく、それらも適切に配置します。

0

あなたはこれを試すことができます。

int cleft = 1; 
intaleft = 1; 
private void button2_Click(object sender, EventArgs e) 
{ 
    TextBox txt = new TextBox(); 
    this.Controls.Add(txt); 
    txt.Top = cleft * 40; 
    txt.Size = new Size(200, 16); 
    txt.Left = 150; 
    cleft = cleft + 1; 
    Label lbl = new Label(); 
    this.Controls.Add(lbl); 
    lbl.Top = aleft * 40; 
    lbl.Size = new Size(100, 16); 
    lbl.ForeColor = Color.Blue; 
    lbl.Text = "BoxNo/CardNo"; 
    lbl.Left = 70; 
    aleft = aleft + 1; 
    return; 
} 
private void btd_Click(object sender, EventArgs e) 
{ 
    //Here you Delete Text Box One By One(int ix for Text Box) 
    for (int ix = this.Controls.Count - 2; ix >= 0; ix--) 
    //Here you Delete Lable One By One(int ix for Lable) 
    for (int x = this.Controls.Count - 2; x >= 0; x--) 
    { 
     if (this.Controls[ix] is TextBox) 
     this.Controls[ix].Dispose(); 
     if (this.Controls[x] is Label) 
     this.Controls[x].Dispose(); 
     return; 
    } 
} 
関連する問題