VBAndCs / sVB-Small-Visual-Basic

Small Visual Basic (sVB) is an educational programming language, created by Eng. Mohammad Hamdy as an evolved version of Microsoft Small Basic (SB). It is meant to be easier and more powerful at the same time, to introduce programming basics to kids and beginners of any age, provided that they can use the English keyboard on the Windows OS.
Other
232 stars 16 forks source link

Press a key single time will arise OnKeyDown event two times #41

Closed dynamicboy closed 3 months ago

dynamicboy commented 9 months ago

Verrsion: 2.8.7.5 Details: Step1: In Form Code, input some code as below:

Me.OnKeyDown = Form1_OnKeyDown
Sub Form1_OnKeyDown
   TW.WriteLine("Time: " + Clock.ElapsedMilliseconds + ", You pressed " + Event.LastKey)
EndSub

Step2: Click Run Step3: Press Key 0 in Numpad just one time

Output: 2024-01-01_003804

dynamicboy commented 9 months ago

I found this behavior when I use space key to pause and resume a avatar.

VBAndCs commented 9 months ago

Hi @dynamicboy

You are the cause of these issue, because you insist to use sVB as if it is SB.

I told you in a previous reply that sVB uses the naming convention ControlName_EventName to handle events as VB6 does, so you can just use:


Sub Form1_OnKeyDown

   TW.WriteLine("Time: " + Clock.ElapsedMilliseconds + ", You pressed " + Event.LastKey)

EndSub

and this will handle the Form1.OnKeyDown event. I also told you that you can generate this handler sub by choosing the Form1 and OnKeyDown from the upper dropdown lists.

Now, when you add this unnecessary code: Me.OnKeyDown = Form1_OnKeyDown

You actually register the handler twice, so it will be called two times!

And if you add the same line of code more times, you will have as many handlers as what you asked for. In fact this is the way VB .NET and C# works.

I can change this behavior, but it will need a lot of code to store a dictionary of handlers for all controls and events, so I can check if the handler is added before not to add the same handler twice, but this will need a lot of unnecessary work.

So, my advice here is to avoid registering the controls event handlers manually and let sVB do the work for you. There are two cases where you need to explicitly add the handler by code:

  1. When using the SB timer or the sVB timer control.

  2. when you want to use the same handler for more than one control, instead of repeating the same code for each control. For example, in the "Simple calculator" application in the sVB samples folder, the TxtNum1_OnTextInput sub is used to handle the TxtNum1.OnTextInput event, and to make txtNum2 use it also we use this code as the global area: txtNum2.OnTextInput = TxtNum1_OnTextInput

I made some improvements in the sVB events system after your last issue, like fixing the Form.OnClosed event which was not fired when there is only one opened form because the program closes immediately. sVB 2.8.8 will have these fixes beside the new properties window.

You are helping a lot, so keep up the good work.

Thanks


From: dynamicboy @.> Sent: Sunday, December 31, 2023 4:44 PM To: VBAndCs/sVB-Small-Visual-Basic @.> Cc: Subscribed @.***> Subject: Re: [VBAndCs/sVB-Small-Visual-Basic] Press a key single time will arise OnKeyDown event two times (Issue #41)

I found this behavior when I use space key to pause and resume a avatar.

— Reply to this email directly, view it on GitHubhttps://github.com/VBAndCs/sVB-Small-Visual-Basic/issues/41#issuecomment-1872991726, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ALQ5MVWQQMNV2CSGMVPCDITYMGJALAVCNFSM6AAAAABBILRP4GVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQNZSHE4TCNZSGY. You are receiving this because you are subscribed to this thread.Message ID: @.***>

dynamicboy commented 9 months ago

@VBAndCs I got it, thanks for the information about event handling. I'm trying to create as many applications as possible to learn small visual basic, based on my previous programming knowledge. I didn't thoroughly read the reference book, and didn't notice the event droplist on the top of the code form, my bad.

I just corrected my code, the pause and resume mechanism is working now.

VBAndCs commented 9 months ago

I decided to fix this issue, because sVB syntax deon't allow to remove event handlers (while VB .NET does). I am working on it for more than 3 hours now, but it will allow sVB programmers to change the event handler at anytime while the program is running without suffering any side effects. All these fixes will be valid soon in sVB 2.8.8


From: dynamicboy @.> Sent: Sunday, December 31, 2023 7:50 PM To: VBAndCs/sVB-Small-Visual-Basic @.> Cc: Mohammad Hamdy Ghanem @.>; Mention @.> Subject: Re: [VBAndCs/sVB-Small-Visual-Basic] Press a key single time will arise OnKeyDown event two times (Issue #41)

@VBAndCshttps://github.com/VBAndCs I got it, thanks for the information about event handling. I'm trying to create as many applications as possible to learn small visual basic, based on my previous programming knowledge. I didn't thoroughly read the reference book, and didn't notice the event droplist on the top of the code form, my bad.

I just corrected my code, the pause and resume mechanism is working now.

— Reply to this email directly, view it on GitHubhttps://github.com/VBAndCs/sVB-Small-Visual-Basic/issues/41#issuecomment-1873025998, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ALQ5MVVAXAOHWBLJEYEU2RLYMG6YLAVCNFSM6AAAAABBILRP4GVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQNZTGAZDKOJZHA. You are receiving this because you were mentioned.Message ID: @.***>