Setfocus to the searchbox in a tlistview - delphi

I am working in Seattle, writing a FM application for windows only.
I have A tlistview on my form and have it populated with data.
I have the search option turned on.
How do I programmatically set focus to the search box?
How do I increase the size and font size of the search box?
thanks

The searchbox is not intended to be accessed programmatically except for setting it visible and to fire event when changed. Otherwise it is intended to be accessed only by the user.
Therefore, access is a little bit involved. However, the example of the OnSearchChange event inspired the following answer:
uses ..., FMX.SearchBox;
type
TForm17 = class(TForm)
ListView1: TListView;
Button1: TButton;
Label1: TLabel;
...
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
sb: TSearchBox; // a local reference
...
end;
implementation
procedure TForm17.Button1Click(Sender: TObject);
begin
if Assigned(sb) then
sb.SetFocus;
end;
procedure TForm17.FormCreate(Sender: TObject);
var
i: integer;
begin
ListView1.SearchVisible := True; // or set in the Object Inspector at design time
for i := 0 to ListView1.Controls.Count-1 do
if ListView1.Controls[I].ClassType = TSearchBox then
begin
sb := TSearchBox(ListView1.Controls[i]);
Break;
end;
end;
procedure TForm17.ListView1SearchChange(Sender: TObject);
begin
if Assigned(sb) then
Label1.Text := sb.Text;
end;
At form creation we search the SearchBox control and if found we store a reference to it in the sb: TSearchBox; field. Then access is quite straightforward.

Related

Disable scrolling possibility of TWebBrowser

Does anyone know, how to disable scrolling possibility of TWebBrowser control in a Firemonkey iOS / Android application? My goal is to get absolutely static element that will not react on touches and so on.
There is no single setting or action that disables all user actions of the Fmx.TWebBrowser. But there is a feature you can use for your purpose.
The feature I refer to, is Fmx.WebBrowser.TCustomWebBrowser.CaptureBitmap documented here
Description
Captures the currently visible Web page as a bitmap.
This method returns a TBitmap object, which allows you to create,
manipulate and store images in memory or as files on a disk. The
scenario of the use of this method could be as follows:
1. Call this method to capture a visible Web page as a bitmap.
2. Hide a TWebBrowser control.
3. Display the bitmap and overlay other components (such as buttons or popups) on top of the bitmap.
In your case you would just hide the WB and display the bitmap.
Tested with the following code:
type
TForm25 = class(TForm)
Button1: TButton;
WebBrowser1: TWebBrowser;
Edit1: TEdit;
Image1: TImage;
Timer1: TTimer;
procedure Button1Click(Sender: TObject);
procedure WebBrowser1DidFinishLoad(ASender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
bmp: TBitmap;
public
{ Public declarations }
end;
implementation
procedure TForm25.Button1Click(Sender: TObject);
begin
WebBrowser1.URL := Edit1.Text;
end;
procedure TForm25.Timer1Timer(Sender: TObject);
begin
bmp := WebBrowser1.CaptureBitmap;
Image1.Bitmap := bmp;
end;
procedure TForm25.WebBrowser1DidFinishLoad(ASender: TObject);
begin
Timer1.Enabled := True;
end;
With the WB hidden, it cannot be operated on.
The timer (1000 ms) I added because my attempts to capture the image already in the OnDidFinishLoad() event did nöt succeed.

How to create a combobox with two columns (one hidden) in Delphi 7?

How to create a TComboBox with two columns that has one of its columns hidden so that it can keep an id value along with the actual item in it? And then how to get to that id value programmatically?
There's no need for two columns here.
You can take advantage of the fact that TComboBox.Items (like many other things in Delphi, like TStringList, TMemo.Lines, and TListBox.Items) descends from TStrings, which has both the Strings and Objects properties. Objects stores anything the size of a TObject, which is a pointer.
This means you can store your integer value by simply typecasting it to a TObject when adding it, and typecasting it back to an Integer when retrieving it.
Something like this should work:
procedure TForm1.FormCreate(Snder: TObject);
var
i: Integer;
sItem: String;
begin
for i := 0 to 9 do
begin
sItem := Format('Item %d', [i]);
ComboBox1.Items.AddObject(sItem, TObject(i));
end;
end;
To retrieve the value:
procedure TForm1.ComboBox1Click(Sender: TObject);
var
Idx: Integer;
Value: Integer;
begin
Idx := ComboBox1.ItemIndex;
if Idx <> -1 then
begin
Value := Integer(ComboBox1.Items.Objects[Idx]);
// Do something with value you retrieved
end;
end;
Note that, since the Objects property is actually meant to store objects, this gives you a lot of flexibility. Here's an example (intentionally very trivial) of storing a customer's contact information in an associated object instance and displaying it in labels when an item from a listbox is selected.
unit Unit1;
interface
uses
Windows, Messages, Variants, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls;
type
TCustomer=class
private
FContact: string;
FPhone: string;
public
constructor CreateCustomer(const AContact, APhone: string);
property Contact: string read FContact write FContact;
property Phone: string read FPhone write FPhone;
end;
TForm1 = class(TForm)
ListBox1: TListBox;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
lblContact: TLabel;
lblPhone: TLabel;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure ListBox1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TCustomer }
constructor TCustomer.CreateCustomer(const AContact, APhone: string);
begin
inherited Create;
FContact := AContact;
FPhone := APhone;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
i: Integer;
begin
for i := 0 to ListBox1.Items.Count - 1 do
ListBox1.Items.Objects[i].Free;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
lblContact.Caption := '';
lblPhone.Caption := '';
// Create some customers. Of course in the real world you'd load this
// from some persistent source instead of hard-coding them here.
ListBox1.Items.AddObject('N Company', TCustomer.CreateCustomer('Nancy', '555-3333'));
ListBox1.Items.AddObject('B Company', TCustomer.CreateCustomer('Brad', '555-1212'));
ListBox1.Items.AddObject('A Company', TCustomer.CreateCustomer('Angie', '555-2345'));
end;
procedure TForm1.ListBox1Click(Sender: TObject);
var
Cust: TCustomer;
begin
if ListBox1.ItemIndex <> -1 then
begin
Cust := TCustomer(ListBox1.Items.Objects[ListBox1.ItemIndex]);
lblContact.Caption := Cust.Contact;
lblPhone.Caption := Cust.Phone;
end;
end;
end.
ComboBox controls do not support columns, and you do not need a hidden column anyway to accomplish what you need.
The TComboBox.Items property is a TStrings descendant. It can hold both string values and associated user-defined data values together at the same time, but the user will only see the string values in the UI. Use the Items.AddObject() method to add string+ID values to the list, and then use the Items.Objects[] property to retrieve the ID values when needed.
Alternatively, you could just store your ID values in a separate array that has the same number of elements as the ComboBox and then use the ComboBox item indexes to access the array values. This is especially important if you need to store a value of -1, because that particular value is not retrievable from the Objects[] property of a TComboBox due to the way the getter method is implemented, like Rob said.

Delphi 2010 - How to Copy and Clear the TShape

Ok, after working with TShape, I need to clean my "Shape1" from Lines and Text.
And also how to copy everything in "Shape1" into "Shape2" ?
Thanks B4 ^o^
type
TShape = class(ExtCtrls.TShape); //interposer class
TForm1 = class(TForm)
Shape1: TShape;
Shape2: TShape;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
public
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
// Draw some text on Shape1 := TShape
Shape1.Canvas.Font.Name :='Arial';// set the font
Shape1.Canvas.Font.Size :=20;//set the size of the font
Shape1.Canvas.Font.Color:=clBlue;//set the color of the text
Shape1.Canvas.TextOut(10,10,'1999');
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
// Copy everything from Shape1 to Shape2 (make a duplication)
// How to do it ?
showmessage('copy Shape1 into Shape2');
end;
End.
Following pseudo-code makes a copy of SourceShape canvas content to the TargetShape canvas, but only until the TargetShape is refreshed:
procedure TForm1.Button1Click(Sender: TObject);
begin
TargetShape.Canvas.CopyRect(Rect(0, 0, TargetShape.ClientWidth,
TargetShape.ClientHeight), SourceShape.Canvas, Rect(0, 0,
SourceShape.ClientWidth, SourceShape.ClientHeight));
end;
To clear the previously copied content, you can use the following:
procedure TForm1.Button2Click(Sender: TObject);
begin
TargetShape.Invalidate;
end;
To keep your drawing persistent you need to implement your own OnPaint event, in which whenever it fires, copy the current canvas content from the source to target using the CopyRect method like shown above.
But the question is then, why to use a TShape control at all. It would be better to use TPaintBox and draw your stuff by yourself including the shapes that are drawn by TShape control.

Frames and Browse History in Delphi

I am currently developing a delphi application that will need a browse history and am trying to work out how exactly to implement this.
The application has 2 modes. Browse and Details. Both designed as Frames.
After a search an appropriate number of Browse Frames are created in Panel 1 and populated.
From a Browse Frame we can either open the Detail Frame, replacing the contents of Panel 1 with the contents of the Detail Frame. Alternatively a new search can be spawned, replacing the current set of results with a new set.
From the Detail Frame we can either edit details, or spawn new searches. Certain searches are only available from the Detail Frame. Others from either the Browse Frames or the Detail Frame.
Each time a user displays the Detail Frame, or spawns a new search I want to record that action and be able to repeat it. Other actions like edits or "more details" won't be recorded. (Obviously if a user goes back a few steps then heads down a different search path this will start the history fresh from this point)
In my mind I want to record the procedure calls that were made in a list e.g.
SearchByName(Search.Text);
SearchByName(ArchName.Text);
DisplayDetails(JobID);
SearchByName(EngineerName.Text);
DisplayDetails(JobID);
Then I can just (somehow) call each item in order as I go bak and forward...
In response to Dan Kelly's request to store the function:
However what I still can't see is how I call the stored function -
What you are referring to is storing a method handler. The code below demonstrates this. But, as you indicated your self, you could do a big if..then or case statement.
This all will works. But an even more "eloquent" way of doing all this is to store object pointers. For example, if a search opens another search, you pass a pointer of the first to the 2nd. Then in the 2nd if you want to refer back to it, you have a pointer to it (first check that it is not nil/free). This is a much more object oriented approach and would lend itself better to situations where someone might close one of the frames out of sequence.
unit searchit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TSearchObject = class
FSearchValue: String;
FOnEventClick: TNotifyEvent;
constructor Create(mSearchValue: string; mOnEventClick: TNotifyEvent);
procedure FireItsEvent;
end;
type
TForm1 = class(TForm)
SearchByName: TButton;
GoBack: TButton;
DisplayDetails: TButton;
searchfield: TEdit;
jobid: TEdit;
procedure FormCreate(Sender: TObject);
procedure SearchByNameClick(Sender: TObject);
procedure GoBackClick(Sender: TObject);
procedure DisplayDetailsClick(Sender: TObject);
private
{ Private declarations }
SearchObjectsList: TStringList;
procedure DisplayDetailFunction(Sender: TObject);
procedure SearchByNameFunction(Sender: TObject);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
constructor TSearchObject.Create(mSearchValue: string;mOnEventClick: TNotifyEvent);
begin
FOnEventClick := mOnEventClick;
FSearchValue := mSearchValue;
end;
{$R *.dfm}
procedure TSearchObject.FireItsEvent;
begin
if Assigned(FOnEventClick) then
FOnEventClick(self);
end;
procedure TForm1.SearchByNameClick(Sender: TObject);
var
mSearchObject: TSearchObject;
begin
mSearchObject := TSearchObject.Create(SearchField.Text,SearchByNameFunction);
SearchObjectsList.AddObject(SearchField.Text,mSearchObject);
end;
procedure TForm1.DisplayDetailFunction(Sender: TObject);
var
mSearchObject: TSearchObject;
begin
mSearchObject := TSearchObject(Sender);
ShowMessage('This is the Display Detail Event. The value of the JobID is '+mSearchObject.FSearchValue);
end;
procedure TForm1.SearchByNameFunction(Sender: TObject);
var
mSearchObject: TSearchObject;
begin
mSearchObject := TSearchObject(Sender);
ShowMessage('This is the SearchByName Event. The value of the Search Field is '+mSearchObject.FSearchValue);
end;
procedure TForm1.DisplayDetailsClick(Sender: TObject);
var
mSearchObject: TSearchObject;
begin
mSearchObject := TSearchObject.Create(jobid.text,DisplayDetailFunction);
SearchObjectsList.AddObject(jobid.text,mSearchObject);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
SearchObjectsList := TStringList.Create;
end;
procedure TForm1.GoBackClick(Sender: TObject);
var
mSearchObject: TSearchObject;
begin
if SearchObjectsList.count=0 then
showmessage('Cannot go Back!')
else begin
mSearchObject := TSearchObject(SearchObjectsList.Objects[SearchObjectsList.count-1]);
mSearchObject.FireItsEvent;
SearchObjectsList.Delete(SearchObjectsList.count-1);
end;
end;
end.
Keep track of everything in a TStringList; when they go "Back" you delete from the string list. This is a sort of prototype:
type
TSearchObject = class
FSearchFunction,FSearchValue: String;
constructor Create(mSearchFunction,mSearchValue: string);
end;
type
TForm1 = class(TForm)
SearchByName: TButton;
GoBack: TButton;
DisplayDetails: TButton;
searchfield: TEdit;
procedure FormCreate(Sender: TObject);
procedure SearchByNameClick(Sender: TObject);
procedure GoBackClick(Sender: TObject);
procedure DisplayDetailsClick(Sender: TObject);
private
{ Private declarations }
SearchObjectsList: TStringList;
jobid: String; //not sure how you get this
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
constructor TSearchObject.Create(mSearchFunction,mSearchValue: string);
begin
FSearchFunction := mSearchFunction;
FSearchValue := mSearchValue;
end;
{$R *.dfm}
procedure TForm1.SearchByNameClick(Sender: TObject);
var
mSearchObject: TSearchObject;
begin
mSearchObject := TSearchObject.Create('SearchByName',SearchField.Text);
SearchObjectsList.AddObject(SearchField.Text,mSearchObject);
end;
procedure TForm1.DisplayDetailsClick(Sender: TObject);
var
mSearchObject: TSearchObject;
begin
mSearchObject := TSearchObject.Create('DisplayDetails',JobID);
SearchObjectsList.AddObject(JobId,mSearchObject);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
SearchObjectsList := TStringList.Create;
end;
procedure TForm1.GoBackClick(Sender: TObject);
var
mSearchObject: TSearchObject;
begin
if SearchObjectsList.count=0 then
showmessage('Cannot go Back!')
else begin
mSearchObject := TSearchObject(SearchObjectsList.Objects[SearchObjectsList.count-1]);
if mSearchObject.FSearchFunction ='SearchByName' then
ShowMessage('Value of Search Field:'+mSearchObject.FSearchValue)
else
ShowMessage('Value of JobID:'+mSearchObject.FSearchValue);
SearchObjectsList.Delete(SearchObjectsList.count-1);
end;
end;
Another option would be to use my wizard framework, which does this with TForms but can easily also be adjusted to use frames. The concept is that each summary form knows how to create its appropriate details. In your case the framework is more of an example of how to do it, rather than a plug and play solution.
Complementing MSchenkel answer.
To persist the list between program runs, use an ini file.
Here is the idea. You have to adapt it. Specially, you have to figure out the way to convert object to string and string to object, sketched here as ObjectToString(), StringToStringID and StringToObject().
At OnClose event, write the list out to the ini file.
const
IniFileName = 'MYPROG.INI';
MaxPersistedObjects = 10;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
ini: TIniFile;
i: integer;
cnt: integer;
begin
ini:=TIniFile.Create(iniFileName);
cnt:=SearchObjectsList.Count;
if cnt>MaxPersistedObjects then
cnt:=MaxPersistedObjects;
for i:=1 to MaxPersistedObjects do
if i>cnt then
ini.WriteString('SearchObjects','SearchObject'+intToStr(i),'');
else
ini.WriteString('SearchObjects','SearchObject'+intToStr(i),
ObjectToString(SearchObjectsList[i-1],SearchObjectsList.Objects[i-1]) );
ini.Free;
end;
and read it back at OnCreate event.
procedure TForm1.FormCreate(Sender: TObject);
var
ini: TIniFile;
i: integer;
begin
SearchObjectsList := TStringList.Create;
ini:=TIniFile.Create(IniFileName);
for i:=1 to MaxPersistedObjects do
begin
s:=ini.ReadString('SearchObjects','SearchObject'+intToStr(i),'');
if s<>'' then
SearchObjectsList.AddObject(StringToID(s),StringToObject(s));
end;
ini.Free;
end;

How to create an array of controls?

I have to create an array and place all controls there in order to access them.Here's a short example:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
const Test:Array[0..2] of TButton = (Button1,Button2,Button3);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
end.
Undeclarated idenitifier 'Button1' at the line where I declarated my array.But it's declarated three lines above.
Where's the problem,how to put all controls in an array?
EDIT:
Thank you for your answers,but I've got problems:
var TestA:TObjectList<TButton>;
var index:TComponent;
begin
TestA := TObjectList<TButton>.Create(false);
for index in Form7 do
if pos(index.name, 'Button') = 1 then
TestA.add(TButton(index));
TestA[0].Caption := 'Test'; //Exception out of range.
Ben's right. You can't set up a control array in the form designer. But if you have 110 images, for this specific case you can put them into a TImageList component and treat its collection of images as an array.
If you've got a bunch of more normal controls, like buttons, you'll have to create an array and load them into it in code. There are two ways to do this. The simple way, for small arrays at least, is Ben's answer. For large control sets, or ones that change frequently, (where your design is not finished, for example,) as long as you make sure to give them all serial names (Button1, Button2, Button3...), you can try something like this:
var
index: TComponent;
list: TObjectList;
begin
list := TObjectList.Create(false); //DO NOT take ownership
for index in frmMyForm do
if pos('Button', index.name) = 1 then
list.add(index);
//do more stuff once the list is built
end;
(Use a TObjectList<TComponent>, or something even more specific, if you're using D2009.) Build the list, based on the code above, then write a sorting function callback that will sort them based on name and use it to sort the list, and you've got your "array."
You may not be able to reference public properties of your form in an array constant like that. Try doing it in your form constructor/OnCreate event instead.
procedure TForm1.FormCreate(Sender: TObject);
begin
Test[0] := Button1;
Test[1] := Button2;
Test[2] := Button3;
end;
This function will iterate over all the controls on a specified container, like a particular TPanel or even the entire form, and populate a specified TObjectList with your TImage controls.
procedure TForm1.AddImageControlsToList(AParent: TWinControl; AList: TObjectList; Recursive: boolean);
var
Index: integer;
AChild: TControl;
begin
for Index := 0 to AParent.ControlCount - 1 do
begin
AChild := AParent.Controls[Index];
if AChild is TImage then // Or whatever test you want to use
AList.Add(AChild)
else if Recursive and (AChild is TWinControl) then
AddImageControlsToList(TWinControl(AChild), AList, True);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
// Call like this or similar to get your list of images
// (assumes MyImageList is declared in Form)
MyImageList := TObjectList.Create(False);
AddImageControlsToList(Self, MyImageList, True);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
// Destroy the list
FreeAndNil(MyImageList);
end;
How about this?
procedure TForm1.FormCreate(Sender: TObject);
begin
for b := 1 to 110 do
Test[b] := FindComponent('Button' + IntToStr(b)) as TButton;
end;
You'll have to declare the array as a variable rather than a constant and it will have to go from 1 to 110 rather than 0 to 109 but that's no problem.
I use this all the time - it is simple and fast (despite Mr Wheeler's comment)- declare the maxbuttons as a constant
var
Form1: TForm1;
pbutton:array[1..maxbuttons] of ^tbutton;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
(* Exit *)
var k:integer;
begin
for k:=1 to maxbuttons do dispose(pbutton[k]);
close;
end;
procedure TForm1.FormActivate(Sender: TObject);
var k:integer;
begin
(*note the buttons must be Button1, Button2 etc in sequence or you need to
allocate them manually eg pbutton[1]^:=exitbtn etc *)
for k:=1 to maxbuttons do
begin
new(pbutton[k]);
pbutton[k]^:= tbutton(FindComponent('Button'+IntToStr(k)));
end;
end;
procedure TForm1.ButtonMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var k:integer;
b:boolean;
begin
b:=false;
k:=1;
while (k<= maxbuttons) and (not b) do
begin
if pbutton[k]^ = sender then (Note sender indicates which button has been clicked)
begin
{ found it so do something}
b:=true;
end;
k:=k+1;
end;
end;
Try this
var
TestA:TObjectList;
index:TComponent;
begin
TestA := TObjectList<TButton>.Create(false);
try
for index in Form7 do
if (pos is TButton) OR {or/and} (pos.tag and 8=8) then
TestA.add(TButton(index));
if TestA.Count>0 then //Fix:Exception out of range.
TestA[0].Caption := 'Test';
finally
TestA.Free;
end;
end;

Resources