How to do "WithEvents" in C#?

B

Brett

What is the C# equivalent for this VB.NET code:
Private WithEvents IE_Inst As New SHDocVw.InternetExplorer

I get as far as:
private SHDocVw.InternetExplorer IE_Inst = new SHDocVw.InternetExplorer();

but am not sure how to translate the WithEvents part. Any suggestions?

Thanks,
Brett
 
M

Markus Thurner

What is the C# equivalent for this VB.NET code: Private WithEvents
IE_Inst As New SHDocVw.InternetExplorer

C# does not support WithEvents directly.
Have a look at differences between C# and VB.NET at
http://www.codeproject.com/dotnet/vbnet_c__difference.asp

To your question:
You have to define each event, where you want to use an event handler,
explicitly:

SHDocVw.Event1 += new EventHandler(HandleEvent_Function1);
SHDocVw.Event2 += new EventHandler(HandleEvent_Function2);
....


hth
thoean
 
B

Bob Powell [MVP]

Don't worry about it. It's another example of keyword diarrhea in VB. It
fits right in with the idiocy of "overloads overrides" and the VB compilers
insistence on seeing the Readonly keyword even when it can see that there is
no set accessor.

If a class exposes public events then you can add a handler to them using
the += operator such as...

theForm.Paint+=new PaintEventHandler(this painthandler); // This is the
equivalent of AddHandler

you can remove events similarly with -= being the equivalent of
removehandler.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
B

Brett

Bob Powell said:
Don't worry about it. It's another example of keyword diarrhea in VB. It
fits right in with the idiocy of "overloads overrides" and the VB
compilers insistence on seeing the Readonly keyword even when it can see
that there is no set accessor.

If a class exposes public events then you can add a handler to them using
the += operator such as...

theForm.Paint+=new PaintEventHandler(this painthandler); // This is the
equivalent of AddHandler

Using my code, what is the .Paint part? Would that be InternetExplorer?

I'm not quite sure how to build the following in C#:
[VB.NET]
Private WithEvents IE_Inst As New SHDocVw.InternetExplorer

Maps to things such as:
Public Sub IEDocComplete(ByVal pDisp As Object, ByRef URL As Object) Handles
IE_Inst.DocumentComplete

Brett
 
G

Guest

The usual general format of the C# add handler equivalent is:
<object>.<event> += new <eventhandler>(<event handling method>);

The equivalent C# code is (using our Instant C# VB to C# converter):

private SHDocVw.InternetExplorer IE_Inst = new SHDocVw.InternetExplorer();

//TODO: INSTANT C# TODO TASK: Insert the following converted event handlers
at the end of the 'InitializeComponent' method for forms or into a
constructor for other classes:

IE_Inst.DocumentComplete += new System.EventHandler(IEDocComplete);

public void IEDocComplete(object pDisp, ref object URL)
{
}

David Anton
www.tangiblesoftwaresolutions.com
Home of the Instant C# VB.NET to C# converter
and the Instant VB C# to VB.NET converter

Brett said:
Bob Powell said:
Don't worry about it. It's another example of keyword diarrhea in VB. It
fits right in with the idiocy of "overloads overrides" and the VB
compilers insistence on seeing the Readonly keyword even when it can see
that there is no set accessor.

If a class exposes public events then you can add a handler to them using
the += operator such as...

theForm.Paint+=new PaintEventHandler(this painthandler); // This is the
equivalent of AddHandler

Using my code, what is the .Paint part? Would that be InternetExplorer?

I'm not quite sure how to build the following in C#:
[VB.NET]
Private WithEvents IE_Inst As New SHDocVw.InternetExplorer

Maps to things such as:
Public Sub IEDocComplete(ByVal pDisp As Object, ByRef URL As Object) Handles
IE_Inst.DocumentComplete

Brett
 
B

Bob Powell [MVP]

The .Paint part was just an example of a well-known event.

In your particular case you can declare the object and add the handler like so:

AxSHDocVw.AxWebBrowser axWebBrowser1 = new AxSHDocVw.AxWebBrowser();

this.axWebBrowser1.DocumentComplete += new AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEventHandler(this.axWebBrowser1_DocumentComplete);


--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





Brett said:
Bob Powell said:
Don't worry about it. It's another example of keyword diarrhea in VB. It
fits right in with the idiocy of "overloads overrides" and the VB
compilers insistence on seeing the Readonly keyword even when it can see
that there is no set accessor.

If a class exposes public events then you can add a handler to them using
the += operator such as...

theForm.Paint+=new PaintEventHandler(this painthandler); // This is the
equivalent of AddHandler

Using my code, what is the .Paint part? Would that be InternetExplorer?

I'm not quite sure how to build the following in C#:
[VB.NET]
Private WithEvents IE_Inst As New SHDocVw.InternetExplorer

Maps to things such as:
Public Sub IEDocComplete(ByVal pDisp As Object, ByRef URL As Object) Handles
IE_Inst.DocumentComplete

Brett
 
B

Brett

Some of the online examples I've seen for events in C# make use of
delegates. No one in this thread has used delegates. Why not?

Here are a few examples that use delegates:
http://www.c-sharpcorner.com/Code/2002/Mar/EventHandlingInNetUsingCSDD001.asp

http://www.codeproject.com/csharp/csevents01.asp

Brett


The .Paint part was just an example of a well-known event.

In your particular case you can declare the object and add the handler like
so:

AxSHDocVw.AxWebBrowser axWebBrowser1 = new AxSHDocVw.AxWebBrowser();
this.axWebBrowser1.DocumentComplete += new
AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEventHandler(this.axWebBrowser1_DocumentComplete);

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





Brett said:
Bob Powell said:
Don't worry about it. It's another example of keyword diarrhea in VB. It
fits right in with the idiocy of "overloads overrides" and the VB
compilers insistence on seeing the Readonly keyword even when it can see
that there is no set accessor.

If a class exposes public events then you can add a handler to them using
the += operator such as...

theForm.Paint+=new PaintEventHandler(this painthandler); // This is the
equivalent of AddHandler

Using my code, what is the .Paint part? Would that be InternetExplorer?

I'm not quite sure how to build the following in C#:
[VB.NET]
Private WithEvents IE_Inst As New SHDocVw.InternetExplorer

Maps to things such as:
Public Sub IEDocComplete(ByVal pDisp As Object, ByRef URL As Object)
Handles
IE_Inst.DocumentComplete

Brett
 
B

Brett

I'm getting this error now on the line below:
C:\myFiles\myprogC#\Main\IE.cs(31): Method 'Mail.IE.IEDocComplete(object,
ref object)' does not match delegate 'void System.EventHandler(object,
System.EventArgs)'

[for this code]
IE_Inst.DocumentComplete += new System.EventHandler(IEDocComplete);

[method defined here]
public void IEDocComplete(object pDisp, ref object url)
{

What exactly is it asking for?

Thanks,
Brett



The .Paint part was just an example of a well-known event.

In your particular case you can declare the object and add the handler like
so:

AxSHDocVw.AxWebBrowser axWebBrowser1 = new AxSHDocVw.AxWebBrowser();
this.axWebBrowser1.DocumentComplete += new
AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEventHandler(this.axWebBrowser1_DocumentComplete);

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





Brett said:
Bob Powell said:
Don't worry about it. It's another example of keyword diarrhea in VB. It
fits right in with the idiocy of "overloads overrides" and the VB
compilers insistence on seeing the Readonly keyword even when it can see
that there is no set accessor.

If a class exposes public events then you can add a handler to them using
the += operator such as...

theForm.Paint+=new PaintEventHandler(this painthandler); // This is the
equivalent of AddHandler

Using my code, what is the .Paint part? Would that be InternetExplorer?

I'm not quite sure how to build the following in C#:
[VB.NET]
Private WithEvents IE_Inst As New SHDocVw.InternetExplorer

Maps to things such as:
Public Sub IEDocComplete(ByVal pDisp As Object, ByRef URL As Object)
Handles
IE_Inst.DocumentComplete

Brett
 
S

Steve Walker

Brett <[email protected]> said:
I'm getting this error now on the line below:
C:\myFiles\myprogC#\Main\IE.cs(31): Method 'Mail.IE.IEDocComplete(object,
ref object)' does not match delegate 'void System.EventHandler(object,
System.EventArgs)'

[for this code]
IE_Inst.DocumentComplete += new System.EventHandler(IEDocComplete);

[method defined here]
public void IEDocComplete(object pDisp, ref object url)
{

What exactly is it asking for?

A method to call with the signature "void Foo(object x, System.EventArgs
y)", not one with the signature "void IEDocComplete(object pDisp, ref
object url)".
 
B

Brett

Steve Walker said:
Brett <[email protected]> said:
I'm getting this error now on the line below:
C:\myFiles\myprogC#\Main\IE.cs(31): Method 'Mail.IE.IEDocComplete(object,
ref object)' does not match delegate 'void System.EventHandler(object,
System.EventArgs)'

[for this code]
IE_Inst.DocumentComplete += new System.EventHandler(IEDocComplete);

[method defined here]
public void IEDocComplete(object pDisp, ref object url)
{

What exactly is it asking for?

A method to call with the signature "void Foo(object x, System.EventArgs
y)", not one with the signature "void IEDocComplete(object pDisp, ref
object url)".

I understand the signatures are different but it works in VB.NET.

[VB.NET code]
Private WithEvents IE_Inst As New SHDocVw.InternetExplorer
Public Sub IEDocComplete(ByVal pDisp As Object, ByRef URL As Object) Handles
IE_Inst.DocumentComplete

How is VB basically getting away with doing the samething I'm doing in c#?

Isn't this saying the same as "Handles IE_Inst.DocumentComplete"
[C#]
IE_Inst.DocumentComplete += new System.EventHandler(IEDocComplete);
public void IEDocComplete(object pDisp, ref object url)
 
S

Steve Walker

Brett <[email protected]> said:
Steve Walker said:
Brett <[email protected]> said:
I'm getting this error now on the line below:
C:\myFiles\myprogC#\Main\IE.cs(31): Method 'Mail.IE.IEDocComplete(object,
ref object)' does not match delegate 'void System.EventHandler(object,
System.EventArgs)'

[for this code]
IE_Inst.DocumentComplete += new System.EventHandler(IEDocComplete);

[method defined here]
public void IEDocComplete(object pDisp, ref object url)
{

What exactly is it asking for?

A method to call with the signature "void Foo(object x, System.EventArgs
y)", not one with the signature "void IEDocComplete(object pDisp, ref
object url)".
I understand the signatures are different but it works in VB.NET.
[VB.NET code]
Private WithEvents IE_Inst As New SHDocVw.InternetExplorer
Public Sub IEDocComplete(ByVal pDisp As Object, ByRef URL As Object) Handles
IE_Inst.DocumentComplete

How is VB basically getting away with doing the samething I'm doing in c#?

Because it ain't wiring it up to a System.EventHandler, it's wiring it
up to a SHDocVw.DWebBrowserEvents2_DocumentCompleteEventHandler. Open
your VB exe with ildasm and you'll see it doing it. You need to do the
same in your C#.

You know, compiling that to have a look at the IL was the first VB I've
done for years. I feel soiled :blush:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top