Add/remove 按钮到带有 arraylist c# 的面板
Add/remove buttons to a panel with an arraylist c#
所以我想要的是将字符串添加到我的数组列表中,然后将其作为按钮显示在面板中,如果您单击它,它就会将其从数组和面板中删除。
所以我有的是
添加按钮:
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;
}
}
}
但它的工作很糟糕,它创建了 2 个同名按钮,然后它只删除了第一个按钮,我不知道如何修复它。
变化:
button.Name = textBox6.Text;
button.Text = textBox6.Text;
收件人:
button.Name = element;
button.Text = element;
所以我想要的是将字符串添加到我的数组列表中,然后将其作为按钮显示在面板中,如果您单击它,它就会将其从数组和面板中删除。
所以我有的是
添加按钮:
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;
}
}
}
但它的工作很糟糕,它创建了 2 个同名按钮,然后它只删除了第一个按钮,我不知道如何修复它。
变化:
button.Name = textBox6.Text;
button.Text = textBox6.Text;
收件人:
button.Name = element;
button.Text = element;