Denaes Posted July 28, 2004 Posted July 28, 2004 Like many of my questions, this seems like it should be a standard easy answer, but I don't seem to find an easy answer. I have a few nud's on my form. Nothing fancy, they just go from 0-20, advancing by one each time. What I need to know is if they pushed up, or if they pushed down. I can can store private variables to act like a "last value" and compare the CurrentValue and LastValue to find out if it went up or down. But is there another way? Quote
Hamburger1984 Posted July 28, 2004 Posted July 28, 2004 see attachment.. there is - as you guessed - a better/easier way than storing the old value and checking etc.. and btw. to get such class/control-internal information I always use Reflector.. maybe you should also try it...UpDownAdvanced.zip Quote
Denaes Posted July 28, 2004 Author Posted July 28, 2004 see attachment.. there is - as you guessed - a better/easier way than storing the old value and checking etc.. and btw. to get such class/control-internal information I always use Reflector.. maybe you should also try it... thank you for the help. let me see if I got what you did in the code correct - for my understanding. ei is EventInfo, which holds event information. doing a For Each loop to cycle through the controls on the form. Each one you grab the event info for "UpDown". Check to make sure EI has a value. then you do the ei.AddEventHandler, telling it the target is the control that just ran through the loop, giving it an UpDown handler and telling the event to occur at the procedure nudUpDown. You got the details from the Reflector... What I'd like to know is where you looked. I found the numericUpDown class under System.Windows.Forms, but even knowing what you showed me, I didn't find anything. I think using a variation of this code, I can just send the event into my main class directly and save on like 20 lines of clutter, checking for the same crud on the form. Quote
Hamburger1984 Posted July 28, 2004 Posted July 28, 2004 thank you for the help. let me see if I got what you did in the code correct - for my understanding. you're welcome.. ;) ei is EventInfo' date=' which holds event information.[/quote'] it points to an Event of an Object (in this case of a Control) doing a For Each loop to cycle through the controls on the form. Each one you grab the event info for "UpDown". ..almost - I cycle throught the Controls-Collection of the NumericUpDown (not the Form) Check to make sure EI has a value. then you do the ei.AddEventHandler, telling it the target is the control that just ran through the loop, giving it an UpDown handler and telling the event to occur at the procedure nudUpDown. in other words I attach my own EventHandler if the Control actually has a "UpDown"-Event (it's the class "System.Windows.Forms.UpDownBase.UpDownButtons" - use Reflector to take a look at it) You got the details from the Reflector... What I'd like to know is where you looked. I found the numericUpDown class under System.Windows.Forms' date=' but even knowing what you showed me, I didn't find anything.[/quote'] see last comment + NumericUpDown is derived from UpDownBase (always take a look in the "Base Types" entry in Reflector + you might have to set some Options in Reflector (View->Options->check all boxes in the Groupbox "Visibility"->Ok) I think using a variation of this code' date=' I can just send the event into my main class directly and save on like 20 lines of clutter, checking for the same crud on the form.[/quote'] yeah.. as I said this seems to be the cleaner version ;) Hope this helps! Andreas Quote
Hamburger1984 Posted July 28, 2004 Posted July 28, 2004 (edited) ..I just figured out another simple way.. see attachment - basically it's just Inheriting a Class from "NumericUpDown" and overriding the Methods "UpButton" and "DownButton"... maybe that's easier to handle/understand + it also reacts to the Up- and Down-Key.. AndreasUpDownAdvanced.zip Edited July 28, 2004 by PlausiblyDamp Quote
Denaes Posted July 28, 2004 Author Posted July 28, 2004 ..almost - I cycle throught the Controls-Collection of the NumericUpDown (not the Form) wait a second. nudTest is a numericUpDown and you use this loop: For Each c As Control In nudTest.Controls Next so you're cycling through each of the controls IN the nudTest control? And you keep going through until you find the one with the "UpDown" event. I hadn't even realized that was possible... or that controls had controls... though I should have. That's what happens when I make controls out of multiple controls. So you're going through the controls used to build the nud and looking for the "UpDown" event in one of them? if I got that right, I think my increasing of component based programming, the framework and how to dig things out just increased :p Quote
Denaes Posted July 28, 2004 Author Posted July 28, 2004 ..I just figured out another simple way.. see attachment - basically it's just Inheriting a Class from "NumericUpDown" and overriding the Methods "UpButton" and "DownButton"... maybe that's easier to handle/understand + it also reacts to the Up- and Down-Key.. Andreas Honestly the second is easier to follow and, IMO, better if you have multiple controls you want to do this with. The first looks better if you want to do it with one or a few controls. What I did was dropped the line in the Inherited Control Class where you set the delegate to work with a specific Sub and compiled it into a new control and added the handlers needed in a "addHandlers" procedure called in formLoad. This is mostly because I currently have 6 of these NUD controls and will probobly have an additional 3-6. Of course doing this all in C# was an interesting exercise in porting it over (I've been doing VB.Net for about a year+ and just started C#) Really both ways have been great learning lessons and honestly I think you should do some tutorials here on the boards for both the situations of adding custom events to classes (like the second) and really digging down into a class to get at it's innards (like the first). Quote
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.