Example 12 : Using radio button inplace editorIn this demo, the basics steps involved to work with radiobutton inplace editors are shown. Radiobutton inplace editors provide a flexible alternative to combolist inplace editors, showing the user immediately the selected item from the available options. Two types of inplace radiobutton editors are possible: the normal radiobutton inplace editor and the cell data bound inplace editor. The difference is simple. In the first case, the radiobutton has no effect on the cell data and its selection is independent of the cell content.
The methods to add, remove and check radio button inplace editors are:
procedure AddRadio(aCol,aRow,DirRadio,IdxRadio:integer;sl:tstrings); procedure RemoveRadio(aCol,aRow:integer); function IsRadio(Acol,Arow:integer):boolean; function GetRadioIdx(Acol,Arow:integer;var IdxRadio:integer):boolean; function SetRadioIdx(Acol,Arow,IdxRadio:integer):boolean;
The first method gets you started. It adds a radiobutton control to cell aCol,aRow. The direction can be either horizontal (DirRadio=1) or vertical (DirRadio=0). The zero based index of the selected item can be initialized through the IdxRadio parameter. Setting this parameter to -1 creates a cell data dependent radiobutton control, that is : the index of the radiobutton group control depends on the cell content and the cell content changes to the item selected. The last parameter is a stringlist, holding the text values of each radiobutton in the radiobutton group. In the example application, the radiobutton inplace editors are setup in the FormCreate event handler:
procedure TForm1.FormCreate(Sender: TObject); var i:integer; begin radopt1:=tstringlist.Create; radopt1.Add('Delphi'); radopt1.Add('C++Builder'); radopt1.Add('JBuilder'); radopt2:=tstringlist.Create; radopt2.Add('Std'); radopt2.Add('Prof'); radopt2.Add('C/S'); with advstringgrid1 do begin for i:=1 to rowcount-1 do begin addradio(1,i,0,-1,radopt1); addradio(2,i,1,-1,radopt2); end; end; end;
Two stringlists are created to hold the data of two radiobutton groups. Then radiobutton inplade editors are added to the grid in columns 1 and 2, one in vertical direction, the other in horizontal direction. The third parameter is -1, indicating this is a cell data bound radiobutton inplace editor. Finally, the last parameter sets the nr. of radiobuttons and its text.
To show the effect of the cell data bound radiobutton control, a secondary grid is placed on the form and the cell data is transferred to the secondary grid control whenever the radiobutton is clicked. This is done in the event handler OnRadioClick:
procedure TForm1.AdvStringGrid1RadioClick(Sender: TObject; aCol, aRow, aIdx: Integer); var ms:tmemorystream; begin ms:=tmemorystream.Create; advstringgrid1.SaveToStream(ms); advstringgrid2.LoadFromStream(ms); ms.Free; end;
In this event handler, a memorystream is used to transfer in an easy and fast way the content of the first TAdvStringGrid to the second TAdvStringGrid, using SaveToStream for the first control and LoadFromStream for the second control. This shows that whenever the cell data bound radiobutton is clicked, the cell content updates immediately (the cell content itself is visible in the second grid) The explore the effect in the other direction (radiobutton update when cell content changes) a combolist editor is used in the second control to set the cell content for columns 1 and 2. Whenever the cell content is updated in the second grid, these changes are transferred (again with a memorystream) to the first grid control.
First of all, the OnGetEditorType event handler for the second grid, shows another interesting aspect of using the combobox property. Through this property, the combobox values for columns 1 and 2 are set to the stringlists holding the radiobutton captions as well, in the following way:
procedure TForm1.AdvStringGrid2GetEditorType(Sender: TObject; aCol, aRow: Integer; var aEditor: TEditorType); begin if acol in [1,2] then aEditor:=edComboList; if acol=1 then advstringgrid2.combobox.Items.Assign(radopt1); if acol=2 then advstringgrid2.combobox.Items.Assign(radopt2); end;
To visualize the changes in the second grid in the first grid, the OnCellValidate event is chosen to transfer the data with a memorystream from grid 2 to grid 1, as this event is called when the cells are updated.
procedure TForm1.AdvStringGrid2CellValidate(Sender: TObject; Col, Row: Integer; var Value: String; var Valid: Boolean); var ms:tmemorystream; begin ms:=tmemorystream.Create; advstringgrid2.savetostream(ms); advstringgrid1.loadfromstream(ms); ms.Free; end;
Delphi project & source files for downloading included in the main demos distribution for Delphi.