2014-05-03 154 views
2

我在Visual Studio 2013中編程,c#winform。爲FLP中的每個按鈕選擇單擊事件是否可行?我試圖做一些像Steam Library這樣的東西,我已經做了很多事情,現在看起來像我想要的。FlowLayoutPanel中的Click事件

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中每個按鈕做不同的單擊事件? 謝謝你的答案。

編輯:我想這樣做,當你點擊庫中的一個按鈕,它會打開那個遊戲(程序)。

非常感謝!

+0

將圖片上傳到一些不錯的網站,如:Tinypic.com或Imgur.com,我不能在你的鏈接查看圖像。 –

+0

無法查看您的圖像。上傳到[TinyPic](http://tinypic.com/)或[Imgur](https://imgur.com/)等網站。 –

+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); 
}