我在Visual Studio 2013中编程,c#winform。为FLP中的每个按钮选择点击事件是否可行?我试图做像Steam Library这样的事情,我已经做了很多事情,现在看起来我想要。
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中的每个按钮执行不同的单击事件? 谢谢你的回答。
编辑:我想这样做,当你点击库中的按钮时,它将打开那个游戏(程序)。非常感谢你!
答案 0 :(得分:2)
将所需信息存储在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);
}