2017-06-22 15 views
-1

私は、テキストボックスとボタンを追加するユーザーの追加ボタンがあります。私は新しいボタンが追加したユーザーを削除するようにしたい。私の問題は、ダイナミックに作成されたテキストボックスを削除するために動的に追加されたボタンを取得する方法がわからないということです...変数をどのように定義したのかという問題はあると思いますが、ここで私が持っているものです。動的に作成されたテキストボックスを削除する

Alias[i].Name = "UserTextBox" + i; 
Remove[i].Name = "UserButton" + i; 

あなたがオブジェクトを除外するために見つけることができますこの方法を:

private void AddUserbtn_Click_1(object sender, EventArgs e) 
    { 
     TextBox[] Alias = new TextBox[n]; 

     Button[] Remove = new Button[n]; 

     int AliasX, AliasY, RemoveX, RemoveY; 

     AliasX = 40; 
     AliasY = 45; 

     RemoveX = 946; 
     RemoveY = 45; 


     for (int i = 0; i < n; i++) 
     { 
      Alias[i] = new TextBox(); 
      Alias[i].Size = new Size(233, 26); 
      Alias[i].Location = new Point(AliasX, AliasY + space); 
      Alias[i].Font = new Font("Arial", 10); 

      Remove[i] = new Button(); 
      Remove[i].Location = new Point(RemoveX, RemoveY + space); 
      Remove[i].Text = ""; 
      Remove[i].Font = new Font("Arial", 10); 
      Remove[i].FlatStyle = FlatStyle.Flat; 
      Remove[i].BackgroundImage =Properties.Resources.btn_remove_user; 
      Remove[i].FlatAppearance.BorderColor = Color.White; 
      Remove[i].BackgroundImageLayout = ImageLayout.Center; 
      Remove[i].Size = new Size(95, 23); 
      Remove[i].UseVisualStyleBackColor = true; 
      Remove[i].Click += new EventHandler(Remove_Click); 

      space += 35; 
     } 


     for (int i = 0; i < n; i++) 
     { 
      Panel.Controls.Add(Alias[i]); 

     } 

     //for(int i=0; i <n;i++) 
     //Remove[i].Click += delegate 
     //{ 
     // Panel.Controls.Remove(Alias[i]); 
     //}; 



    } 

    private void Remove_Click(object sender, EventArgs e) 
    { 
     // Button Remove = sender as Button; 

     // //TextBox[] Alias = new TextBox[n]; 
     // //for (int i = 0; i <n; i++) 
     // //{ 
     // // Panel.Controls.Remove(Alias[i]); 



     // //} 
    } 
+1

設定されたIDプロパティを、とは、どのようにどのコントロール(複数可)、削除したいを知っていますか?私はあなたの質問に情報を追加する必要があると思います。 –

+1

TextBoxとButtonはおそらくUserControlにあるはずです。 – LarsTech

+0

削除ボタンをクリックすると、削除ボタンとエイリアステキストボックスが削除されます。 – Julie

答えて

1

は以下のようにオブジェクトに意味のある名前を付けます。

Panel.Controls.Remove(Panel.Controls["UserTextBox" + i]); 
Panel.Controls.Remove(Panel.Controls["UserButton" + i]); 
あなたのコントロールに
+0

ありがとうございました! – Julie

関連する問題