Click here to Skip to main content
15,896,497 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, i have some C# code that i have to convert to VB.NET but ...

C#
// i dont know how to do this in vb.net
backgroundWorker1.DoWork += work; 


Additional info

// long operation
private void LongOp(object sender, DoWorkEventArgs e) {// for...}
// show progressdialog
private void button1_Click(object sender, EventArgs e)
{
   // pass method
   WaitOrProgressDialog w = new WaitOrProgressDialog("Test", LongOp);
   w.ShowDialog();
}


C#
public partial class WaitOrProgressDialog : Form
 { 
         public WaitOrProgressDialog(string aTitle, DoWorkEventHandler work)
     {
         InitializeComponent();
         this.Text = aTitle;

         // How to make this in VB.NET?
         backgroundWorker1.DoWork += work;
     }
}
Posted

Simple - use AddHandler. MSDN[^] shows you how.
 
Share this answer
 
VB
' First define the Event, then the delegate Method.
AddHandler backgroundWorker1.DoWork, AddressOf work

It's a bit bloated compared to C#, but it's the only way in VB :)
For your other code this convertor[^] will probably do fine.
Good luck!
 
Share this answer
 
found the way
AddHandler BackgroundWorker1.DoWork, AddressOf work.Invoke
BackgroundWorker1.RunWorkerAsync()
 
Share this answer
 
Comments
Sander Rossel 31-Oct-11 6:48am    
Please see my solution for adding an event handler... I do not think work.Invoke is a valid Method name...
Dragan01 31-Oct-11 11:53am    
' First define the Event, then the delegate Method.
AddHandler backgroundWorker1.DoWork, AddressOf work

- works in c# but not in VB.NET, can not compile so // its not method

- this works in VB.NET

AddHandler BackgroundWorker1.DoWork, AddressOf work.Invoke
BackgroundWorker1.RunWorkerAsync()

tnx all of you anyway
Sander Rossel 31-Oct-11 14:05pm    
You must be mixing things up... AddHandler can NEVER work in C#, just as AddressOf work.Invoke can NEVER work in VB... But good you sorted it out I guess.
Dragan01 31-Oct-11 15:27pm    
of course that AddHandler exists only in VB
BackgroundWorker1.DoWork += DoSomething; // in c#

//
AddHandler BackgroundWorker.DoWork, AddressOf work
- cant compile, work is not a method, actually is LongOp a method, but it's part of another file

it can be i've mixd things up but that's only way i can compile and run the code

Sander Rossel 31-Oct-11 19:32pm    
So you should replace work in my example with your own Method... It's just that from your question I could make up that work was actually a Method. But I might be seeing what you're getting at.
Good luck!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900