2017-10-19 10 views
0

私はarraylistに文字列を追加し、パネルにボタンとして表示し、それをクリックすると配列とパネルから文字列を削除します。arraylistのパネルにボタンを追加/削除する#

それでは、私が持っていることは、ボタンを追加

です:

if (!tags.Contains(tag.Text)) { 
    tags.Add(tag.Text); 
    organizeTags(tags); 
} 
else { 
    MessageBox.Show("Ese tag ya está registrado", "Error: ", MessageBoxButtons.OK, MessageBoxIcon.Error); 
} 
label10.Text = tags.Count.ToString(); 

削除ボタン:

private void Button_Click(object sender, EventArgs e) 
{ 
    Button button = new Button(); 
    button = (Button)sender; 
    tags.Remove(button.Name); 
    organizeTags(tags); 
} 

そしてorganizeTags機能:

private void organizaTags(ArrayList tags) 
{ 
    panel1.Controls.Clear(); 
    ArrayList botones = new ArrayList(); 
    int j = 0, i = 0; 
    foreach (string element in tags) { 
     Button button = new Button(); 
     button.Name = textBox6.Text; 
     button.Text = textBox6.Text; 
     button.Width = 100; 
     button.Left = i * 100; 
     button.Top = j * 30; 
     button.Click += new EventHandler(Button_Click); 
     panel1.Controls.Add(button); 
     i++; 
     if (i == 6) 
     { 
      j++; 
      i = 0; 
     }    
    } 
} 

しかし、それは恐ろしく作品、それはcr同じ名前の2つのボタンを表示すると、最初のボタンだけが削除され、修正方法はわかりません。

+1

には、次の投稿で私の答えを参照してください。https://stackoverflow.com/questions/37165402/c-sharp-adding-button-with-実行時の値 – jdweng

+1

コードにブレークポイントを入れてから、デバッガでブレークポイントを実行してみてください。各行が何をしているかを正確に見てください。各変数の値を調べます。 –

答えて

0

変更:

button.Name = textBox6.Text; 
    button.Text = textBox6.Text; 

へ:

button.Name = element; 
    button.Text = element; 
+0

ありがとう、それは働いた! – Fran

関連する問題