2015-05-29 59 views
1

我試圖從數據的文件和一個列表框刪除線。但我得到的錯誤:沒有重載「removeButton_Click」匹配委託「System.EventHandler。」我該如何解決這個錯誤?無過載「button_click」匹配委託「System.EventHandler」

public partial class Message_ReaderMainForm : Form 
{ 
private void validSitesListBox_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     removeButton.Visible = true; 
     moveButton.Visible = true; 
     editButton.Visible = true; 

     removeButton_Click(sender, e, validSitesListBox.SelectedIndex); 
    } 

    private void removeButton_Click(object sender, EventArgs e, int location) 
    { 
     StreamWriter file = new StreamWriter("userFile.txt"); 

     validSitesListBox.Items.RemoveAt(location); 
     data.RemoveAt(location); 

     for (int i = 0; i < data.Count(); i++) 
     { 
      file.WriteLine(data[i].Item1 + " " + data[i].Item2 + " " + data[i].Item3); 
     } 

    } 

} 

這是發生錯誤的位置:

partial class Message_ReaderMainForm 
{ 
     // 
     // removeButton 
     // 
     this.removeButton.Location = new System.Drawing.Point(255, 351); 
     this.removeButton.Margin = new System.Windows.Forms.Padding(2); 
     this.removeButton.Name = "removeButton"; 
     this.removeButton.Size = new System.Drawing.Size(59, 21); 
     this.removeButton.TabIndex = 5; 
     this.removeButton.Text = "Remove"; 
     this.removeButton.UseVisualStyleBackColor = true; 
    // This is where the error is showing up in the code. 
     this.removeButton.Click += new System.EventHandler(this.removeButton_Click); 
} 
+0

this.removeButton.Click + =新System.EventHandler(this.removeButton_Click); //每個事件只能有1個方法。把第二個放在一個函數中,並從事件中調用它。 –

+0

從'removeButton_Click'方法中刪除'location'參數 –

回答

1

事件處理程序的簽名不匹配,它必須是,

private void removeButton_Click(object sender, EventArgs e) 

你不能在現有的傳遞aditional的參數委託是EventHandler

事件處理程序的實際簽名,

public delegate void EventHandler (Object sender, EventArgs e) 

你不能改變它...

4

那是因爲你已經宣佈它作爲

private void removeButton_Click(object sender, EventArgs e, int location) 

int location參數意味着它不會在System.EventHandler委託定義相匹配。你將需要刪除的參數,如果你想使用它作爲一個事件處理程序,並通過其他手段獲得location

0

應該

private void removeButton_Click(object sender, EventArgs e) 
你的情況

,你可以用它代替位置

  • ListBox.SelectedIndex財產(validSitesListBox.SelectedIndex)我不爲什麼已瞭解你叫removeButton_Click在validSitesListBox_SelectedIndexChanged。這是一個事件,當你點擊「removeButton」時它會「自動」觸發。