No it's not that kind of basic question. I am doing a application and got a scenerio like, the file will be downloaded then it will be uploaded to the FTP server, then the local copy will be deleted, then one entry will be placed in a dictionary for that filename. So, the code is below
public void download_This_WebPage(string url, string cookies_String, string local_Saving_File_Name_With_Path)
{
WebClient wb = new WebClient();
wb.Headers.Add(HttpRequestHeader.Cookie, cookies_String);
// Below I want to pass this local_File _Path to the event handler
wb.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(wb,);
wb.DownloadFileAsync(new Uri(url), local_Saving_File_Name_With_Path + ".html");
}
public void data_Download_Completed(Object sender, System.ComponentModel.AsyncCompletedEventArgs args)
{
//use the file name to upload the file to FTP
}
public FTP_Completed
{
// Delete the file
}
But, I dont know how to pass that filename to the event handler of download_Completed. Can anybody guide me in this
EDIT: Thank you for the answers from "Darin" and "Frederic". Is there any general way to pass the custom data to the (already defined)event handler like below
void main_Fn()
{
string my_Data = "Data";
some_object a = new some_object();
some_Object.click_event += new eventHandler(click_Happened);
(Assume that the event passes two ints, I also want to pass the string "my_Data"
to "click_Happened")
some_object.start();
}
void click_Happened(int a, int b)
{
// I want to get the string "my_Data" here.
}
In short how to trick the signature?
Each key node has a handler that receives key events when the key has focus. The handler responds to the key-pressed and key-released events for the Enter key by changing the color of the key on the screen. The event is then consumed so that the keyboard node, which is the parent node, does not receive the event.
To pass an onChange event handler to a child component in React: Define the event handler function in the parent component. Pass it as a prop to the child component, e.g. <Child handleChange={handleChange} /> . Set it to the onChange prop on the input field in the child.
We cannot directly pass the argument to the event handler because, in that scenario, the event handler function would call automatically before you even pressed the button.
If you want to pass a parameter to the click event handler you need to make use of the arrow function or bind the function. If you pass the argument directly the onClick function would be called automatically even before pressing the button.
You can pass the file name in the userToken
argument to DownloadFileAsync(). When the operation finishes, it will be available in the UserState
property of the AsyncCompletedEventArgs argument passed to data_Download_Completed()
:
string filename = local_Saving_File_Name_With_Path + ".html";
wb.DownloadFileAsync(new Uri(url), filename, filename);
Then:
public void data_Download_Completed(Object sender,
System.ComponentModel.AsyncCompletedEventArgs args)
{
string filename = (string) args.UserState;
// Now do something with 'filename'...
}
You could use the 3-rd argument of the DownloadFileAsync
method which allows you to pass an UserState
to the completed handler:
// subscribe to the completed event
wb.DownloadFileCompleted += data_Download_Completed;
string file = local_Saving_File_Name_With_Path + ".html";
wb.DownloadFileAsync(new Uri("url"), file, file);
and inside the handler:
public void data_Download_Completed(Object sender, AsyncCompletedEventArgs args)
{
// extract the filename from the UserState of the args
string file = args.UserState as string;
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With