Button gadget requires double-click sometimes but not always

Just starting out? Need help? Post your questions and find answers here.
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Button gadget requires double-click sometimes but not always

Post by Oso »

I've noticed that on a simple form containing a string input and a button, that if the user enters a value into the string input, and the cursor still remains located inside the input string, a single-click of the button gadget will often not work. The user must click the button again.

However, if the cursor is not inside the string input field (i.e. click anywhere else on the window area before clicking the button), then the button gadget only requires a single click. This I think is inconsistent with Windows applications generally, where the buttons can be single-clicked whenever required. I don't know if this is a behaviour that can be controlled or is a known anomaly, as I've only just started using PB. I also notice that behaviour sometimes varies. If the user clicks inside the string input, but doesn't change the existing input value, then clicks the button, the button only requires a single click. It therefore doesn't seem to be working as it ought to.

Here is the code, which is just a simple example from this forum. I've tried other code examples too that behave in exactly the same way, so it doesn't appear to be anything wrong with the below, unless there's something I've missed.

MyForm.pbf

Code: Select all

; This code is automatically generated by the FormDesigner.
Global Window_0
Global Button_0, String_0, Text_0
Declare buttonEvent(EventType)

Procedure OpenWindow_0(x = 0, y = 0, width = 600, height = 400)
  Window_0 = OpenWindow(#PB_Any, x, y, width, height, "", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget | #PB_Window_ScreenCentered)
  Button_0 = ButtonGadget(#PB_Any, 40, 170, 150, 50, "Click Me")
  String_0 = StringGadget(#PB_Any, 40, 30, 290, 25, "")
  Text_0 = TextGadget(#PB_Any, 40, 70, 200, 30, "This is a label")
EndProcedure

Procedure Window_0_Events(event)
  Select event
    Case #PB_Event_CloseWindow
      ProcedureReturn #False
    Case #PB_Event_Menu
      Select EventMenu()
      EndSelect
    Case #PB_Event_Gadget
      Select EventGadget()
        Case Button_0
          buttonEvent(EventType())          
      EndSelect
  EndSelect
  ProcedureReturn #True
EndProcedure
MyFormEvents.pb

Code: Select all

XIncludeFile "MyForm.pbf"

Procedure buttonEvent(eventType.i)
  SetGadgetText(Text_0, GetGadgetText(String_0))  
EndProcedure

OpenWindow_0()

Repeat
  event = WaitWindowEvent()
Until Window_0_Events(event) = #False
End
Another example, is the simple 'Exit' button, to terminate the programme. The behaviour is the same, in other words, the user must double-click the Exit button, if the cursor is positioned inside the input string, but if the cursor is not inside the input string, then button can be single-clicked.

test3.pbf

Code: Select all

; This code is automatically generated by the FormDesigner.
Global Window_0
Global String_0, Button_0
Declare buttonEvent(EventType)

Procedure OpenWindow_0(x = 0, y = 0, width = 600, height = 400)
  Window_0 = OpenWindow(#PB_Any, x, y, width, height, "", #PB_Window_SystemMenu)
  String_0 = StringGadget(#PB_Any, 40, 20, 200, 40, "")
  Button_0 = ButtonGadget(#PB_Any, 40, 90, 100, 25, "Exit")
EndProcedure

Procedure Window_0_Events(event)
  Select event
    Case #PB_Event_CloseWindow
      ProcedureReturn #False
    Case #PB_Event_Menu
      Select EventMenu()
      EndSelect
    Case #PB_Event_Gadget
      Select EventGadget()
        Case Button_0
          buttonEvent(EventType())          
      EndSelect
  EndSelect
  ProcedureReturn #True
EndProcedure
test3.pb

Code: Select all

XIncludeFile "test3.pbf"
OpenWindow_0()

Procedure buttonEvent(eventType.i)
  End
EndProcedure

Repeat
  event = WaitWindowEvent()
Until Window_0_Events(event) = #False
End
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Button gadget requires double-click sometimes but not always

Post by jacdelad »

The click on the button is determined via the function EventType() and is declared as #PB_EventType_LeftClick:

Code: Select all

; This code is automatically generated by the FormDesigner.
Global Window_0
Global Button_0, String_0, Text_0
Declare buttonEvent(EventType)

Procedure OpenWindow_0(x = 0, y = 0, width = 600, height = 400)
  Window_0 = OpenWindow(#PB_Any, x, y, width, height, "", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget | #PB_Window_ScreenCentered)
  Button_0 = ButtonGadget(#PB_Any, 40, 170, 150, 50, "Click Me")
  String_0 = StringGadget(#PB_Any, 40, 30, 290, 25, "")
  Text_0 = TextGadget(#PB_Any, 40, 70, 200, 30, "This is a label")
EndProcedure

Procedure Window_0_Events(event)
  Select event
    Case #PB_Event_CloseWindow
      ProcedureReturn #False
    Case #PB_Event_Menu
      Select EventMenu()
      EndSelect
    Case #PB_Event_Gadget
      Select EventGadget()
        Case Button_0
          Select EventType()
            Case #PB_EventType_LeftClick
              buttonEvent(EventType())     
          EndSelect
      EndSelect
  EndSelect
  ProcedureReturn #True
EndProcedure

Procedure buttonEvent(eventType.i)
  SetGadgetText(Text_0, GetGadgetText(String_0))  
EndProcedure

OpenWindow_0()

Repeat
  event = WaitWindowEvent()
Until Window_0_Events(event) = #False
End
All in all I would restructure the eventloop (it's easier to expand it later, if needed):

Code: Select all

; This code is automatically generated by the FormDesigner.
Global Window_0
Global Button_0, String_0, Text_0

Procedure OpenWindow_0(x = 0, y = 0, width = 600, height = 400)
  Window_0 = OpenWindow(#PB_Any, x, y, width, height, "", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget | #PB_Window_ScreenCentered)
  Button_0 = ButtonGadget(#PB_Any, 40, 170, 150, 50, "Click Me")
  String_0 = StringGadget(#PB_Any, 40, 30, 290, 25, "")
  Text_0 = TextGadget(#PB_Any, 40, 70, 200, 30, "This is a label")
EndProcedure

OpenWindow_0()

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    ;#PB_Event_Menu only if you use menus or shortcuts
    Case #PB_Event_Menu
      Select EventMenu()
      EndSelect
    Case #PB_Event_Gadget
      Select EventType()
        Case #PB_EventType_LeftClick
          Select EventGadget()
            Case button_0
              SetGadgetText(Text_0, GetGadgetText(String_0))  
          EndSelect
      EndSelect
  EndSelect
ForEver
BTW: No need for "End" at the end of your code, just if you want to pass a value or stop the program mid-code.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Button gadget requires double-click sometimes but not always

Post by jacdelad »

BTW, Oso, I enjoy how you discover PureBasic; you seem to be very enthusiastically! You can also bind certain events to functions (this sometimes makes sense, like a function which is called when a window is resized), but I would advise you to firstly get firm with eventloops.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Button gadget requires double-click sometimes but not always

Post by Oso »

jacdelad wrote: Wed Aug 03, 2022 5:05 pm All in all I would restructure the eventloop (it's easier to expand it later, if needed):
Thanks for taking a look. Your code may have slightly improved the behaviour, but it still happens, just not so often. It looks like a timing problem to me. I've just taken a screen capture video of it (with your code) to show it happening on my screen. In my example, I did the following.

1. Entered "Hello" and clicked "Click Me"

It correctly changes the text to be "Hello"

2. Entered "aaaaaaa" and clicked "Click Me"

It correctly changes the text to be "Hello aaaaaaa"

3. Entered "bbbbbbb" and clicked "Click Me"

No effect from the button. It isn't a programme logic problem, but something wrong with timing or something along those lines. It seems to happen about 25% of the time. Whereas with my example code it was maybe 75% of the time. It also makes a difference where the cursor is situated at the time the button is clicked. In other words, which gadget "has focus". If focus is not with the input string, there's no problem with it.

https://www.veed.io/view/c988eb96-beac- ... idget=true
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Button gadget requires double-click sometimes but not always

Post by jacdelad »

Umm...I cannot explain that. That's how I always handle events and it always works flawlessly (even now). Maybe one of the Gurus can help.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Button gadget requires double-click sometimes but not always

Post by Oso »

jacdelad wrote: Wed Aug 03, 2022 5:31 pm BTW, Oso, I enjoy how you discover PureBasic; you seem to be very enthusiastically! You can also bind certain events to functions (this sometimes makes sense, like a function which is called when a window is resized), but I would advise you to firstly get firm with eventloops.
Yes! It seems to be very productive and easy to get things to work. I've been using other tools during the past couple of years, such as C# and Xamarin but they have significant learning curves to master them. I'm a programmer but I also need to think about the development time (i.e. cost).
Last edited by Oso on Wed Aug 03, 2022 6:07 pm, edited 1 time in total.
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Button gadget requires double-click sometimes but not always

Post by Oso »

jacdelad wrote: Wed Aug 03, 2022 5:38 pm Umm...I cannot explain that. That's how I always handle events and it always works flawlessly (even now). Maybe one of the Gurus can help.
Yeah, it's interesting that changing the flow of code had a noticeable effect on how often it happened. That's why I wondered if there's a timing problem. I'm also running inside a VirtualBox guest, so performance could be affected, but to be honest VirtualBox guests don't normally behave too differently.

What I could do, is swap the drive in my machine with a Windows Server 2016 O/S and run it directly. I'm a bit limited at the moment because I'm in Portugal with only a trusty laptop.
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Button gadget requires double-click sometimes but not always

Post by Oso »

I've done some more testing on this (still inside VirtualBox). I've found it's caused by the time between typing in the string input and waiting before clicking the button. If I type text into the string input and quickly single-click the button, it ignores the button. If I type something and then wait a second, the button works fine.

A video to show what's happening is at https://www.veed.io/view/5b17b2c6-6d4f- ... idget=true

In the video, I begin by typing "Hello" and waiting a moment before clicking the button. The button works fine.

I then type "Quick" and quickly click the button. Nothing happens. I then repeat this several times, where it shows "Quick" and "Slow" and each time I do it quickly, the button doesn't operate. Just to be clear, it isn't the typing input speed that matters -- it's how quickly I click the button after finishing typing. Interesting stuff this!

EDIT: @jacdelad I was wrong about your code making a slight difference. It's entirely the speed between entering an input and clicking the button that makes the difference. I've gone back to my original code and compared the results with yours. They are both the same. If I wait a second after typing the string, it's fine. Maybe I'm just being too quick.
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Button gadget requires double-click sometimes but not always

Post by infratec »

Your code from the first post works without problems in win10.

A button event don't need to check the event type, it only generates one event.

Your code looks fine.
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Button gadget requires double-click sometimes but not always

Post by jacdelad »

infratec wrote: Wed Aug 03, 2022 6:29 pm Your code from the first post works without problems in win10.

A button event don't need to check the event type, it only generates one event.

Your code looks fine.
...though I wouldn't recommend to learn it this way, because I think the EventType should be evaluated too (or different events would trigger the function).
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Button gadget requires double-click sometimes but not always

Post by jacdelad »

Oso wrote: Wed Aug 03, 2022 6:04 pm I then type "Quick" and quickly click the button. Nothing happens. I then repeat this several times, where it shows "Quick" and "Slow" and each time I do it quickly, the button doesn't operate. Just to be clear, it isn't the typing input speed that matters -- it's how quickly I click the button after finishing typing. Interesting stuff this!

EDIT: @jacdelad I was wrong about your code making a slight difference. It's entirely the speed between entering an input and clicking the button that makes the difference. I've gone back to my original code and compared the results with yours. They are both the same. If I wait a second after typing the string, it's fine. Maybe I'm just being too quick.
When was your computer manufactured? :mrgreen:
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: Button gadget requires double-click sometimes but not always

Post by BarryG »

Oso wrote: Wed Aug 03, 2022 4:26 pmI've noticed that on a simple form containing a string input and a button, that if the user enters a value into the string input, and the cursor still remains located inside the input string, a single-click of the button gadget will often not work. The user must click the button again.
Oso wrote: Wed Aug 03, 2022 4:26 pmAnother example, is the simple 'Exit' button, to terminate the programme. The behaviour is the same, in other words, the user must double-click the Exit button, if the cursor is positioned inside the input string, but if the cursor is not inside the input string, then button can be single-clicked.
I can't duplicate these problems here. You said you're using PureBasic under a virtual machine: I suggest you try your code on a real PC, before assuming PureBasic is the cause. What you've described are very simple operations and if they were a bug, they would've been noticed years ago. I am 99% sure your VM is the cause.
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Button gadget requires double-click sometimes but not always

Post by Oso »

I'm pleased to report back that the problem with the button performance is resolved. I believe it was a keyboard problem -- very likely a sticking key that didn't release for a half a second, or perhaps the keyboard is failing.

I also put together an equivalent Visual Basic test. VB didn't exhibit the problem, but PB seemed sensitive to it. I then disassembled the machine to install a Windows Server 2016 SSD. I wanted to check the result with a native O/S, rather than a virtual guest. When I put the machine back together again, everything was fine. It's been one of those kinds of annoying problems. Thanks to all for the help, it's appreciated.
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Button gadget requires double-click sometimes but not always

Post by Oso »

jacdelad wrote: Wed Aug 03, 2022 6:56 pm When was your computer manufactured? :mrgreen:
The machine isn't new, but the twin-floppy drives are still working fine. :-)

Image
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Button gadget requires double-click sometimes but not always

Post by mk-soft »

Everything runs on virtual machines, except macOS (host machine), and there have been no problems.

It is important to ALWAYS HAVE ONLY ONE EVENT LOOP (WaitWindowEvent). This is the interface between the user and a program.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply