How to fill ComboBox while you are typing in it? - delphi

I am trying to accomplish a search as you type in a TComboBox and add items automatically as I type.
I use Delphi 7 and MSSQL.
Lets say I have a long table with name lists in a table with one column named 'names' and I typed 'Jonathan'.
I want to get results into the TComboBox as I type one by one.
Thanks.

Try the following:
procedure TForm1.ComboBox1Change(Sender: TObject);
var
I: Integer;
begin
ComboBox1.Items.Clear;
ComboBox1.SelStart:= Length(ComboBox1.Text); //To put the cursor in the end
of the string typed in the ComboBox
if ComboBox1.Text = '' then
ADOTable1.Filtered:= False
else
begin
ADOTable1.Filter:= 'Names LIKE ' + QuotedStr(ComboBox1.Text + '*');
ADOTable1.Filtered:= True;
for I := 1 to ADOTable1.RecordCount do
begin
ADOTable1.RecNo:= I;
ComboBox1.Items.Add(ADOTable1.FieldByName('Names').Value);
end;
end;
end;

Related

How To Increment Numbers in Calculated field of TQuery?

I Have TQuery With Calculated Field N.
How To Increment Numbers in the example (N starts with 5):
I tried this but Nothing:
procedure TForm1.Query1CalcFields(DataSet: TDataSet);
var i:integer;
begin
i := strtoint(edit2.Text);
Query1['N'] := inttostr(i+1);
end;
result:
N
2
2
2
2
.
.
Note: Foxpro database ,i use BDE to connect with ,It does not have to be a calculated field ,i want the Incremented value to use it in print of quickreport like a single reference for each Page (not pagenumber).
I Found This Solution With The Help Of #kobik
In Printing Of TQRLabel I Add This Code And No Needed To The Calculated Field Or Other Varible:
procedure TForm1.QRLabel1Print(sender: TObject; var Value: string);
begin
value:=inttostr(Query1.RecNo+strtoint(edit2.Text)-1);
end;
The Tedit To Costume The Start Number At Runtime.
This is a simple way that I test it:
1- declare a global variable for saving auto number
2- Set it in FormShow to 5
3- In OnCalcFields assign global variable to the new field
4- increment global variable
Notes: Do not use TEdit or any thing for show the result of calculate field, because it will just show the first result. but all the result will save in table or query correctly.
Codes
Global Variable:
var
Form1: TForm1;
i : Integer;
Form Show:
procedure TForm1.FormShow(Sender: TObject);
begin
i := 5;
end;
Calc Filed:
procedure TForm1.adoqry1CalcFields(DataSet: TDataSet);
begin
adoqry1['n'] := i;
//OR adoqry1N.AsInteger := i;
//OR adoqry1.FieldByName('n').AsInteger := i;
i := i + 1;
end;
At the end, I test it with ADOQuery.

Is it possible to extract a string from a selected TMetropolisUIListBoxItem's Title, Subtitle or Description properties?

I'm creating an app that uses Metropolis UI Listbox Items and I want to transfer the Description parameter of a selected item to a Memo with a button click. In regular listbox items it's easy to extract the Text string, for example:
procedure TForm1.Button1Click(Sender: TObject);
begin
Memo1.Text := ListBox1.Selected.Text;
end;
This also works on the Text property of a Metropolis UI Listbox Item, as normal.
However, if I try the same thing with the Title, Subtitle or Description properties, it just doesn't work.
procedure TForm1.Button1Click(Sender: TObject);
begin
Memo1.Text := ListBox1.Selected.Description;
end;
Does anybody knows how can I use the strings on those properties in the way I intend?
If you cast the listbox item to the metropolis listbox item you can access the properties.
procedure TForm1.Button1Click(Sender: TObject);
var
lbItemSel: TListBoxItem;
begin
lbItemSel := ListBox1.Selected;
if (lbItemSel is TMetropolisUIListBoxItem) then
begin
Memo1.Text :=
TMetropolisUIListBoxItem(lbItemSel).Title + ' ' +
TMetropolisUIListBoxItem(lbItemSel).SubTitle + ' ' +
TMetropolisUIListBoxItem(lbItemSel).Description
;
end;
end;

Insert column into string grid, delphi

I have a string grid, from which i can delete columns. I defined a CustomStringGrid type that allows me to use DeleteColumn method.
This is how it looks:
TCustomStringGrid = class(TStringGrid)
[...]
With tCustomStringGrid(mygrid) do
DeleteColumn(col)
end;
IS there something similar to add a column? I've tried InsertColumn but it doesn't seem to exist. I want to add a column at a particular position. In fact, if a user deletes a column i have an undo button which i want to reinsert the deleted column (i'm keeping the data in an array so i can recreate the column but i don't know how to insert one in a particular position).
Thank you!
It's not built in but easy to emulate, with ColCount = ColCount + 1 and MoveColumn from a HackClass.
type
THackGrid=Class(Grids.TCustomGrid)
End;
Procedure InsertColumn(G:TStringGrid;Position:Integer);
begin
if Position<G.ColCount then
begin
G.ColCount := G.ColCount + 1;
THackGrid(g).MoveColumn(G.ColCount - 1,Position);
end;
end;
procedure TMyForm.Button1Click(Sender: TObject);
begin
InsertColumn(StringGrid1,1);
end;
THack grid is not working, maybe it is ok when both cols are visible, but that works always :
Procedure MoveColumn(G:TStringGrid;OldPosition : integer;NewPosition:Integer);
var
i : integer;
temp : string;
begin
for i := 0 to g.rowcount - 1 do
begin
temp := g.cells[OldPosition,i];
g.cells[OldPosition,i] := g.cells[NewPosition,i];
g.cells[NewPosition,i] := temp;
end;
end;

How to find which column has been dragged at TListView.OnColumnDragged?

OnColumnDragged event of TListView has a simple TNotifyEvent type, so there is no straight way to find which column has been actually dragged into the new position.
How can I find which column has been dragged?
With a help of interposed class you might catch the HDN_ENDDRAG notification code in the WM_NOTIFY message handler.
The HDN_ENDDRAG notification returns in the lParam parameter the NMHEADER structure containing information about the header item that was being dragged. Here is the code sample; you can follow the commented version of the post as well:
uses
ComCtrls, CommCtrl;
type
TListView = class(ComCtrls.TListView)
private
procedure WMNotify(var AMessage: TWMNotify); message WM_NOTIFY;
end;
implementation
{ TListView }
procedure TListView.WMNotify(var AMessage: TWMNotify);
var
HeaderHandle: HWND;
begin
inherited;
if (AMessage.NMHdr^.code = HDN_ENDDRAG) then
begin
HeaderHandle := ListView_GetHeader(Handle);
if (AMessage.NMHdr^.hWndFrom = HeaderHandle) then
ShowMessage(
'The header with index ' +
IntToStr(TWMNotifyHC(AMessage).HDNotify^.Item) + ' ' +
'has been dragged to the position with index ' +
IntToStr(TWMNotifyHC(AMessage).HDNotify^.PItem^.iOrder) + '. ' +
'Columns are not updated yet!');
end;
end;
You don't get any indication which column has been moved. What does happen is that the items in the list view's Columns list are re-arranged to match the new order of the columns in the list view. So long as you can identify each column uniquely, and not by using the column's position in the list, then you can infer the order of the columns.
One possible approach is to give each column a different Tag value. Then you can do something like this:
procedure TForm1.ListView1ColumnDragged(Sender: TObject);
var
i: Integer;
s: string;
begin
s := '';
for i := 0 to ListView1.Columns.Count-1 do begin
s := s + IntToStr(ListView1.Columns[i].Tag) + ' ';
end;
Caption := Trim(s);
end;
Naturally you'll want to do something more useful than this, but I trust that it gets the idea across.

How to convert an integer value to boolean in delphi

I have a database field value, which is an integer like 0 and 1. Is it possible to convert
this integer values to Boolean while loading the data in to a DB Grid. I'm expecting without condition checking, like direct typecasting.
Thanks
There is no way to convert Integer to Boolean.
you can implement a function like this
function IntToBool(const AnInt: Integer): Boolean;
begin
if AnInt = 0 then Result := False
else Result := True;
end;
I guess that you want to show a database field in DBGrid as a CheckBox. If so, read article by Zarko Gajic. It is about Boolean fields, but you can easily modify the code for your needs.
The most simple solution to your problem would probably be to use a boolean calcfield.
If you need to edit it from the DBGrid, it gets a little bit more tricky (But still possible).
If you want to show the words "True" and "False" in DBGrid, you should use OnGetText event of Field like this :
procedure TMyForm.MyDataSetFieldGetText(Sender: TField;
var Text: string; DisplayText: Boolean);
begin
case Sender.AsInteger of
0 : Text := 'False';
1 : Text := 'True';
else
Text := '-';
end;
end;
try this sample function here:
function IntToBooleanStr(AInteger: Integer): string;
begin
case AInteger of
0:begin
Result := 'False';
end;
1:begin
Result := 'True';
end
else
Result := 'False';
end;
end;
that's all and you can use it inside the combobox onChange Event for FILTERING Some Logical Data that has the boolean values inside.
like here:
procedure TFrm_Books.ComBox_AvailableFilterChange(Sender: TObject);
begin
Table_Book.Filtered := False;
Table_Book.FilterOptions := [foCaseInsensitive];
Table_Book.Filter := '';
Table_Book.Filter := 'Available = ' + IntToBooleanStr(ComBox_AvailableFilter.ItemIndex);
Table_Book.Filtered := True;
end;
and here is the DFM Code for this combobox:
object ComBox_AvailableFilter: TComboBox
Left = 336
Top = 120
Width = 193
Height = 21
ItemHeight = 13
Items.Strings = (
'Not Available'
'Available')
TabOrder = 0
end
i hope this function resolve your question Above.

Resources