2016-04-06 13 views
0

ボタンとテキストボックスを実行時に動的に追加したい場合は、それぞれの ボタンの反応が異なります。独立したクリックイベントでボタンを追加する方法

texbox1 , newbutton2 linked with textbox2`今

とリンクnewbutton1すなわち、いずれかのボタンは、単に最初から最後のテキストボックス 次々に出力します。

 List<Button> buttons = new List<Button>(); 
     List<TextBox> textboxes = new List<TextBox>(); 

     int NumTextBox = 0; 
     void click(object sender, EventArgs e) 
     { 

      MessageBox.Show(textboxes[NumTextBox].Text); 
      NumTextBox++; 
     } 

     int x = 0; 
     int y = 0; 
     void AddClick(object sender, EventArgs e) 
     { 
       Button newButton = new Button(); 
       buttons.Add(newButton); 
       newButton.Click += click;// 
       // newButton.Location.Y = button1.Location.Y + 20; 
       newButton.Location = new Point(button1.Location.X, button1.Location.Y+25+x); 
       x += 25; 
       this.Controls.Add(newButton); 

       TextBox newTextBox = new TextBox(); 
       textboxes.Add(newTextBox); 
       // newTextBox.Click += click; 

       newTextBox.Location = new Point(textBox1.Location.X, textBox1.Location.Y+25+y); 
       y += 25; 
       this.Controls.Add(newTextBox); 

     } 
+0

を追加しましたか? –

+0

リストにaddignの前に 'newButton.Tag = buttons.count + 1'をセットし、次にclickイベントでこのような保存されたインデックスを使ってテキストボックスを取得します。var txt = textboxes [Convert.ToIn32((Button)sender) .Tag)]; ' – Franck

答えて

0

は皆、私はフランクさん@続くありがとう答え .So 変更された内容

私はbutton1 & textbox1既製を削除し、私は Lists

に証拠スクリーンショットそれらを追加することができるようにForm_loadにプログラム的にそれらを 追加しました:http://prntscr.com/aprqxz

コード:

 List<Button> buttons = new List<Button>(); 
     List<TextBox> textboxes = new List<TextBox>(); 

     Button button1 = new Button(); 
     TextBox textBox1 = new TextBox(); 

     int x = 0; 
     int y = 0; 

     void click(object sender, EventArgs e) 
     { 


      var txt = textboxes[Convert.ToInt32(((Button)sender).Tag)].Text; 
      MessageBox.Show(txt.ToString()); 

     } 

     void AddClick(object sender, EventArgs e) 
     { 

       Button newButton = new Button(); 
       newButton.Click += click; 
       newButton.Location = new Point(button1.Location.X, button1.Location.Y+25+x); 
       x += 25; 
       newButton.Tag = buttons.Count; 

       this.Controls.Add(newButton); 

       buttons.Add(newButton);    
       // 
       TextBox newTextBox = new TextBox(); 
       newTextBox.Location = new Point(textBox1.Location.X, textBox1.Location.Y+25+y); 
       y += 25; 
       this.Controls.Add(newTextBox); 

       textboxes.Add(newTextBox); 

     } 
     void MainFormLoad(object sender, EventArgs e) 
     { 
       button1.Click += click; 
       button1.Location = new Point(55, 48); 

       button1.Tag = buttons.Count; 

       this.Controls.Add(button1); 

       buttons.Add(button1);    
       // 
       textBox1.Location = new Point(137, 50); 

       this.Controls.Add(textBox1); 

       textboxes.Add(textBox1); 
     } 

EDIT 1:カウントは0から始まると私はnewButton.Tag = buttons.count+1;を追加していなかった私は、あなたが `のHashMap <ボタン、テキストボックス>`と考えたことがありますちょうどnewButton.Tag = buttons.count;

0

あなたがこの中に、ボタンクラスから継承myButtonというようなクラスを持つことができます。

はまた、私はここでガイド

ため、すでにフォーム上のボタン1 &のTextBox1が私のコードでいることを考えます新しいクラスでは、テキストボックスタイプのプロパティを持つことができます。次のコードのように。あなたのコードでは、list<mybutton>を使用して、linkedTextboxプロパティをテキストボックスに設定することができます。

public class myButton:Button 
{ 
    ... 
    public TextBox linkedTextBox{set;get;} 
} 

とあなたのコードで、あなたは、このようにいくつかのことを書く必要があります。

list<myButton> buttons=new list<myButton>(); 
Textbox someTextBox=new TextBox(); 
buttons[0].linkedTextbox=someTextBox; 

とあなたのイベントにご使用できます。

((myButton)sender).linkedTextBox.text="Some thing"; 
関連する問題