2015-05-29 63 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”时它会“自动”触发。