aewarnick Posted March 24, 2003 Posted March 24, 2003 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? Quote C#
*Experts* Nerseus Posted March 24, 2003 *Experts* Posted March 24, 2003 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 Quote "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
aewarnick Posted March 24, 2003 Author Posted March 24, 2003 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. Quote C#
Heiko Posted March 25, 2003 Posted March 25, 2003 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 :) ) Quote .nerd
*Gurus* divil Posted March 25, 2003 *Gurus* Posted March 25, 2003 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. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
aewarnick Posted March 25, 2003 Author Posted March 25, 2003 I hope my signature does not offend anyone. If it does I will most certainly change it. Just let me know. Quote C#
Heiko Posted March 25, 2003 Posted March 25, 2003 You need a hug or what? Of course I am not offended. :) Quote .nerd
aewarnick Posted March 25, 2003 Author Posted March 25, 2003 Good. Some people are just touchy and I didn't mean just you, I meant anyone. Quote C#
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.