点击FlowLayoutPanel中的event

本文关键字:event 中的 FlowLayoutPanel 点击 | 更新日期: 2023-09-27 18:01:23

我在Visual Studio 2013, c# winform中编程。是否可以为FLP中的每个按钮选择点击事件?我试着做一些像Steam库的东西,我已经做了很多事情,现在看起来我想要。

这是它看起来的样子(添加)

这就是它的样子(library)

(对不起,我不能添加图像)

但我不知道如何打开选择的游戏,当你点击一个按钮在FLP(在库)。我的代码是这样的:

private void btnTest_Click_1(object sender, EventArgs e)
    {
        if (textBox1.Text != "")
             {
            if (textBox2.Text != "")
                {
                    Button btn = sender as Button;
                    Button btnNew = new Button();
                    btnNew.Text = "";
                    btnNew.Height = 108;
                    btnNew.Width = 230;
                    btnNew.Image = new Bitmap(textBox1.Text);
                    btnNew.FlatStyle = FlatStyle.Flat;
                    flpContainer.Controls.Add(btnNew);
                    btnNew.Click += btnNew_Click;
                    counter1++;
                    label1.Text = counter1.ToString(); //label1 is that "Number of games in library:"
                    System.Windows.Forms.MessageBox.Show("Game was succesfully added to library!");
                }
            else if (textBox2.Text == "")
                {
                System.Windows.Forms.MessageBox.Show("You didn't choosed exe file!");
                }
             }
        if (textBox1.Text =="")
            {
                System.Windows.Forms.MessageBox.Show("You didn't choosed image!");
            }
    }

    private void btnNew_Click(object sender, EventArgs e)
        {
            Process.Start(textBox2.Text); //textbox2 is the path to exe file, but it change when you want to add another game to library
        }

但是如何在FlowLayoutPanel中为每个按钮做不同的点击事件?谢谢你的回答。

编辑:我想这样做,当你点击一个按钮在库它会打开游戏(程序)。

非常感谢!

点击FlowLayoutPanel中的event

将需要的信息存储在Tag属性中,以备将来使用并完成工作

Button btnNew = new Button();
btnNew.Text = "";
btnNew.Height = 108;
btnNew.Width = 230;
btnNew.Image = new Bitmap(textBox1.Text);
btnNew.FlatStyle = FlatStyle.Flat;
flpContainer.Controls.Add(btnNew);
btnNew.Click += btnNew_Click;
btnNew.Tag = textBox2.Text;// <--Store it in Tag

然后在Click事件中使用

private void btnNew_Click(object sender, EventArgs e)
{
    Button clickedButton = (Button)sender;
    Process.Start((string)clickedButton.Tag); 
}