Recent

Author Topic: [Solved] Radiobuttons in a groupbox  (Read 1867 times)

MarkMLl

  • Hero Member
  • *****
  • Posts: 8551
[Solved] Radiobuttons in a groupbox
« on: October 12, 2022, 09:01:22 pm »
What is the expected behaviour of two TRadioButtons in a TGroupBox (together with other controls)?

I was expecting that they would be independent and need event handlers to plumb them together, and slightly surprised to find that the LCL associated them automatically.

However I'm having problems setting their initial state under software control (to reflect the state of an attached bench instrument connected via serial/USB)... before I post test code are there any gotchas of which I should be aware?

Lazarus 2.2.4 on x86_64 Debian 10 KDE using either GTK2 or Qt widget sets.

MarkMLl
« Last Edit: October 13, 2022, 11:28:52 am by MarkMLl »
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

wp

  • Hero Member
  • *****
  • Posts: 13424
Re: Radiobuttons in a groupbox
« Reply #1 on: October 12, 2022, 09:28:56 pm »
I was expecting that they would be independent and need event handlers to plumb them together, and slightly surprised to find that the LCL associated them automatically.
No. When you check one radiobutton all other radiobuttons in that same container (groupbox, panel) are automatically unchecked. Note the emphasis is on "the same container". Radiobuttons in another container are not affected.

However I'm having problems setting their initial state under software control (to reflect the state of an attached bench instrument connected via serial/USB)
Maybe you still have event handlers attached to the radiobuttons to interlock them? Simply set the Checked of the one radiobutton to true that you want to be checked; no special event handlers needed.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Radiobuttons in a groupbox
« Reply #2 on: October 12, 2022, 09:34:39 pm »
I think TRadioGroup introduces an invisible extra radiobutton to facilitate initialisation. You could try doing that if you have problems and want all your visible radiobuttons to start out unchecked.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8551
Re: Radiobuttons in a groupbox
« Reply #3 on: October 12, 2022, 09:56:37 pm »
Maybe you still have event handlers attached to the radiobuttons to interlock them? Simply set the Checked of the one radiobutton to true that you want to be checked; no special event handlers needed.

Definitely not. It's a clean project I was "building up gradually" with an initial focus on Linux hotplug events etc. so I've only just started to add real functionality.

It's really very odd: I still don't feel that I've characterised the symptoms, but I've got "English" and "中文" buttons which I'm trying to set based on what comes from the instrument. If the program starts with the instrument set to Chinese the correct radiobutton is checked, but once the instrument has reported that it's set to English only the "English" radiobutton is checked... despite the fact that I can see the correct data arriving.

As a working hypothesis, there might be a problem caused by having a checkbox in the same group, i.e. something that's explicitly got a "Checked" property.

I'll try to do a test app in the morning, but I might need to find some way of simulating the serial data using e.g. a file.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

MarkMLl

  • Hero Member
  • *****
  • Posts: 8551
Re: Radiobuttons in a groupbox
« Reply #4 on: October 12, 2022, 09:59:15 pm »
I think TRadioGroup introduces an invisible extra radiobutton to facilitate initialisation. You could try doing that if you have problems and want all your visible radiobuttons to start out unchecked.

I don't. The instrument (cheap signal generator) has a UI in either English or Chinese (specifically referred to as 中文 so my GUI has UTF-8 in it) and I want one or the other to show up.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

wp

  • Hero Member
  • *****
  • Posts: 13424
Re: Radiobuttons in a groupbox
« Reply #5 on: October 12, 2022, 10:51:52 pm »
Maybe you still have event handlers attached to the radiobuttons to interlock them? Simply set the Checked of the one radiobutton to true that you want to be checked; no special event handlers needed.

Definitely not. It's a clean project I was "building up gradually" with an initial focus on Linux hotplug events etc. so I've only just started to add real functionality.

It's really very odd: I still don't feel that I've characterised the symptoms, but I've got "English" and "中文" buttons which I'm trying to set based on what comes from the instrument. If the program starts with the instrument set to Chinese the correct radiobutton is checked, but once the instrument has reported that it's set to English only the "English" radiobutton is checked... despite the fact that I can see the correct data arriving.

As a working hypothesis, there might be a problem caused by having a checkbox in the same group, i.e. something that's explicitly got a "Checked" property.
I am attaching a small project with several radiobuttons and checkboxes inside the same groupbox. Checking them by clicking or by code (by means of the buttons on the form) behaves correctly.

jamie

  • Hero Member
  • *****
  • Posts: 7603
Re: Radiobuttons in a groupbox
« Reply #6 on: October 12, 2022, 11:21:04 pm »
Maybe the problem is the events can't decide how they got there?

This is why a lot of these controls needs a MODIFIED property to indicate if software made changes of the user actually clicked it.

  MODIFIED would be set to true for user actions and false if software is defining it.

  With this tool in hand, you can detect at the events what happen and don't do something that will retrigger another event, falsely.

 Its just a hunch but this could be something totally different like a bug in the widget that I don't see otherwise in the Windows widget.
The only true wisdom is knowing you know nothing

VTwin

  • Hero Member
  • *****
  • Posts: 1227
  • Former Turbo Pascal 3 user
Re: Radiobuttons in a groupbox
« Reply #7 on: October 13, 2022, 01:55:27 am »
I am not sure, but this may follow jamie's comment.

Setting a control in code may (is supposed to I believe) trigger an event one would normally associate with a user click. This makes no sense to me, but seems to be the default behavior.
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 15.3.2: Lazarus 3.8 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 3.8 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 3.8 (64 bit on VBox)

MarkMLl

  • Hero Member
  • *****
  • Posts: 8551
Re: Radiobuttons in a groupbox
« Reply #8 on: October 13, 2022, 08:48:08 am »
In any event: I'm on it and will report back.

There's a few obfuscatory tchotchkes relating to the groupbox being non-visible while the instrument is offline etc. At least there's no threads in this one.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

MarkMLl

  • Hero Member
  • *****
  • Posts: 8551
Re: Radiobuttons in a groupbox
« Reply #9 on: October 13, 2022, 10:15:47 am »
OK, could somebody who's familiar with this stuff take a look at the attached please. I've stripped the program I was working on down (i.e. preserving as much of the GUI layout as possible), there's no threads, no timers and few if any event handlers.

Click repeatedly on the Connect button, note the alternating 0/1 on the status line, note the lack of alternation in the radiobuttons.

Lazarus 2.2.4 FPC 3.2.2 GTK2 (also apparent on the full program using Qt) Debian 10 with KDE desktop, also apparently on Debian 11.

I'm still sure it's really me doing something silly...

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

dseligo

  • Hero Member
  • *****
  • Posts: 1674
Re: Radiobuttons in a groupbox
« Reply #10 on: October 13, 2022, 10:32:36 am »
You have error in your code.

Change like this:
Code: Pascal  [Select][+][-]
  1.         if scratch = '0' then
  2.           RadioButtonEnglish.Checked := true
  3.         else
  4.           RadioButtonChinese.Checked := true; // it was just: RadioButtonChinese.Checked;

MarkMLl

  • Hero Member
  • *****
  • Posts: 8551
Re: Radiobuttons in a groupbox
« Reply #11 on: October 13, 2022, 10:48:52 am »
Yes. Damn.

Damn. Damn. Damn. Damn. Damn. Damn. Damn. Damn. Damn. Damn.

Better now. Thanks for that ;-)

That's the result of going round in circles, initially trying to explicitly set one on and the other off etc. for an extended session.

I /thought/ that originally there was some sort of grouping that had to be set up where multiple radiobuttons weren't actually in a radiobox, and I think that's where I went wrong initially.

I'll re-enable some of the other bits that I've commented out, then mark this solved if everything looks OK. My apologies to everybody for causing noise.

MarkMLl


MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018