Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#-如何确定单击了哪个动态按钮?_C#_Visual Studio_Button_Dynamic - Fatal编程技术网

C#-如何确定单击了哪个动态按钮?

C#-如何确定单击了哪个动态按钮?,c#,visual-studio,button,dynamic,C#,Visual Studio,Button,Dynamic,我正在编写一个程序,在其中动态添加按钮,通过将它们存储在字典中,以便稍后从中获取特定值(背景颜色) 我需要在每个按钮上设置一个单击事件,但是每个单击事件都必须有一点不同,因为单击按钮时,会弹出一个颜色对话框并更改按钮的背景 有没有办法知道我点击了哪个按钮?在下面的代码中,button1 click事件添加其他按钮并为每个按钮设置EventHandler,EventHandler的代码应该是什么?提前非常感谢各位 int i = 0; Dictionary<int, Button> b

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

我需要在每个按钮上设置一个单击事件,但是每个单击事件都必须有一点不同,因为单击按钮时,会弹出一个颜色对话框并更改按钮的背景

有没有办法知道我点击了哪个按钮?在下面的代码中,button1 click事件添加其他按钮并为每个按钮设置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;
    }
}
inti=0;
Dictionary buttonDictionary=新字典();
字典颜色字典=新字典();
公共无效按钮1\u单击(对象发送者,事件参数e)
{
i++;
添加(i,newbutton());
buttonDictionary[i]。单击+=新建事件处理程序(单击);
this.Controls.Add(buttonDictionary[i]);
}
公共无效单击(对象发送者,事件参数e)
{
//以某种方式获取所单击按钮的int键???(在本例中为int j)
int j;
如果(!colorsDictionary.ContainsKey(j))
{
添加(j,newcolorDialog());
}
if(colorsDictionary[j].ShowDialog()==DialogResult.OK)
{
buttonDictionary[j]。BackColor=colorsDictionary[j]。颜色;
}
}

该代码仅用于添加按钮,我很高兴能得到任何帮助,谢谢大家

对你的问题的直接回答是:将
发送者
投射到
按钮

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 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;
    }
}
更重要的是,问题是为什么你需要这么多彩色拨号键,因为它应该只需要一个对话框。您可以摆脱
i
j
、所有字典以及大多数处理。IMHO,以下内容应足够:

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);
这将确保您的按钮排列整齐


好吧,对你的问题的直接回答是:将
发送者
投射到
按钮

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 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;
    }
}
更重要的是,问题是为什么你需要这么多彩色拨号键,因为它应该只需要一个对话框。您可以摆脱
i
j
、所有字典以及大多数处理。IMHO,以下内容应足够:

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:如果您已经测试了答案并且它有效,那么单击复选标记表示问题已经解决。@spoky:我已经添加了一些信息来自动排列按钮。我忘记检查了,谢谢您提醒我!我知道按钮重叠,我想通过为每个按钮设置一个不同的位置来解决这个问题,这个位置会随着单击而增加,但我不知道你能做到这一点!再次非常感谢你,我真的很感激