473,467 Members | 1,570 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to do "WithEvents" in C#?

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
Nov 17 '05 #1
11 18482
> 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/vb...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
Nov 17 '05 #2
'WithEvents' is not required in C#. (It *shouldn't* be required in VB, but
that's a different story).

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

"Markus Thurner" wrote:
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/vb...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

Nov 17 '05 #3
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.

"Brett" <no@spam.com> wrote in message
news:u$*************@TK2MSFTNGP10.phx.gbl...
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

Nov 17 '05 #4

"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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
Nov 17 '05 #5
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" wrote:

"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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

Nov 17 '05 #6
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_DocumentCompleteEvent Handler(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" <no@spam.com> wrote in message news:OQ**************@TK2MSFTNGP09.phx.gbl...

"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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

Nov 17 '05 #7
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/20...ingCSDD001.asp

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

Brett
"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:ui**************@TK2MSFTNGP12.phx.gbl...
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_DocumentCompleteEvent Handler(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" <no@spam.com> wrote in message
news:OQ**************@TK2MSFTNGP09.phx.gbl...

"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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


Nov 17 '05 #8
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

"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:ui**************@TK2MSFTNGP12.phx.gbl...
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_DocumentCompleteEvent Handler(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" <no@spam.com> wrote in message
news:OQ**************@TK2MSFTNGP09.phx.gbl...

"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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


Nov 17 '05 #9
In message <u5*************@tk2msftngp13.phx.gbl>, Brett <no@spam.com>
writes
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)".

--
Steve Walker
Nov 17 '05 #10

"Steve Walker" <st***@otolith.demon.co.uk> wrote in message
news:Wv**************@otolith.demon.co.uk...
In message <u5*************@tk2msftngp13.phx.gbl>, Brett <no@spam.com>
writes
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)".

--
Steve Walker


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)
Nov 17 '05 #11
In message <#E*************@TK2MSFTNGP15.phx.gbl>, Brett <no@spam.com>
writes

"Steve Walker" <st***@otolith.demon.co.uk> wrote in message
news:Wv**************@otolith.demon.co.uk...
In message <u5*************@tk2msftngp13.phx.gbl>, Brett <no@spam.com>
writes
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_DocumentCompleteEventHa ndler. 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 :o)

--
Steve Walker
Nov 17 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Chris | last post by:
Wait.. before you flame.. If someone can program in Java, or Javascript, or C, or (insert your language here that uses basically the same syntax as C#), and that person knew how to program in...
2
by: Andreas Klemt | last post by:
Hello, what is the difference between a) Protected WithEvents myClassName b) Protected myClassName Thanks, Andreas
5
by: Sue | last post by:
Help! I have an asp table with an embedded table. The asp tablerow that contains this table has a static ID assigned of "FilterRow2" (see snippets of code below). When I click on the button to set...
0
by: hansiman | last post by:
I sometimes see references to controls that has been removed from the aspx page in the code behind. The references linger in the region "Web Form Designer Generated Code" and look like Protected...
0
by: maitrepoy | last post by:
Hello I have to create a small addin which works on Powerpoint, Word, Outlook, and Excel on Office 2000, XP, and 2003. This addin consists in adding 2 new Buttons in the "File" Menu of office....
0
by: maitrepoy | last post by:
hello I have to create a small addin which works on Powerpoint, Word, Outlook, and Excel on Office 2000, XP, and 2003. This addin consists in adding 2 new Buttons in the "File" Menu of office....
0
by: Ralf Gedrat | last post by:
Hello! I have a Application, this throws after some time following exception: Item has already been added. Key in dictionary: "- 1" key being added: "- 1" I use Application.Run with...
16
by: abc my vclass | last post by:
C# has VB's "with" command? I like VB's with command, why don't know C# has it or not?
1
by: samarthkumar84 | last post by:
Hi I had used following code for sending e-mail but facing this problem. I want to send this e-mail in ASP.NET using VB.NET code. I am attaching both code an output. CODE Imports...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.