473,666 Members | 2,065 Online
Bytes | Software Development & Data Engineering Community
+ 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.Interne tExplorer

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

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

Thanks,
Brett
Nov 17 '05 #1
11 18541
> What is the C# equivalent for this VB.NET code: Private WithEvents
IE_Inst As New SHDocVw.Interne tExplorer


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(Ha ndleEvent_Funct ion1);
SHDocVw.Event2 += new EventHandler(Ha ndleEvent_Funct ion2);
....
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.Interne tExplorer


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(Ha ndleEvent_Funct ion1);
SHDocVw.Event2 += new EventHandler(Ha ndleEvent_Funct ion2);
....
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 PaintEventHandl er(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$******** *****@TK2MSFTNG P10.phx.gbl...
What is the C# equivalent for this VB.NET code:
Private WithEvents IE_Inst As New SHDocVw.Interne tExplorer

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

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

Thanks,
Brett

Nov 17 '05 #4

"Bob Powell [MVP]" <bob@_spamkille r_bobpowell.net > wrote in message
news:%2******** ********@TK2MSF TNGP09.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 PaintEventHandl er(this painthandler); // This is the
equivalent of AddHandler


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

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

Maps to things such as:
Public Sub IEDocComplete(B yVal pDisp As Object, ByRef URL As Object) Handles
IE_Inst.Documen tComplete

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.Interne tExplorer IE_Inst = new SHDocVw.Interne tExplorer();

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

IE_Inst.Documen tComplete += new System.EventHan dler(IEDocCompl ete);

public void IEDocComplete(o bject 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@_spamkille r_bobpowell.net > wrote in message
news:%2******** ********@TK2MSF TNGP09.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 PaintEventHandl er(this painthandler); // This is the
equivalent of AddHandler


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

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

Maps to things such as:
Public Sub IEDocComplete(B yVal pDisp As Object, ByRef URL As Object) Handles
IE_Inst.Documen tComplete

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.AxWeb Browser axWebBrowser1 = new AxSHDocVw.AxWeb Browser();

this.axWebBrows er1.DocumentCom plete += new AxSHDocVw.DWebB rowserEvents2_D ocumentComplete EventHandler(th is.axWebBrowser 1_DocumentCompl ete);
--
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******** ******@TK2MSFTN GP09.phx.gbl...

"Bob Powell [MVP]" <bob@_spamkille r_bobpowell.net > wrote in message
news:%2******** ********@TK2MSF TNGP09.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 PaintEventHandl er(this painthandler); // This is the
equivalent of AddHandler


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

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

Maps to things such as:
Public Sub IEDocComplete(B yVal pDisp As Object, ByRef URL As Object) Handles
IE_Inst.Documen tComplete

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@_spamkille r_bobpowell.net > wrote in message
news:ui******** ******@TK2MSFTN GP12.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.AxWeb Browser axWebBrowser1 = new AxSHDocVw.AxWeb Browser();
this.axWebBrows er1.DocumentCom plete += new
AxSHDocVw.DWebB rowserEvents2_D ocumentComplete EventHandler(th is.axWebBrowser 1_DocumentCompl ete);

--
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******** ******@TK2MSFTN GP09.phx.gbl...

"Bob Powell [MVP]" <bob@_spamkille r_bobpowell.net > wrote in message
news:%2******** ********@TK2MSF TNGP09.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 PaintEventHandl er(this painthandler); // This is the
equivalent of AddHandler


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

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

Maps to things such as:
Public Sub IEDocComplete(B yVal pDisp As Object, ByRef URL As Object)
Handles
IE_Inst.Documen tComplete

Brett


Nov 17 '05 #8
I'm getting this error now on the line below:
C:\myFiles\mypr ogC#\Main\IE.cs (31): Method 'Mail.IE.IEDocC omplete(object,
ref object)' does not match delegate 'void System.EventHan dler(object,
System.EventArg s)'

[for this code]
IE_Inst.Documen tComplete += new System.EventHan dler(IEDocCompl ete);

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

What exactly is it asking for?

Thanks,
Brett

"Bob Powell [MVP]" <bob@_spamkille r_bobpowell.net > wrote in message
news:ui******** ******@TK2MSFTN GP12.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.AxWeb Browser axWebBrowser1 = new AxSHDocVw.AxWeb Browser();
this.axWebBrows er1.DocumentCom plete += new
AxSHDocVw.DWebB rowserEvents2_D ocumentComplete EventHandler(th is.axWebBrowser 1_DocumentCompl ete);

--
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******** ******@TK2MSFTN GP09.phx.gbl...

"Bob Powell [MVP]" <bob@_spamkille r_bobpowell.net > wrote in message
news:%2******** ********@TK2MSF TNGP09.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 PaintEventHandl er(this painthandler); // This is the
equivalent of AddHandler


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

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

Maps to things such as:
Public Sub IEDocComplete(B yVal pDisp As Object, ByRef URL As Object)
Handles
IE_Inst.Documen tComplete

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\myp rogC#\Main\IE.c s(31): Method 'Mail.IE.IEDocC omplete(object,
ref object)' does not match delegate 'void System.EventHan dler(object,
System.EventAr gs)'

[for this code]
IE_Inst.Docume ntComplete += new System.EventHan dler(IEDocCompl ete);

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

What exactly is it asking for?


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

--
Steve Walker
Nov 17 '05 #10

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

Similar topics

3
3571
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 VB.NET (meaning they understand the .NET Framwork, how to use the IDE, the classes and components available, etc.) then would they really need to "learn" C#? I have done some coding in C#, I wrote an entire app in it, but I use VB.NET whe...
2
5061
by: Andreas Klemt | last post by:
Hello, what is the difference between a) Protected WithEvents myClassName b) Protected myClassName Thanks, Andreas
5
9819
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 the tablerow style property to "None", the row (And embedded table) briefly disappear, but then bounce right back into sight. Any ideas on why it's doing this and how to make the style.display = "None" stick?
0
1353
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 WithEvents lblLabel1 As System.Web.UI.WebControls.Label Why do they persist and is there a was to remove them easily? /M
0
1912
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. This is properly done, but the events which should be triggered with the button.click method are not triggered in Word. I don't understand because it properly works in the 3 others host applications. If someone has an issue, I would be most grateful....
0
2434
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. This is properly done, but the events which should be triggered with the button.click method are not triggered in Word. I don't understand because it properly works in the 3 others host applications. If someone has an issue, I would be most grateful....
0
4196
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 ApplicationContext. This error message comes from deeper levels must be thrown (mscorlib.dll?!) ?.
16
1821
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
6712
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 System.Web.HttpCookie Imports System.Web.Mail Imports System.Web.Mail.SmtpMail
0
8869
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7386
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6198
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5664
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4368
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2771
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1775
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.