【问题标题】:Add/remove buttons to a panel with an arraylist c#使用arraylist c#向面板添加/删除按钮
【发布时间】:2017-10-20 09:07:02
【问题描述】:

所以我想要的是将字符串添加到我的数组列表中,然后将其作为按钮显示在面板中,如果单击,它将从数组和面板中删除它。

所以我有的是

添加按钮:

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;
        }            
    }
}

但它的工作很糟糕,它创建了两个同名的按钮,然后它只删除了第一个按钮,我不知道如何修复它。

【问题讨论】:

  • 在以下帖子中查看我的答案:stackoverflow.com/questions/37165402/…
  • 尝试在代码中放置一个断点,然后在调试器中单步执行。看看每一行的确切作用。检查每个变量的值。

标签: c# button arraylist panel


【解决方案1】:

变化:

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

收件人:

    button.Name = element;
    button.Text = element;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多