Delphi(FMX) Livebindings with Multiple RadioButtons - delphi

I have a form with 2 RadioButtons(with same GroupName) and I need to save 'A'(if RadioButton1 is selected) or 'I'(if RadioButton2 is selected) in the field Status using LiveBindings.
One Component to One Field is easy, but in this case I have two components getting and setting values from one field.
I created a function that returns the radiobutton selecting through Groupname and fill the field manually, but I wanted something more automatic.
Thanks in advanced!

Here are the steps to accomplish this.
Create your two RadioButtons, call it RadioButton1 and RadioButton2.
Set their GroupName property to the same string for both radio buttons.
Right click your 1st radio button and select Bind Visually...
In the LiveBindings designer, right click your radio button and select Bindable Members, and then select the checkbox IsChecked followed by clicking the ok button.
Still within the Live Bindings designer, Now drag a link between the IsChecked property and the field you wish to bind to (note this can be a string field).
Repeat steps 4 and 5 for the other Radio Button.
Now you are almost down, but you need to convert the string to a boolean so that the IsChecked property will have a boolean value. To do this, select the binding link from the LiveBindings Designer for your radio button. Then in its CustomFormat property, assign the following string
IfThen(ToStr(%s)="Poor",True, False)
This will allow the radio button to be checked when the underlying database value is 'Poor'
Do the same for your other radio button, except use a different string
IfThen(ToStr(%s)="Excellent",True, False)
Now to give the radio buttons the ability to change the underlying database field, you will need to attach code to perform this. Let us use the radio button's OnClick event (attach to both radio buttons). This code assumes your underlying dataset is named FDCustomer, and your field is named Status. Note that the radio button is not checked yet at the time of the event, so we look for IsChecked to be false.
if Sender = RadioButton1 then
begin
if not TRadioButton(Sender).IsChecked then // checking
begin
fdcustomer.Edit;
fdcustomer.FieldByName('Status').AsString:= 'Poor';
end;
end
else if Sender = RadioButton2 then
begin
if not TRadioButton(Sender).IsChecked then
begin
fdcustomer.Edit;
fdcustomer.FieldByName('Status').AsString:= 'Excellent';
end;
end;

Related

cxgrid - set all checkboxes checked

I am using the cxGrid. I have a field (column) in my grid that is of boolean type (true/false) represented in the grid as a checkbox. How can I make all the checkboxes in the column checked (or unchecked) on button click ?
it looks like this :
Now I would like, on button click, to turn those 3 checkboxes checked BEFORE I save everything..
DATA on the left(USERS) comes from a table, the data on the right is from a query. The SAVE of everything goes to a separate LOG table.
When I hit 'Check all' button,the result :
I could run the update query : update MYFIELD set SELECTED = '2';
but I am more interested in manipulating the grid itself.Something simple...
You will have to add a button or pop up menu somewhere on your form to accept the check all 'command', or maybe even place a checkbox in your column header. Then go through your underlying dataset and set all field values. Don't forget a DisableControls/EnableControls.
added an extra field to my table (boolean type) and changed its property in the cxGrid to that of a checkbox.Then on button click:
with uniquery1 do begin
Active:=False;
sql.Clear;
SQL.Add('update users set selected = 0'); //or '1'
execSql;
end;
Uniquery1.Refresh;
this I found was the easiest way ....

Combo box - typing selection then clicking out of focus - doesn't select the typed item

I'm having an issue with the combo box. I have an event handler for OnClick which refreshes data based on what item was selected. The problem is when this scenario occurs:
Drop-down the combo box to list the various options
Type on the keyboard to find a matching item
Combo box changes this selection and calls the OnClick event
My screen refreshes due to this selection / event
Click somewhere outside of the combo box to take the focus away from it
Combo box goes back to the previous selection, even though OnClick was already called
Even though Combo box changed back to prior selection, OnClick isn't called again
After this, Combo Box shows different value than what my data actually represents
So when you open a combo box, and type a few letters on the keyboard to find the item in the drop-down list, OnClick is fired which refreshes my screen. But when you click somewhere outside the combo box (thus taking the focus away from it), the combo box changes back to whatever value was previously selected, instead of what I had typed. And at the same time, the OnClick event isn't fired, so the combo box shows the incorrect value compared to what I had loaded on the screen.
How do I make the combo box stay on the selected item in this scenario of typing the item on the keyboard?
In my code, I deal with this using the OnCloseUp event. Well, in fact I'm using a sub-classed combo for my drop-down lists and they override both the Change and CloseUp methods:
procedure TMyDropDownList.Change;
begin
RespondToChange;
inherited;
end;
procedure TMyDropDownList.CloseUp;
begin
RespondToChange;
inherited;
end;
The RespondToChange method reacts to the new ItemIndex value. If it is expensive to react to every single change whilst the combo is dropped down, then you might consider omitting the call to RespondToChange from the Change method.
You could use OnExit to make the entry with the keyboard jive with the Index on the ComboBox; where VarS is assigned OnChange and is the answer you would like to keep:
procedure TForm1.ComboBox1Exit(Sender: TObject);
begin
{ Windows keyboard select bug, force use of selected }
ComboBox1.ItemIndex := ComboBox1.Items.IndexOf(VarS);
end;
I would call this a bug in the ComboBox design.

How do i assign a RadioButton to a RadioGroup in XE2?

Don't hate (or devote) me on this silly question, but i noticed on XE2 it has changed, i try to drop a new RadioButton to a RadioGroup and i notice it is actually NOT a part of that group, why ?
and what is this TStrings i need to write ? it's kind of hard for me to control it like that.
You cannot manually add a TRadioButton to a TRadioGroup. The TRadioGroup control has always worked in this way. You must use its Items property to add the radio buttons.
The Embarcadero documentation says
To add radio buttons to a TRadioGroup, edit the Items property in the
Object Inspector. Each string in Items makes a radio button appear in
the group box with the string as its caption. The value of the
ItemIndex property determines which radio button is currently
selected.
So you can use the Object Inspector to edit the Items property or writing code like this:
RadioGroup1.Items.Add('Option 1');
RadioGroup1.Items.Add('Option 2');
RadioGroup1.Items.Add('Option 3');
RadioGroup1.Items.Add('Option 4');
RadioGroup1.Items.Add('Option 5');
Finally to check which radio button is selected use the ItemIndex property like so
if RadioGroup1.ItemIndex>=0 then
ShowMessage(RadioGroup1.Items[RadioGroup1.ItemIndex]);

delphi 2009 accelerators

How to remove the Accelerators from TMainMenuActionBar ?
can't seem to find the AutoHotKey = maManual property to change, nor to find any other property that will cause the right effect.
(Assuming the question is about TActionMainMenuBar) you would set the AutoHotKeys property through the ActionManager component that the action bar is linked to (through its ActionManager property). Unlike the TMainMenu's AutoHotKeys, this one is a boolean property.
To set the property at design time,
Select the 'ActionManager' component on the form
Click the ... button on the right side of the ActionBars property in OI.
Select your MainMenuBar from the popped up Editing ActionManager1.ActionBars' dialog.
Click the ... button on the right side of the Items property in OI, which will launch the Editing ActionManager1.Items dialog
Do not select any of the items at this time. Instead, set the AutoHotKeys property to True or False in OI.
At run time you can do:
ActionManager1.ActionBars[0].Items.AutoHotKeys := False;
Note that you might need to re-set the Caption of an Item after toggling AutoHotKeys. I.e. 'F&ormat' -> 'Format'.

Which radio button is selected in a TRadioGroup?

As you can see in my question history, I'm developing a eBook manager, that will be open-source and I will release it in about 10 days, but I have a TRadioGroup, as you can see:
TRadioGroup Used On My Form http://img85.imageshack.us/img85/1830/radiogroup.png
And I want to store somethings in a variable(that needs to be a Integer) that will be "linked" with this TRadioGroup.
I need to do a if function like this:
Caption Of The TRadioButton -> Number that will need to be stored in the variable
Fit 2xWidth - Default -> 0
Fit 2xHeight -> 1
Fit Width -> 2
Fit Height -> 3
But I just used a TRadioGroup and a TRadioButton one time, different than in C# that I've used more than 20 times. Then I want to know what I need to put on the if function, because what it will do I already know how to do:
var
num: Integer;
begin
if(TRadioButton1 checked?)
begin
num := 0;
end;
end.
What I need to put inside the brackets of the if function?
PS: I'm going to put the credits on the program for the people that helped me in this little project.
A TRadioButton has the Checked property. But A TRadioGroup has the ItemIndex property.
The items in a TRadioGroup are stored using a TStrings. So you can associate an object to each option and you can cast an integer to a TObject to be stored.
Example:
// fill the radiogroup
radiogroup.Items.AddObject('Fit 2xWidth', TObject(0));
radiogroup.Items.AddObject('Fit 2xHeight', TObject(1));
radiogroup.Items.AddObject('Fit Width', TObject(2));
radiogroup.Items.AddObject('Fit Height', TObject(3));
radiogroup.ItemIndex := 0;
To read the current setting:
value := radiogroup.ItemIndex;
Or to get the associated integer:
index := radiogroup.ItemIndex;
Assert(index>=0); // Sanity check
value := Integer(radiogroup.Items.Objects[index]);
In your case, the values are 0 to 3 so you can use the ItemIndex.
As a note, if is not a function. A function is a piece of code that returns a value based on the input parameters. If is a statement, which is a command that can be executed. The if statement is special because it enables you to execute a different statement based on the if condition.
Just a little TIP: Setting .ItemIndex does not send keyboard focus to the radio item, i know how to fix it, read ahead.
Instead of selecting by code a Radio in a RadioGroup by setting .ItemIndex it is much better to do it by sending focus to the radio item; just to be very clear: i mean sending focus just to the radio item, not the whole radio group.
Instead of: radiogroup.itemindex:=TheIndex;
Do it as this: TRadioButton(radiogroup.Controls[TheIndex]).SetFocus;
It will make the radio item to be selected and send the keyboard focus to it, so it will display the dotted rectangle arroud it, just as if the user had clicked on it.
Note1: To see it in action use keyboard cursor keys and compare behavor of just setting .ItemIndex and sending focus to radio item.
Note2: If you use TRadioButton(radiogroup.Controls[TheIndex]).SetFocus; then there is no no need to set .ItemIndex at all, it will also be done.
Hope that helps someone having the same problem as me, when need to set it by code, for example to avoid circular keyboard behavor, for example for making it to stay on last radio item when last radio item is selected and keyboard right cursor is pressed, same for first.

Resources