This is what I have:
public void initiate(WebBrowser browser)
{
browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(refDocumentCompleted);
// navigate browser to the referal Uri
browser.Navigate(refreral);
browser.DocumentCompleted -= refDocumentCompleted;
//remove here so that it doesn't do this everytime a document is completed, i want it just in this method
}
private void refDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
// want to call navigate method on browser here. but Its out of scope.
}
What I would like it to do is, navigating to referral, then once that is loaded navigate to another page stored as a global string in the class.
I'm sure my trouble here is from a poor understanding of how events work, I've tried to read up on it, but can't seem to get my head around it but don't think I need to create my own handler.
You already have the webbrowser object. You just need to cast it:
((WebBrowser)sender).Navigate(...);
etc.
Use lambdas, e.g.:
public void initiate(WebBrowser browser)
{
browser.DocumentCompleted += (sender, e) => {
browser.DoStuff(); // it's in scope via closure
};
// etc
}
EDIT: to add / remove it, assign the lambda to a variable:
public void initiate(WebBrowser browser)
{
var doStuff = (sender, e) => {
browser.DoStuff(); // it's in scope via closure
};
browser.DocumentCompleted += doStuff;
// etc
browser.DocumentCompleted -= doStuff;
}
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