【问题标题】:C# - How to find out which dynamic button was clicked?C# - 如何找出点击了哪个动态按钮?
【发布时间】:2019-04-19 00:37:55
【问题描述】:

我正在编写一个动态添加按钮的程序,我通过将它们存储在字典中以便稍后从它们中获取特定值(背景颜色)来做到这一点。

我需要为它们中的每一个设置一个 Click 事件,但是每个 Click 事件都必须有所不同,因为单击按钮,会弹出一个 ColorDialog 并更改按钮的背景。

有没有办法知道我点击了哪个按钮?在下面的代码中,button1点击事件添加了其他按钮并为每个按钮设置了EventHandler,EventHandler的代码应该是什么?非常感谢你们。

int i = 0;
Dictionary<int, Button> buttonDictionary = new Dictionary<int, Button>();
Dictionary<int, ColorDialog> colorsDictionary = new Dictionary<int ColorDialog>();


public void button1_Click(object sender, EventArgs e)
{
    i++;
    buttonDictionary.Add(i, new Button());
    buttonDictionary[i].Click += new EventHandler(Click);
    this.Controls.Add(buttonDictionary[i]);
}

public void Click(object sender, EventArgs e)
{
    //Somehow get the int key of the button that was clicked???? (in this case: int j)

    int j;

    if (!colorsDictionary.ContainsKey(j))
    {
        colorsDictionary.Add(j, new ColorDialog());
    }

    if (colorsDictionary[j].ShowDialog() == DialogResult.OK)
    {
        buttonDictionary[j].BackColor = colorsDictionary[j].Color;
    }
}

代码仅用于添加按钮,我会很高兴为您提供任何帮助,谢谢大家!

【问题讨论】:

  • 使用sender,将其转换为Button,然后检查与字典中的项目是否相等

标签: c# visual-studio button dynamic


【解决方案1】:

嗯,直接回答您的问题是:将 sender 转换为 Button

Button pressedButton = (Button) sender;

然后检查它与字典的哪个按钮匹配:

foreach (var entry in buttonDictionary)
{
    if (entry.Value == pressedButton)
    {
        j = entry.Key;
        break;
    }
}

但是,这对于您想要实现的目标来说过于复杂。如果您在按钮和颜色选择器之间有直接关系会容易得多:

Dictionary<Button, ColorDialog> buttonDictionary = new Dictionary<Button, ColorDialog>();

然后像这样填写:

public void button1_Click(object sender, EventArgs e)
{
    i++;
    var button = new Button();
    this.Controls.Add(button);
    button.Click += new EventHandler(Click);

    buttonDictionary.Add(button, null);
}

然后使用

访问它
public void Click(object sender, EventArgs e)
{
    Button pressedButton = (Button) sender;
    ColorDialog dialog = buttonDictionary[pressedButton];

    if (dialog == null)
    {
        dialog = new ColorDialog();
        buttonDictionary[pressedButton] = dialog;
    }

    if (dialog.ShowDialog() == DialogResult.OK)
    {
        pressedButton.BackColor = dialog.Color;
    }
}

更重要的是,问题是为什么您需要这么多 ColorDialgo,因为它应该只需要一个对话框。您可以摆脱ij、所有字典和大多数处理。恕我直言,以下内容就足够了:

public void button1_Click(object sender, EventArgs e)
{
    var button = new Button();
    Controls.Add(button);
    button.Click += Click;
}

public void Click(object sender, EventArgs e)
{
    Button pressedButton = (Button) sender;
    ColorDialog dialog = new ColorDialog {Color = pressedButton.BackColor};
    if (dialog.ShowDialog() == DialogResult.OK)
    {
        pressedButton.BackColor = dialog.Color;
    }
}

奖金信息:

我不完全知道你想要实现什么。但是你的按钮都在同一个地方,相互重叠。为避免这种情况,请将流程布局面板拖到表单上,然后将按钮添加到流程布局中:

flowLayoutPanel1.Controls.Add(button);

这将确保您的按钮排列整齐。

【讨论】:

  • 非常感谢托马斯!你不知道我多么感激你花时间向我解释这一切。你是个好人,再次感谢你!祝你一切顺利。
  • @spoky:很高兴我能帮上忙,你学到了一些东西。在编程时玩得开心。
  • @spoky:如果您已经测试了答案并且它有效,则单击复选标记表示问题已解决。
  • @spoky:我添加了一些信息来自动排列按钮。
  • 忘记查看了,谢谢提醒!我知道按钮重叠,我想通过为每个按钮设置不同的位置来解决这个问题,点击后会增加,但不知道你可以做到这一点!再次感谢你,我真的很感激
猜你喜欢
  • 2011-11-22
  • 1970-01-01
  • 2014-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多