Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / July 2007

Tip: Looking for answers? Try searching our database.

Can I call an event in code?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dom - 31 Jul 2007 16:05 GMT
In the days of VB, it was possible to do the following:  "Call
Button1_Click()", although there was some argument about whether this
was asking for trouble.

Is it possible to do it in CSharp?  The trouble that I see is that
every event handler needs arguments.  But can you do something like
"Raise Button1_Click"?

Dom
zacks@construction-imaging.com - 31 Jul 2007 16:14 GMT
> In the days of VB, it was possible to do the following:  "Call
> Button1_Click()", although there was some argument about whether this
[quoted text clipped - 5 lines]
>
> Dom

ControlName.PerformClick()
Dom - 31 Jul 2007 16:41 GMT
On Jul 31, 11:14 am, za...@construction-imaging.com wrote:

> > In the days of VB, it was possible to do the following:  "Call
> > Button1_Click()", although there was some argument about whether this
[quoted text clipped - 7 lines]
>
> ControlName.PerformClick()

Thanks.  Exactly what I wanted.  I guess the "PerformClick" method was
added since VB people were doing it anyway.
Nicholas Paldino [.NET/C# MVP] - 31 Jul 2007 16:26 GMT
Dom,

   An event handler is nothing more than a method.  That method is called
through a delegate by the source of an event.  However, there is nothing
that is preventing you from calling that method.  To that end, you have to
supply the method with what it expects, in this case, the source of the
event (sender) and the EventArgs instance (or class derived from it).

   Now, depending on the code in the event handler, you could possibly get
away with not providing values for those parameters (null for sender, and
default values for whatever is passed as the second parameter).

   However, if you are trying to synthesize the firing of the event, then
you really should look for a method on the object itself which will fire the
event.

   If you are just trying to share logic between calls, then you should
have another method which takes the parameters you need, and then just call
it from the event handler, passing the appropriate information, and the
other section of your code, again, passing the appropriate information.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> In the days of VB, it was possible to do the following:  "Call
> Button1_Click()", although there was some argument about whether this
[quoted text clipped - 5 lines]
>
> Dom
Ignacio Machin ( .NET/ C# MVP ) - 31 Jul 2007 17:04 GMT
Hi,

> In the days of VB, it was possible to do the following:  "Call
> Button1_Click()", although there was some argument about whether this
[quoted text clipped - 3 lines]
> every event handler needs arguments.  But can you do something like
> "Raise Button1_Click"?

Of course you can, the event handler is just a regular method, you can call
it from code if you want.

Regarding the parameters, depending of the use you give them you can pass
null.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.