I have a method that开发者_StackOverflow社区 tries to execute an event / method (Download_Click), although for some reason I get:
The name 'sender' does not exist in the current context The name 'e' does not exist in the current context
The button definitely exists, although for some reason won't work in my code below:
private void checkver()
{
FileInfo sourceFile = new FileInfo("download.zip");
if (sourceFile.Exists)
{
var request = (HttpWebRequest)WebRequest.Create(@"http://www.google.com/download.zip");
request.Method = "HEAD";
var response = (HttpWebResponse)request.GetResponse();
if (response.LastModified > sourceFile.LastWriteTime)
{
MessageBox.Show("File outdated");
Download_Click(sender, e);
// use response.GetStream() to download the file.
}
else
{
MessageBox.Show("File in date");
}
}
}
You're getting this message because there is no such variable named sender
(nor is there one named e
) in your function.
It looks like you've tried to move the code for a button's Click
event handler out into another function (sender
and e
are, by convention, the name of the two parameters sent to an event handler). Unfortunately, the sender
and e
variables don't exist in your function because you didn't define them as parameters.
The best thing to do here is to do the same for the Download
button. Rather than calling its Click
event handler directly (Download_Click
), create a new function that performs the download, then just call that function from within Download_Click
. Then change the code above to call that function directly.
Adam Robinson's answer is the right one - but if you really really really want to do that call (no, please don't) (I mean, REALLY, DON'T) you can use
Download_Click(new object(), new System.EventArgs());
It appears you are trying to programmatically invoke the buttons click event. sender and e do not exist in this context. you could call the click event in this manner.
Download_Click(this, new EventArgs());
精彩评论