FireMonkey ComboBox assigning Selected. Text from query - delphi

I need to allow my users to view a previously selected item from a ComboBox. A query provides the previous choice to the ComboBox.
I was hoping Live Bindings would get me through it, but it is so slow, I can't use it.
I am able to complete a similar action using a ComboEditBox, but can't seem to sort out what needs to be done with the ComboBox.
For the ComboEditBox, the following code works fine:
ComboInspector1.Text := FDQueryGetInspectionInspector_1.Text;
However, the following code does not work for the ComboBox:
ComboStationLocated.Selected.Text := FDQueryGetInspectionStation_Found.Text;
Any Ideas?

I believe you are trying to select an item based on some text, so below will select an item in the combobox if it already exists from the text of YourObject (whatever this may be)
ComboStationLocated.ItemIndex:=ComboStationLocated.Items.IndexOf(YourObject.Text);
If this is not what your are trying to do I will edit my answer.

Related

Editing single item in useFieldArray

I'm using and loving react-hook-form. I've run across an issue where I have a list of items (let's say in the left column), then when you click on any item it will display an edit component for the selected item in the right column. The issue is when I switch between items, nested array values don't seem to update the data correctly.
I think I understand that because a new edit component isn't created for each parent item (I'm just passing the selected index down) the nested useFieldArray in the edit component still references the first parent item. If that's the case, I'm not exactly sure how to make sure a new useFieldArray is created for each item that gets selected.
Here's a codesandbox replicating the issue:
https://codesandbox.io/s/dreamy-brattain-r74lx
Any help is much appreciated.
I think you can just map over the childItems prop of your watched item inside your <Details /> component instead of using fields. As you are using watch the <Details /> will be re-rendered after appending a new child item.
If your <Details /> component should get more complex i would also suggest to use useWatch here instead of passing watch. Check this quote from the documentation for useWatch:
Behaves similarly to the watch API, however, this will isolate
re-rendering at the component level and potentially result in better
performance for your application.
Here is an example using useWatch:

Delphi TDBGrid edit inplace with drop down list

I have to do maintenance to an old Delphi v7 application which uses a TDBGrid allowing inplace editting with drop down lists.
The issue is: the grid will show only the values present at data source. Without editting, this would be no problem at all. But if I change some value, the grid will only show the newly set value after updating the underlying register (until that the "old" value is displayed).
This only applies to drop down lists. Other input types (checkboxes, text edits) work without problems.
The edit itself also works fine: the changed values are actually reflected in the datasource when the register is UPDATE'd. My only problem is displaying this not-yet-UPDATE'd value.
I have no idea how to even debug this, or further inspect this behavior. So even if you don't have a complete answer, please indicate what else can I do to find a solution.
UPDATE
The project originally used TDB3DGrid (whose source I'm not aware of), but to simplify things I've changed it to TDBGrid. To no avail. The only change was that true/false columns are now displayed as text, and edited as text boxes (displaying and accepting "True" and "False" strings); with TDB3DGrid it showed checkboxes.
Other columns (not edited trough drop down list) are fine: after editing the new value is displayed correctly even before updating the underlying register.
I'm still tracking down where does the list of alternatives that fill the drop down list comes from.
Also still trying to come with an MCVE.
UPDATE 2
Apparently the (now) TDBGrid's affected TDBGridColumn has an empty PickList property. Sure for designtime. But I think this is not changed in runtime.
TDBGrid's source is a child of a TQuery, whose affected field has FieldKind equal to fkLookup. Other properties set for the field are FieldName, KeyFields, LookupDataSet, LookupKeyFields and LookupResultField.

Delphi Firemonkey ComboBox insert in AdoTable

So, I've been using LiveBindings between two tables at a combobox to get the Id and Description of a foreign key(with the Item.LookupData and Item.text properties) and assign the key to a field in another table with the SelectedValue propertie. This is working fine i guess but i'm using custom dbnavigator controls to make a "register form".
I'm using methods like this to make the inserts: adotablealuno.FieldValues['Nome']:=editnomeAluno.Text;
But I'cant find how to use the combobox in this way, i've already tried the ItemIndex and Selected properties, but none of this work (I'm using a Access DB btw). How can I use my foreign key in ComboBox for this?
Actually I was already stating this question and there was no good answer for a long time. I found a way out that I am currently using. At least I know that I'll get reliable data.
You need to handle OnFillingListItem event of the LinkFillControlToField link in alike way and store id number in ComboBox Items. I use Tag property for this purpose though it is not actually good.
procedure TForm1.LinkFillControlToField1FillingListItem(Sender: TObject;
const AEditor: IBindListEditorItem);
begin
(AEditor.CurrentObject as TListBoxItem).Tag :=
YourLookuptable.FieldByName('id').AsInteger;
end;
And then fetch the item id from ListBox1.Selected.Tag. The text value can be accessed via ComboBox1.Selected.Text.
Appended.
You make an alike LinkFillControlToField link.
Then you select this link and create an OnFillingListItem event handler to the link (select events tab in the Object Inspector and double click on OnFillingListItem ComboBox). The event handler (empty procedure) will appear. It will be given a name like TForm1.LinkFillControlToField1FillingListItem(... Then you write the code setting the id property to the items' Tags.

Delphi TComboBox Dropdown fields filtering [duplicate]

Everyone probably knows what I mean, but to clarify the control would need to:
Fire an event when user edits the text. The event would provide a SuggestionList: TStrings which you could fill with matches/suggestions.
if the SuggestionList is not empty a drop down should appear.
Unlike combo, the control should not attempt to automatically select/auto complete or otherwise affect the editing.
So, is there a Delphi edit/combo control that works like that ?
Use the autocompletion feature built in to all Windows edit controls.
First, fill your TStrings object however you want. Then use GetOleStrings to create a TStringsAdapter to wrap it. (The adapter does not claim ownership of the TStrings object, so you must make sure you don't destroy it while the adapter is still live.) The adapter gives you an IStrings interface, which you'll need because the autocompletion feature requires an IEnumString interface to provide the completion matches. Call _NewEnum for that.
Next, call CoCreateInstance to create an IAutoComplete object. Call its Init method to associate it with the window handle of your edit control. If you're using a combo box, then send it a cbem_GetEditControl message to find the underlying edit window.
You can stop at that point and autocompletion should work automatically. You can disable autocompletion if you want, or you can set any number of autocompletion options.
You say you don't want autocompletion, but in the OS terminology, I think what you really don't want is called auto append, where the remainder of the string is entered into the edit box automatically as the user types, but selected so that further typing will overwrite it, and the user needs to delete the excess text if the desired value is shorter than one of the matches.
There is also auto suggest, which displays a drop-down list of suggestions.
You can enable either or both options. You don't need to filter the list of suggestions yourself; the autocomplete object filters the IEnumString list by itself.
You can use standard TComboBox and faststrings library (for stringMatches() function).
procedure TForm1.cbChange(Sender: TObject);
var
s:Integer;
tmpstr:string;
begin
//suggestions: tstringlist
cb.AutoComplete:=false;
tmpstr:=cb.Text;
cb.Items.Clear;
for s:=0 to suggestions.Count - 1 do
if StringMatches(suggestions[s],cb.Text+'*') then
cb.Items.Add(suggestions[s]);
cb.DroppedDown:=(cb.Items.Count<>0) and (Length(cb.Text)<>0);
cb.Text:=tmpstr;
cb.SelStart:=Length(cb.Text)
end;
If you just want to show a file or url list:
SHAutoComplete(GetWindow(eb_MyComboBox->Handle, GW_CHILD), SHACF_AUTOSUGGEST_FORCE_ON | SHACF_FILESYS_DIRS);
I first implemented this feature like Rob described it in his answer. Later I saw that TComboBoxEx has the property AutoCompleteOptions where I set acoAutoSuggest to True and acoAutoAppend to False. The ComboBox now filters its item list when doing some entry and shows the matching items.
I'm using RAD Studio 10 Seattle and XE2 but don't know if this feature is available in older versions.
To the last bit of your question: "So, is there a Delphi edit/combo control that works like that ?":
A bit late to the party but yes, I have written a free and open source component that implements the Google Place Autocomplete and Google Place Details API's:
It does inherit from the standard TComboBox but you can modify the code to work with any TEdit
https://carbonsoft.co.za/components/
or
https://github.com/RynoCoetzee/TRCGPlaceAutoCompleteCombo

Cloning a TStringGrid Component

I am starting to learn Delphi. So I decided to write an application like MS Excel from scratch. In a new Form1, I did put a TPageControl component containing only 1 page. In that page, I did put a TAdvStringGrid and a TPanel with some buttons (button1, button2) and a Popup1 menu for defining some actions on the grid, like copy cell, copy row, delete row, etc. For that StringGrid, also, I have defined some properties, like colors, fonts, etc. I added a toolbar with a button to the main form, in order to add more pages to the PageControl. The OnClick method of that button defines two actions:
1) to add a new Page2 to the PageControl, and
2) to add a new StringGrid In the new created Page2 .
That new (runtime defined) StringGrid created in a new Page of a Tpagecontrol should inherite (get, copy, clone, duplicate) the properties and methods of the StringGrid parent already defined in the first page at design time, and should be able to call the PopUp1 menu just like the StringGrid parent. How we do this?
At the beginning, I thought I just should copy the StringGrid properties using assing(), but when using this approach, the popup menu does not pop up when right mouse clicking on the new StringGrid... and the buttons (button1 and button2) of Form1 only work with the first StringGrid but not with the new added one. I did read somewhere that in order to solve this problem, I could duplicate the StringGrid component by using the write and read TMemoryStream (save the parent stringgrid into a memorystream, create a new stringgrid and then read that memorystream into the new created stringgrid), so I did, but when the program executes this component cloning method, I get a runtime error. :-(
I did carefully chech the Help. Nothing found on that topic. Seems there is not any Delphi component library or third party components that cope with this kind of task. Can anybody help, please? :o)
I would use an tabcontrol instead of an pagecontrol. That way, you would end up with multiple tabs but only one page and grid. I would then make some kind of data structure to keep all my cell information in, and render this structure to the grid. This way, I can have multiple structures, and let the active tab decide which structure to render. You will also end up with a looser coupling between your gui and your logic, making it easier change things later. E.g. if you need to bring in some values form a different spreadsheet into a cell in the current spreadsheet, you can load up a structure and pick out the wanted values. No need to make any gui for the second spreadsheet at all.
For a 3.party component, I will recommend TMS FlexCell and TAdvSpreadGrid. That will give you most of what you need.
A tricky choice for a learner :) however you do not need to start streaming things.
Look up the assign() procedure for TPersistent this is the routine you need to copy parts of the grid easily. For example
for i := 0 to StringGrid1.RowCount - 1 do
StringGrid2.Rows[i].Assign(StringGrid1.Rows[i]);
for an easy start differentiate your grids with the Tag property(StringGrid1.Tag := 1, StringGrid2.Tag := 2 Etc.
The popup menu is pretty simple too:
StringGrid2.popupmenu := stringGrid1.popupMenu But then then you must decide in the Popup Routine Which Grid is "Active" some thing l like
Tform1.popupMenuItem1Click(Sender: TObject)
if Sender is TStringGrid then
Case TStrigngGrid(Sender).Tag of
1: // Grid 1
2: // Grid 2
You can use the same simple logic with the buttons.
As neftali mentioned the best thing would be to put the created grids in an ObjectList.
You would then end up with the slightly more complex but expandabe:
Tform1.popupMenuItem1Click(Sender: TObject)
var AGrid: TStringGrid;
if Sender is TStringGrid then
AGrid := MyListOfStringGrids[MyListOfStringGrids.IndexOf(Sender)];
DoMenuItem1Stuff(AGrid);
Have fun
"...and the buttons (button1 and button2) of Form1 only work with the first StringGrid but not with the new added one. I did read somewhere that in order to solve this problem..."
There is no generic method for solving this. Delphi offers different tools to solve it.
You can create a list of Objects (TObjectList) that containts all the StringGrid; At Button1 Click event you must search what is the grid that you are using at this moment. For example (BIS for the other buttons):
var
index:integer;
sg:TStringGrid;
begin
...
// search the active page
index := pageControl.ActivePageIndex; //0, 1, 2,...
// USe this for search the StringGrid
sg := TStringGrid(OList.Objects[index]);
// the code that you have at the event bus woking with sg
// not stringgrid1, stringgrid2,...
...
sg.Color :=
...
If you don't want use ObjectList, there are alternatives. You can use Tag property for all StringGrids. Assign 0, 1, 2, 3,...
Extract the index (active page) and search the TStringGrid that have the property Tag with the same value. You can do this with FindComponent. The first methos is better. ;-)
Regards.
P.D: Excuse for my bad english.
Dear all, I am trying to learn Delphi
The Delphi style is to find/create/buy a component that does the job and use them in the design-time. You could try making a custom component based on a grid or use TFrame. See links from Custom Component Development and help files that comes with Delphi.
If you really need to clone the control dynamically, here's an example I found that uses stream.ReadComponent.

Resources