Recent

Author Topic: [solved] RadioGroup - get the name / string of the chosen item  (Read 480 times)

Nicole

  • Hero Member
  • *****
  • Posts: 509
[solved] RadioGroup - get the name / string of the chosen item
« on: September 19, 2022, 08:16:17 pm »
If we have a radiogroup1 with the items:
  • blue
  • green
  • red

the user chose "green".
How can I grab this "green" in my code?

myString:=radiogroup1.....................?
« Last Edit: September 20, 2022, 10:31:48 am by Nicole »

Arioch

  • Sr. Member
  • ****
  • Posts: 415
Re: RadioGroup - how to get the "itembyname"?
« Reply #1 on: September 19, 2022, 08:23:18 pm »
rg.itemindex is -1 if no utton is selected
if some was selected - then rg.itemindex would be 0,1,2,3, ..., Pred(Count)

rg.Items: TStrings is the lines that are captions of the buttons - and the added pointers/objects you can add

https://www.freepascal.org/docs-html/rtl/classes/tstrings.html

Code: Pascal  [Select][+][-]
  1. s: string; i: integer;
  2.  
  3. begin
  4.    s := '';
  5.    i := rg.ItemIndex;
  6.  
  7.    if (i >= 0) and (i < rg.Items.Count) then
  8.       s := rg.Items[i];
  9.  

Notice, than many LCL components dealing with multiple text lines expose TStrings interface as one or another property

usually it is xxxx.Items or xxxx.Lines

example: https://lazarus-ccr.sourceforge.io/docs/lcl/stdctrls/tcombobox.html
example: https://lazarus-ccr.sourceforge.io/docs/lcl/stdctrls/tmemo.html

But sometimes it is named differently like TStringGrid.Rows or TStringGrid.Columns
« Last Edit: September 19, 2022, 08:25:59 pm by Arioch »

Arioch

  • Sr. Member
  • ****
  • Posts: 415
Re: RadioGroup - how to get the "itembyname"?
« Reply #2 on: September 19, 2022, 08:35:43 pm »
However the better approach would be the opposite, you should not make your data go from visual controls, but you should make your visual controls follow data

Otherwise you would eventually become the hostage of the controls. Once ago there was great XMPP/Jabber client, JAJC. Then the author abandoned it. He was asked togive sources ti users, nand he said "i can not. There are commercial controls, and i modified them to store more data, and no i can not release the sources, because it is all half-commercial". A very propular program died.

so
Code: Pascal  [Select][+][-]
  1.  
  2. type TForm1 = class(TForm)
  3. ...
  4.   private
  5.      ColourNames: TArray<string>;
  6. ....
  7.  
  8. procedure TForm1.FormCreate(....);
  9. begin
  10.    ColourNames := ['red', 'green', 'blue'];
  11. // older Pascal:  ColourNames := TArray<string>.Create( 'red', 'green', 'blue');
  12. // yet older Pascal:  SetLength(ColourNames, 3); ColoutrNames[0] := 'red'; ColourNames[1] := ...
  13.  
  14.    with RadioGroups1.Items do begin
  15.        Clear;
  16.        AddStrings(ColourNames);
  17.       // for TList<string> type would be .AddRange(...) instead - but LCL was built before TList<T>, so Tstrings
  18.    end;
  19. end;
  20.  
  21. function TForm1.GetSelectedColourName:string; var i: integer;
  22. begin
  23.     i := RadioGroup1.ItemIndex;
  24.    Result := '';
  25.    if i < Low(ColourNames) then exit;
  26.    if i > High(ColourNames) then exit;
  27.    Result := ColourNames[i];
  28. end;
  29.  

wp

  • Hero Member
  • *****
  • Posts: 10298
Re: RadioGroup - how to get the "itembyname"?
« Reply #3 on: September 19, 2022, 10:58:45 pm »
If we have a radiogroup1 with the items:
  • blue
  • green
  • red
the user chose "green".
How can I grab this "green" in my code?
TRadioGroup.Items is a TStrings class (like TStringList), and has an IndexOf() method. Therefore:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   Radiogroup1.ItemIndex := RadioGroup1.Items.IndexOf('red');
  4. end

Nicole

  • Hero Member
  • *****
  • Posts: 509
[solved] RadioGroup - how to get the "itembyname"?
« Reply #4 on: September 20, 2022, 10:29:28 am »
Thank you for the answers.
Before I posted here, I checked "Google" (I never check Google, I check "startpage.com" instead, which has more privacy and better results because the paid ranks are out).

There are a lot of solutions in the net which had all in common, that they did not work for me.

Your answers (talking about strings and lists) let me try something special: I took the checkbox syntax for it. - Just a try. - The compiler ate it! With this, I have this solution, which works in my case:

Code: Pascal  [Select][+][-]
  1. nyString:= RadioGroup1.Items.strings[RadioGroup1.ItemIndex];

KodeZwerg

  • Hero Member
  • *****
  • Posts: 574
  • Fifty shades of code.
    • Delphi & FreePascal
Re: [solved] RadioGroup - how to get the "itembyname"?
« Reply #5 on: September 20, 2022, 11:42:33 am »
Code: Pascal  [Select][+][-]
  1. nyString:= RadioGroup1.Items.strings[RadioGroup1.ItemIndex];
Is actual the same like ->
Code: Pascal  [Select][+][-]
  1. s: string; i: integer;
  2.  
  3. begin
  4.    s := '';
  5.    i := rg.ItemIndex;
  6.  
  7.    if (i >= 0) and (i < rg.Items.Count) then
  8.       s := rg.Items[i];
Please add his checks for safety purposes aswell. (eg: nothing selected = your code blow up)
Code: Pascal  [Select][+][-]
  1. if ((RadioGroup1.ItemIndex > -1) and (RadioGroup1.ItemIndex < RadioGroup1.Items.Count)) then
  2.   myString := RadioGroup1.Items.strings[RadioGroup1.ItemIndex]
  3.   else
  4.   myString := 'out of bounds.';
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

paweld

  • Hero Member
  • *****
  • Posts: 526
Re: [solved] RadioGroup - get the name / string of the chosen item
« Reply #6 on: September 20, 2022, 12:35:00 pm »
@KodeZwerg: While checking whether ItemIndex is less than 0 is needed, because very often you can get an error here, checking whether ItemIndex >= Count is completely unnecessary, because ItemIndex cannot be greater than or equal to Count
Best regards / Pozdrawiam
paweld

KodeZwerg

  • Hero Member
  • *****
  • Posts: 574
  • Fifty shades of code.
    • Delphi & FreePascal
Re: [solved] RadioGroup - get the name / string of the chosen item
« Reply #7 on: September 20, 2022, 12:39:33 pm »
@KodeZwerg: While checking whether ItemIndex is less than 0 is needed, because very often you can get an error here, checking whether ItemIndex >= Count is completely unnecessary, because ItemIndex cannot be greater than or equal to Count
I do agree but upcoming code can be wrong, and this will catch developer made mistakes.
Code: Pascal  [Select][+][-]
  1. if index > -1 then
  2.   astring := items[1234]; // here it can blow, out of range/bounds
« Last Edit: September 20, 2022, 12:42:57 pm by KodeZwerg »
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

Arioch

  • Sr. Member
  • ****
  • Posts: 415
Re: [solved] RadioGroup - get the name / string of the chosen item
« Reply #8 on: September 20, 2022, 11:34:39 pm »
ItemIndex >= Count is completely unnecessary, because ItemIndex cannot be greater than or equal to Count

today it will not, tomorrow some update comes and it becomes possible

in this place it can not - in others it can be (i remember implementing OnItemHint for RxLib's checklistbox,  ItemIndex param would exactly be Count if the mouse was below the last item)

you better train good habbits uniformly, than memorize hundred places which spcecific sets of checks is needed  in every of them

better safe than sorry

if to take issues i'd rather have one with index > -1 as opposed to index >= 0  :-D
« Last Edit: September 20, 2022, 11:36:51 pm by Arioch »

paweld

  • Hero Member
  • *****
  • Posts: 526
Re: [solved] RadioGroup - get the name / string of the chosen item
« Reply #9 on: September 21, 2022, 07:11:02 am »
@Arioch: Redundant unnecessary code is not a good habit. It only unnecessarily obscures the actual code.
Best regards / Pozdrawiam
paweld

 

TinyPortal © 2005-2018