c# - FlowLayoutPanel中的点击事件

标签 c# winforms visual-studio

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

This is how it looks (adding)

This is how it looks (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中的每个按钮执行不同的点击事件呢? 谢谢您的解答。

编辑:我想这样做,当您单击库中的按钮时,它将打开该游戏(程序)。

非常感谢!

最佳答案

将所需信息存储在 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); 
}

关于c# - FlowLayoutPanel中的点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23443901/

相关文章:

c# - 从 Silverlight 调用 WCF 服务

c# - 控件中的嵌入窗体或作为用户控件的窗体

visual-studio - 尝试为 iOS 手机构建跨平台 Xamarin 应用程序时出现 "Failed to resolve Plugin.Permissions.Abstractions.Permission"错误

c# - 如何从后台 worker 将文本设置为 TextBlock

c# - 如何使用 C# 从 url 解析 Json 内容?

c# - 从 If 语句内部调用组件

winforms - 在 F# 中使用 Windows 窗体的面向对象的 "Hello world"

c# - 多选 WinForms 列表框的双向绑定(bind)?

git - Visual Studio 2013 删除已删除的 git 分支

visual-studio - 我想在 4 个工作站上使用 VS 2010 Pro,哪种 MSDN 订阅或许可模式最好?