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

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]);

Related

How can I add a caption/text to my csExDropDownList Combobox in Delphi?

I'm currently trying to create a combobox that has a "caption". By caption I mean the text that you see when you haven't clicked on it yet or if you are choosing an option.
Before I added the csExDropDownList it worked fine, but I wanted it to be ReadOnly. Now when I edit the Text property, it instantly gets deleted. I thought about using a TLabel in front of the combobox and making it dissapear the moment I chose a dropdown, but the TLabel is always in the background. I also tried with the TStaticText component, but that leaves a different colored background in front of the combobox which looks bad.
If I was unable to explain with words what I'm trying to edit/wanted to add a text to, this is what I mean:
I've found a workaround to my problem. I added a third dropdown with the index of 0. Now in properties I put the ItemIndex to 0 which means that it will be displayed similar to the Text property. When I interact with either QuickSort or InsertSort I delete Index 0.
My code looks like this:
procedure TSorterForm.AlgorithmCbxChange(Sender: TObject);
begin
if (AlgorithmCbx.Text <> 'Choose Algorithm...') and not IsAlgorithmSelected then begin
AlgorithmCbx.Items.Delete(0);
IsAlgorithmSelected:= true;
end;
end;
Obviously not perfect so it'd be great if you could tell me how to improve this.

Delphi(FMX) Livebindings with Multiple RadioButtons

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;

What control should I use to create this UI in Delphi Firemonkey

I am developing an application for mobile (android and ios) by Delphi xe5.
I am willing to create this UI:
I tried TListBox but image on left and right cant be set.
I tried TListView but same problem as TListBox
I tried TGrid with custom column, The problem of texts and images is solved but I can't create headers of each rows (it hasn't something like colspan)
What I need is to create a custom control and repeat it.
What is the best solution?
Any solution or guide line will be appreciated.
Solution
Thanks #Mike Sutton for answer, this is the result
The style here is so different from a standard TListBoxItem style that is probably makes sense to start from scratch, in which case the issues with accessing the default styles become immaterial.
Add a TStyleBook to your form.
Set the StyleBook property of the form to point to it.
Double click the icon to open the editor.
Drag a TLayout to the structure panel and drop it on the only item which will be there.
Set the StyleName property of the TLayout (e.g. ScoreListBoxItemStyle).
Drag/drop other components to build up the layout you want (remember TLayouts for 'hidden' positioning).
Set the StyleName property of any components you want reference from your code.
Subclass TLIstBoxItem to TScoreListBoxItem (if using the StyleName suggested above).
Add properties for your text, images etc.
In the setter methods for each of these, cache the data and call a method such as:
procedure SetFlag1;
var O: TFMXObject;
begin
O := FindStyleResource('flag1'); //StyleName of the item
if O is TImage then
TImage(O).Bitmap.Assign(FFlag1);
end;
Override the ApplyStyle method and call all of your methods that set the data in the style.
Now create your items in code:
Item := TScoreListBoxItem.Create(Self);
ListBox1.AddObject(Item);
Item.Flag1.LoadFromReource ...
...
Here's an idea that I don't have time to test:
Create a descendant of a TListBoxItem and in that add you two images as normal TImages. I'm pretty sure that a TListBoxItem can parent an object. You'll have to place the images on the listbox item where you want them. Then whenever you add an item to the listbox item just pass in your own descendant.
(If this doesn't work someone let me know and I'll delete this.)

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