0

Usually when i need to convert code from c# to vb.net i use this link http://converter.telerik.com/ but looking to an old answer (WebBrowser Control in a new thread)

i found this line that i don't understand, and that the converter don't translate:

br.DocumentCompleted += browser_DocumentCompleted;

private void runBrowserThread(Uri url) {
var th = new Thread(() => {
    var br = new WebBrowser();
    br.DocumentCompleted += browser_DocumentCompleted;
    br.Navigate(url);
    Application.Run();
});
th.SetApartmentState(ApartmentState.STA);
th.Start();
}

void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
    var br = sender as WebBrowser;
    if (br.Url == e.Url) {
        Console.WriteLine("Natigated to {0}", e.Url);
        Application.ExitThread();   // Stops the thread
    }
}

Anyone know the translation?

Thanks

Community
  • 1
  • 1
Marcello
  • 438
  • 5
  • 21
  • I'm not a VB guru, but if it helps - that line is wiring up an event handler. It's setting browser_DocumentCompleted() to be the event handler for the DocumentCompleted event. – squillman Jun 03 '16 at 13:16
  • Thanks, yes, it's an eventHandler, but i don't understand the code, as i know in vb.net isn't possible to sum an eventHandler to a procedure, and i want to understand what the c# code mean – Marcello Jun 03 '16 at 13:29
  • It's just the C# syntax for adding the handler. += is shorthand for increment by some value. So you can say x+=1 instead of x = x + 1. It's also used in the case for the wireup. – squillman Jun 03 '16 at 13:31
  • `+=` is used for many things. You can increment as squillman has shown or you can append one string to another, or add an event handler as shown in your example – A Friend Jun 03 '16 at 14:05

1 Answers1

1

That line is adding an event handler for WebBrowser.DocumentCompleted event, pointing to the browser_DocumentCompleted method.

This is the translation: AddHandler br.DocumentCompleted, AddressOf browser_DocumentCompleted

A Friend
  • 2,750
  • 2
  • 14
  • 23
  • thank you very much for the quickly reply, but when i meet ' br.DocumentCompleted -= browser_DocumentCompleted;' (note the - instead +) what it means? thanks – Marcello Jun 03 '16 at 13:30
  • 1
    @Marcello That's the RemoveHandler syntax. It would translate to `RemoveHandler br.DocumentCompleted, AddressOf browser_DocumentCompleted`. – Martin Soles Jun 03 '16 at 13:42