New topic: Overriding events : Sub classes - specifically KeyDown event
<http://forums.realsoftware.com/viewtopic.php?t=47783> Page 1 of 1 [ 3 posts ] Previous topic | Next topic Author Message superjacent Post subject: Overriding events : Sub classes - specifically KeyDown eventPosted: Fri May 03, 2013 9:35 pm Joined: Sat Oct 01, 2005 4:47 am Posts: 109 Location: Melbourne, Australia I'm having no end of trouble coming to grips with the process of sub-classing and then overriding events. In my project it wasn't working so I've resorted to an example from RB Curriculum Guide, Chapter 11. I've added to the project to help/better explain the issue. https://dl.dropboxusercontent.com/u/946 ... Polym2.zip A Sub-Class is created based on a TextField. The sub-class has code in the KeyDown event. This code restricts keystrokes to numbers only. An Event Definition is then created, called KeyDown, the same definition as the original KeyDown event (that has been populated with code - restrict to numbers). My understanding is that if the KeyDown event is to be further referenced, say in a sub-class of this sub-class (or from a window) then an Event Definition has to be created for this purpose otherwise a KeyDown event would appear not to exist. Now placing this Sub-Class control on a window the control works as intended, only numbers are accepted. Whilst at the Window, because an Event Definition was created called KeyDown, there is a KeyDown event present, albeit empty of code. Entering code into the KeyDown event of the sub-classed control on the window and when a key is pressed the KeyDown event is called. In my addition to the example project I added code to merely display the key pressed, display that key in another TextField control. Yes, only numbers are returned, if pressing a non-number, the KeyDown event is not called at the window level as the Super Class has correctly handled it. So far so good. Lets now create another Sub-Class based upon our original sub-class (same as example project) whereby this time we need to restrict the numbers entered to a certain range (<500). Placing that sub-class on a window, it works, numbers only are accepted AND only to a certain range (<500). Here is my problem or issue. With this new sub-class of a sub-class, I also created an Event Definition called KeyDown. I want to reference the KeyDown event at the window, again, merely displaying the key pressed in another TextField control. This time though, the KeyDown event of this particular sub-class (restrict numbers to range) at the Window is not being called, I can not get the key pressed to be displayed in another TextField. I feel like I am missing something really basic but can't figure it out. The example project clearly shows the behaviour I've described above. Any clues or hints please. _________________ Steve rs2012 r2.1 Windows 7. Top zmarc Post subject: Re: Overriding events : Sub classes - specifically KeyDown ePosted: Fri May 03, 2013 10:31 pm Joined: Wed Nov 14, 2007 3:06 am Posts: 7 Location: Oregon Looking at your code, I think I see your problem. Your first subclass, NumEdit, calls KeyDown: If (Key >= "0") and (Key <= "9") then Return KeyDown(Key) That code there passes Key on to the parent window. Your second subclass, LimitNumEdit, does *not* call Keydown: Dim ChangedString As String = Left(Text, SelStart) + Key + Mid(Text, SelStart + SelLength + 1) if Val(ChangedString) <= Maximum then Return False Else Return True End if It's returning true or false, which just means it either handles the event or not, but it doesn't pass on the key that was pressed. If I understood your post correctly, that was your problem: the first class was passing on the key to the window's KeyDown event, but the second was not. To fix this, you'd need to change the code in the second class to return keydown(key). When I did that: Dim ChangedString As String = Left(Text, SelStart) + Key + Mid(Text, SelStart + SelLength + 1) if Val(ChangedString) <= Maximum then Return False Else Return keydown(key) End if It works the same as the first textfield. _________________ Marc Zeedar Publisher Real Studio Developer Magazine http://www.rsdeveloper.com Top superjacent Post subject: Re: Overriding events : Sub classes - specifically KeyDown ePosted: Fri May 03, 2013 10:55 pm Joined: Sat Oct 01, 2005 4:47 am Posts: 109 Location: Melbourne, Australia Thanks Marc, zmarc wrote:It's returning true or false, which just means it either handles the event or not, but it doesn't pass on the key that was pressed. Reading your reply just above now makes sense to me. I just couldn't see it (obviously). superjacent wrote:Dim ChangedString As String = Left(Text, SelStart) + Key + Mid(Text, SelStart + SelLength + 1) if Val(ChangedString) <= Maximum then Return False Else Return keydown(key) End if Actually, is the other way around, the return values that is, but yes, it works. Thank you very much. _________________ Steve rs2012 r2.1 Windows 7. Top Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending Page 1 of 1 [ 3 posts ] -- Over 1500 classes with 29000 functions in one REALbasic plug-in collection. The Monkeybread Software Realbasic Plugin v9.3. http://www.monkeybreadsoftware.de/realbasic/plugins.shtml [email protected]
