Forum > LCL

[solved] RadioGroup - get the name / string of the chosen item

(1/2) > >>

Nicole:
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.....................?

Arioch:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---s: string; i: integer; begin   s := '';   i := rg.ItemIndex;    if (i >= 0) and (i < rg.Items.Count) then      s := rg.Items[i]; 
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

Arioch:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- type TForm1 = class(TForm)...  private     ColourNames: TArray<string>;.... procedure TForm1.FormCreate(....);begin   ColourNames := ['red', 'green', 'blue'];// older Pascal:  ColourNames := TArray<string>.Create( 'red', 'green', 'blue');// yet older Pascal:  SetLength(ColourNames, 3); ColoutrNames[0] := 'red'; ColourNames[1] := ...    with RadioGroups1.Items do begin       Clear;       AddStrings(ColourNames);       // for TList<string> type would be .AddRange(...) instead - but LCL was built before TList<T>, so Tstrings   end;end; function TForm1.GetSelectedColourName:string; var i: integer;begin    i := RadioGroup1.ItemIndex;   Result := '';   if i < Low(ColourNames) then exit;   if i > High(ColourNames) then exit;   Result := ColourNames[i];end; 

wp:

--- Quote from: Nicole on September 19, 2022, 08:16:17 pm ---If we have a radiogroup1 with the items:

* blue
* green
* redthe user chose "green".
How can I grab this "green" in my code?

--- End quote ---
TRadioGroup.Items is a TStrings class (like TStringList), and has an IndexOf() method. Therefore:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.Button1Click(Sender: TObject);begin  Radiogroup1.ItemIndex := RadioGroup1.Items.IndexOf('red');end

Nicole:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---nyString:= RadioGroup1.Items.strings[RadioGroup1.ItemIndex];

Navigation

[0] Message Index

[#] Next page

Go to full version