Firemokey ComboBox PosSource - delphi

In Firemonkey by using ComboBoxes I set the displayed item with the following row:
ComboBoxSomething.ItemIndex := ComboBoxSomething.Items.IndexOf(VarToStr(Query.FieldValues['fieldname']));
There is a BindListLink PosSource binding where the controls ItemIndex+1 binded to the Bindsource's Recno.
However it works only when I change the Combobox selected text by cliking on it, and does nothing when I set the ItemIndex with the code above.
Do you have any suggestion, what to do different?
Thank you in advance!
Endre

Ok, I've got it. At the same time I have to set the RecNo of the query as well (Query.Recno := ComboBox.ItemIndex+1;).
But I still don't know, why it isn't set automatically by the livebinding.

Related

Uncheck all checkboxes in a TCheckListBox

I want the user to be able to press a button to reset a TCheckListBox to it's initial state (having no boxes checked).
See images below of an example of what I want to happen after clicking the button:
Changed to:
How would I go about doing this? I know there would probably be a loop involved, but I'm unsure where to start. Thanks for the help in advance.
There is nice method - look at official help.
If we open TCheckListBox help page, choose Methods and filter off "inherited" ones, we'll see CheckAll method
CheckListBox1.CheckAll(cbUnchecked);
For individual checkboxes (missed this is a TCheckListBox).
Something along these lines,
Let's assume the checkboxes are on a panel called panel1.
var n: Integer;
begin
for n := 0 to panel1.ComponentCount - 1 do
if panel1.Components[n] is TCheckbox then
Tcheckbox(panel1.components[n]).checked := False;
end;
Note: if there is an event associated with the checkbox, you need to set the event to nil before modifying it, and returning the event after - else, the event will trigger as if you clicked the box.

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.

Enabling editing for TStringGrid

I am making a simple database application by following this video. My problem occurs when I use a TStringGrid instead of a TGrid as stated the video because I don't have it. I have a Navigator and all of my data loaded into the TStringGrid, but I am unable to edit it at all. So far I've tried double clicking an entry, pressing F2 and clicking on the Edit button on the Navigator, but nothing is working.
Is there anything I need to alter in the properties of the TStringGrid to allow editing or is the purpose of it only to display data?
Thank you in advanced!
You need to add goEditing to the Options property. Include it in the Object Inspector, or in code:
StringGrid1.Options := StringGrid1.Options + [goEditing];

Using commands concerning DBGrid from the main form in another form

I'm making a simple program for use in automotive parts shop. Here's how it's supposed to look: Link
The problem is the small window on the left. It should be opened when double clicked on any of the rows in DBGrid in the main window, and it should show all of the selected item's characteristics in DBEdit fields. If the Save button is clicked it should save the changes from the DBEdit fields into the database, but otherwise it should just ignore the changes.
I succeeded opening another form by double click on a field in DBGrid by using this code:
procedure TForm1.DBGrid1DblClick(Sender: TObject);
begin
if not Assigned(Form2)
then Form2 := TForm2.Create(Application);
Form2.Show;
end;
The only problem now is how to get the program to detect which row in the DBGrid is selected and then show its contents in DBEdit fields in the smaller window.
Can anyone tell me how to do this, please?
Thanks!
Just add the TDBEdits to TForm2, and connect them to the same DataSource as the DBGrid is using. They will automatically show the contents of the same row that is selected in the DBGrid, and you can edit or insert into the DataSource's DataSet and have the new or changed data appear in the DBGrid automatically.
There are many ways to achieve this. I'll describe two:
If you link the DBEdits in the little window to the same DataSource as the DBGrid's, then you are all set
OR
You can pass whatever info you want inside DBGrid1DblClick from Form1 to Form2. This option, by itself, has many possibilities.
Update
For the DataSource to be visible in Form2, unit to make Form2's unit use Form1's unit.

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