Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I have a couple of places in my code where it would be convenient to simulate an event. I looked on the internet but could find nothing about it.

 

For example, I have a date and time picker with an event - dateTimePicker_ValueChanged

 

How do I simulate the value changed so that everything in that block executes?

C#
  • *Experts*
Posted

Can you not just call that function directly? For the "sender" pass in the variable name itself. Pass in dummy arguments for the second one.

 

For example:

dateTimePicker1_ValueChanged(dateTimePicker1, new System.EventArgs());

 

Remember, an event is just a function. The event handler code (usually in InitializeComponent) just hooks the event to call your function, but it's still just a function.

 

-nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

Yep, that works. I realize that I could just take everything out of the event block and put it in a regular method (C# function) but I really wanted to know how to do that anyway.

 

Thanks.

C#
Posted

No such thing as "RaiseEvent" in C# ??

 

In VB (yes, I read your signature, but VB is my home turf) In VB you need to declare an Event

 

e.g

Public Event NothingHappened (sender as Object, e as NothingHappenedEventArgs(

[\code]

 

and then

 

[code]

RaiseEvent NothingHappened (me, New NothingHappenedEventArgs (arg1, arg2))

[\code]

 

(You could use EventArgs as the class for your arguments but I recommend always to derive your own special EventArgs class, this will reduce correction times drastically later in the project. Believe me :) )

.nerd
  • *Gurus*
Posted
Events are probably the biggest difference between C# and VB. With VB, you use its helper functions to add and remove delegates, and even to call the list of delegates. In C# you have direct access to the event and you add and remove delegates yourself, and just call the delegate list like any other function.

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...