Is it possible to disable view source option in Delphi Chromium Embedded ?
I haven't found anything suitable in properties/methods list.
There are no direct settings or events allowing to hide Chromium popup menu items. However you have at least few options how to continue, you can for instance:
1. Tell user that the View source option is forbidden and decline the action
You can decide what action will you allow or decline in the OnMenuAction event handler, where if you assign True to the Result parameter the action is declined. The following code checks that you have performed the view source action and if so, decline the action and show the information message:
type
TCefMenuId = TCefHandlerMenuId;
procedure TForm1.Chromium1MenuAction(Sender: TObject;
const browser: ICefBrowser; menuId: TCefMenuId; out Result: Boolean);
begin
if menuId = MENU_ID_VIEWSOURCE then
begin
Result := True;
ShowMessage('View page source is not allowed!');
end;
end;
2. Fake the menu item to something custom by changing menu item's caption with its action
You can take advantage of the menu item for something else by changing the menu item's caption and executing some custom action. The following sample code shows how to change the view source menu item into the about box menu item:
type
TCefMenuId = TCefHandlerMenuId;
procedure TForm1.Chromium1GetMenuLabel(Sender: TObject;
const browser: ICefBrowser; menuId: TCefMenuId; var caption: ustring;
out Result: Boolean);
begin
if menuId = MENU_ID_VIEWSOURCE then
caption := 'About my application...';
end;
procedure TForm1.Chromium1MenuAction(Sender: TObject;
const browser: ICefBrowser; menuId: TCefMenuId; out Result: Boolean);
begin
if menuId = MENU_ID_VIEWSOURCE then
begin
Result := True;
ShowMessage('About box...!');
end;
end;
3. Create you own custom page (frame) popup menu
You can create your own popup menu, but you need to consider that this menu is quite hardcoded, so you will need to maintain it if you'll need to have it the same with each new version of Delphi Chromium wrapper. Here is the code how to create the page menu without view source menu item:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Menus, cefvcl, ceflib;
type
PCefMenuInfo = PCefHandlerMenuInfo;
type
TForm1 = class(TForm)
Chromium1: TChromium;
procedure FormCreate(Sender: TObject);
procedure Chromium1BeforeMenu(Sender: TObject; const browser: ICefBrowser;
const menuInfo: PCefMenuInfo; out Result: Boolean);
private
PageMenu: TPopupMenu;
procedure OnNavigateBackMenuItemClick(Sender: TObject);
procedure OnNavigateForwardMenuItemClick(Sender: TObject);
procedure OnPrintMenuItemClick(Sender: TObject);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.OnNavigateBackMenuItemClick(Sender: TObject);
begin
Chromium1.Browser.GoBack;
end;
procedure TForm1.OnNavigateForwardMenuItemClick(Sender: TObject);
begin
Chromium1.Browser.GoForward;
end;
procedure TForm1.OnPrintMenuItemClick(Sender: TObject);
begin
Chromium1.Browser.GetFocusedFrame.Print;
end;
procedure TForm1.Chromium1BeforeMenu(Sender: TObject;
const browser: ICefBrowser; const menuInfo: PCefMenuInfo;
out Result: Boolean);
begin
if menuInfo.typeFlags = MENUTYPE_PAGE then
begin
Result := True;
PageMenu.Items[0].Enabled := browser.CanGoBack;
PageMenu.Items[1].Enabled := browser.CanGoForward;
PageMenu.Popup(menuInfo^.x, menuInfo^.y);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
MenuItem: TMenuItem;
begin
PageMenu := TPopupMenu.Create(Self);
MenuItem := TMenuItem.Create(PageMenu);
MenuItem.Caption := 'Back';
MenuItem.OnClick := OnNavigateBackMenuItemClick;
PageMenu.Items.Add(MenuItem);
MenuItem := TMenuItem.Create(PageMenu);
MenuItem.Caption := 'Forward';
MenuItem.OnClick := OnNavigateForwardMenuItemClick;
PageMenu.Items.Add(MenuItem);
MenuItem := TMenuItem.Create(PageMenu);
MenuItem.Caption := '-';
PageMenu.Items.Add(MenuItem);
MenuItem := TMenuItem.Create(PageMenu);
MenuItem.Caption := 'Print';
MenuItem.OnClick := OnPrintMenuItemClick;
PageMenu.Items.Add(MenuItem);
Chromium1.Load('www.stackoverflow.com');
end;
end.
Footnote
The type definitions used in all code samples are there because I've noticed that some version of Delphi Chromium has wrong event handler definitions.
Probably things changed over years, today a direct method exists:
uses
ceflib;
[..]
implementation
procedure TForm1.Chromium1BeforeContextMenu(Sender: TObject;
const browser: ICefBrowser; const frame: ICefFrame;
const params: ICefContextMenuParams; const model: ICefMenuModel);
begin
//model.Clear;
model.Remove(Integer(MENU_ID_VIEW_SOURCE));
end;
You can use model.Clear if you want to completely get rid of popup menu.
Related
I want to build a menu form that acts similar to ribbon keytips - you can
press and hold Alt, then press and release e. g. d, then release Alt to trigger an action or
press and release Alt, then press and release d to trigger the same action
I took inspiration at Hidden Main Menu in a delphi program automatically shown using Alt key and came up with the following demo:
unit Unit1;
interface
uses
Windows,
Messages,
SysUtils,
Variants,
Classes,
Graphics,
Controls,
Forms,
Dialogs,
ImgList,
StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
strict private
FShowKeyTips: Boolean;
procedure UpdateKeyTipState(AShowKeyTips: Boolean);
procedure WMExitMenuLoop(var Message: TMessage); message WM_EXITMENULOOP;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses
ShellAPI,
Menus;
{ TForm1 }
constructor TForm1.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Label1.Caption := 'Dummy';
end;
destructor TForm1.Destroy;
begin
inherited Destroy;
end;
procedure TForm1.WMExitMenuLoop(var Message: TMessage);
begin
UpdateKeyTipState(False);
end;
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
const
MAPVK_VK_TO_CHAR = 2;
// Adapted from dxBar.pas:
function IsTextCharForKeyTip(AKey: Word): Boolean;
var
ARes: UINT;
begin
ARes := MapVirtualKey(AKey, MAPVK_VK_TO_CHAR);
Result := ((ARes and $FFFF0000) = 0) and (Char(ARes) <> ' ') and (Char(ARes) in [#32..#255]);
end;
var
hk: string;
CheckKeyTips: Boolean;
begin
if (Key = VK_MENU) or (Key = VK_F10) then
begin
UpdateKeyTipState(True);
Exit;
end;
if FShowKeyTips then
CheckKeyTips := True
else
CheckKeyTips := Shift = [ssAlt];
if CheckKeyTips and IsTextCharForKeyTip(Key) then
begin
hk := Char(Key); // TODO: Handle analogouos to TdxBarItemLink.IsAccel?
if SameText(hk, 'd') then
begin
Caption := Caption + '+';
Key := 0;
Exit;
end;
end;
end;
procedure TForm1.UpdateKeyTipState(AShowKeyTips: Boolean);
begin
if FShowKeyTips = AShowKeyTips then
Exit;
FShowKeyTips := AShowKeyTips;
if AShowKeyTips then
Label1.Caption := 'Dummy (d)'
else
Label1.Caption := 'Dummy';
end;
end.
(Create a standard VCL app, add Label1 to Form1 and replace the contents of Unit1.pas with the above.)
The first bullet point works (adds a + to the form caption), however I can't make the second one work. I can't find where the d gets handled. I tried WM_(SYS)KEYDOWN, CM_DIALOGCHAR and more to no avail.
Any ideas?
As documented the Alt key, when pressed and released alone, "toggles in and out of menu bar mode". This is true even if your form does not have a window menu, the system menu is sufficient for the system to put the window into a modal menu loop. In this mode a non-accelerator will generate a WM_MENUCHAR message:
Sent when a menu is active and the user presses a key that does not
correspond to any mnemonic or accelerator key.
This is the message that you're looking for, read the character from the User field. And you don't have to track the Alt key, since the window being in a modal menu loop means the Alt key has been pressed once. Otherwise a key down message is generated instead of a menu character message.
Note that if your form does not have a system menu (in BorderIcons uncheck biSystemMenu) and a window menu, a regular WM_KEYDOWN will be sent which you're already handling.
This question already has answers here:
Cross-application drag-and-drop in Delphi
(2 answers)
Closed 8 years ago.
I am trying to drag and drop a video file (like .avi) from desktop But ı can not take it to the my program.But when ı try to drag and drop inside my program it works fine.For ex: I have an edittext and a listbox inside my pro and ı can move text that inside edittext to listbox.I could not get what is the difference ??
I take the video using openDialog.But ı wanna change it with drag and drop.
procedure TForm1.Button1Click(Sender: TObject);
begin
if OpenDialog1.Execute then
begin
MediaPlayer1.DeviceType:=dtAutoSelect;
MediaPlayer1.FileName := OpenDialog1.FileName;
Label1.Caption := ExtractFileExt(MediaPlayer1.FileName);
MediaPlayer1.Open;
MediaPlayer1.Display:=Self;
MediaPlayer1.DisplayRect := Rect(panel1.Left,panel1.Top,panel1.Width,panel1.Height);
panel1.Visible:=false;
MediaPlayer1.Play;
end;
end;
Here is a simple demo how to drag&drop files from Windows Explorer into a ListBox (for Delphi XE):
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
ListBox1: TListBox;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
protected
procedure WMDropFiles(var Msg: TMessage); message WM_DROPFILES;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses ShellAPI;
procedure TForm1.FormCreate(Sender: TObject);
begin
DragAcceptFiles(Handle, True);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
DragAcceptFiles(Handle, False);
end;
procedure TForm1.WMDropFiles(var Msg: TMessage);
var
hDrop: THandle;
FileCount: Integer;
NameLen: Integer;
I: Integer;
S: string;
begin
hDrop:= Msg.wParam;
FileCount:= DragQueryFile (hDrop , $FFFFFFFF, nil, 0);
for I:= 0 to FileCount - 1 do begin
NameLen:= DragQueryFile(hDrop, I, nil, 0) + 1;
SetLength(S, NameLen);
DragQueryFile(hDrop, I, Pointer(S), NameLen);
Listbox1.Items.Add (S);
end;
DragFinish(hDrop);
end;
end.
You can also use DropMaster from Raize software.
You can catch the WM_DROPFILES message.
First, set that your form will "accept" files from dragging in the FormCreate procedure:
DragAcceptFiles(Self.Handle, True);
After, declare the procedure in the desired form class:
procedure WMDropFiles(var Msg: TMessage); message WM_DROPFILES;
Finally, fill the procedure body as follows:
procedure TForm1.WMDropFiles(var Msg: TMessage);
begin
// do your job with the help of DragQueryFile function
DragFinish(Msg.WParam);
end
Alternatively, check out "The Drag and Drop Component Suite for Delphi" by Anders Melander. It works as-is with 32-bit and with some tweaking can be made to work with 64-bit as well (read the blog - it has been upgraded by 3rd parties).
I have a TChromium broser from Delphi Chromium Embedded (http://code.google.com/p/delphichromiumembedded). I would like to attach a context menu to it. How I can achieve that?
You need to handle the OnBeforeMenu event. In that event handler is enough to set the output parameter Result to True what will suppress the default context menus to popup. After that you can display your own menu on the positions obtained from the menuInfo structure.
Here's the code sample with a custom popup menu:
uses
ceflib, cefvcl;
procedure TForm1.FormCreate(Sender: TObject);
begin
Chromium1.Load('www.example.com');
end;
procedure TForm1.Chromium1BeforeMenu(Sender: TObject;
const browser: ICefBrowser; const menuInfo: PCefHandlerMenuInfo;
out Result: Boolean);
begin
Result := True;
PopupMenu1.Popup(menuInfo.x, menuInfo.y);
end;
procedure TForm1.PopupMenuItemClick(Sender: TObject);
begin
ShowMessage('You''ve clicked on a custom popup item :)');
end;
Update:
For dynamically created instance you have to assign the event handler manually. Try the following code.
uses
ceflib, cefvcl;
type
TForm1 = class(TForm)
Panel1: TPanel;
Button1: TButton;
PopupMenu1: TPopupMenu;
procedure Button1Click(Sender: TObject);
private
procedure ChromiumOnBeforeMenu(Sender: TObject;
const browser: ICefBrowser; const menuInfo: PCefHandlerMenuInfo;
out Result: Boolean);
public
{ Public declarations }
end;
implementation
procedure Form1.ChromiumOnBeforeMenu(Sender: TObject; const browser: ICefBrowser;
const menuInfo: PCefHandlerMenuInfo; out Result: Boolean);
begin
Result := True;
PopupMenu1.Popup(menuInfo.x, menuInfo.y);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Chromium: TChromium;
begin
// owner is responsible for destroying the component
// in this case you are telling to Panel1 to destroy
// the Chromium instance before he destroys itself,
// it doesn't affect the event handling
Chromium := TChromium.Create(Panel1);
Chromium.Parent := Panel1;
Chromium.Left := 10;
Chromium.Top := 10;
Chromium.Width := Panel1.Width - 20;
Chromium.Height := Panel1.Height - 20;
// this line is important, you are assigning the event
// handler for OnBeforeMenu event, so in fact you tell
// to the Chromium; hey if the OnBeforeMenu fires, run
// the code I'm pointing at, in this case will execute
// the ChromiumOnBeforeMenu procedure
Chromium.OnBeforeMenu := ChromiumOnBeforeMenu;
Chromium.Load('www.example.com');
end;
actually you dont need popupmenu and you dont have to have add vcl.menus unit into your application if you have already can build chromium's context menu. also chromium's own menu is more modern and clear look like and faster draw perfrmance rather than a vcl which uses vintage win32 api library.
cef3 has its menu totally configurable like this.
procedure Tfmmain.Chromium1BeforeContextMenu(Sender: TObject;
const browser: ICefBrowser; const frame: ICefFrame;
const params: ICefContextMenuParams; const model: ICefMenuModel);
begin
model.Clear;
model.AddItem(1, 'Your Command 1');
model.AddItem(2, 'Your Command 2');
model.AddSeparator;
model.AddItem(3, 'Your Command 3');
model.AddItem(4, 'your Command 4');
model.AddSeparator;
model.AddItem(999, 'Quit');
model.SetAccelerator(1, VK_RIGHT, false, false, false);
model.SetAccelerator(2, VK_LEFT, false, false, false);
model.SetAccelerator(3, VK_DOWN, false, false, false);
model.SetAccelerator(4, VK_UP, false, false, false);
model.SetAccelerator(999, VK_ESCAPE, false, false, false);
end;
procedure Tfmmain.Chromium1ContextMenuCommand(Sender: TObject;
const browser: ICefBrowser; const frame: ICefFrame;
const params: ICefContextMenuParams; commandId: Integer;
eventFlags: TCefEventFlags; out Result: Boolean);
begin
case commandId of
1:
begin
DoIt1;
Result := true;
end;
2:
begin
DoIt2;
Result := true;
end;
3:
begin
DoIt3;
Result := true;
end;
4:
DoIt4;
Result := true;
end;
999:
begin
Application.MainForm.Close;
Result := true;
end;
end;
end;
note:SetAccelerator shortcuts only functional if popup appears.so you may need onPreKeyEvent
i use webbrowser in my delphi application . how can i disable Ctrl+P to prevent print ?
Look at the code below :
var
mClass : Array[0..1024] of Char;
begin
if (GetClassName(Msg.hwnd, mClass, 1024) > 0) then
begin
if (StrIComp(#mClass, 'Internet Explorer_Server') = 0) then
begin
if Msg.message = WM_KEYDOWN then
Handled := (Msg.wParam = Ord('P')) and (GetKeyState(VK_CONTROL) <> 0);
end
end;
end;
To prevent messages sent to a TWebBrowser control , we can get the class name of message receiver and then compare the class name with "Internet Explorer_Server" that is the IE Server Calss Name , if class-names where equal then you can make sure that the message sent to WebBrowser Control , now you can Handle Message arrived ...
In the code above we do this to Handle Ctrl+P Shortcut , but you can use this idea for more like disabling Context Menu or ...
Notice that when a page loaded in the WebBrowser , messages will post to IE Server not to TWebBrowser Handle ...
First Put a TApplicationEvents on the Form , next Copy/Paste code from here to it`s OnMessage Event ...
Good Luck ...
I used EmbeddedWB and my problem solved via this tiny code :
procedure TForm1.EmbeddedWb1KeyDown(Sender: TObject; var Key: Word; ScanCode: Word;
Shift: TShiftState);
begin
Key := 0;
end;
This is an old thread, but I wanted to update with the method I found to work. This builds on the helpful post by #Mahmood_N.
Notice that I first wrote the code to get the class name, and compared that to 'Shell Embedding', which is what showed up for TWebBrowser messages (see here: https://support.microsoft.com/en-us/kb/244310). But my application will include more than one TWebBrowser, so I modified it to be better, by getting the window handle directly, and use that for comparison with the handle of the window message.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, AppEvnts, StdCtrls, OleCtrls, SHDocVw, ActiveX;
type
TForm1 = class(TForm)
WebBrowser: TWebBrowser;
ApplicationEvents1: TApplicationEvents;
procedure FormCreate(Sender: TObject);
procedure ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean);
private
{ Private declarations }
WBHandle : THandle;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function GetStrClassName(Handle: THandle): String;
var
Buffer: array[0..MAX_PATH] of Char;
begin
Windows.GetClassName(Handle, #Buffer, MAX_PATH);
Result := String(Buffer);
end;
procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean);
var
ClassName : string;
begin
ClassName := GetStrClassName(Msg.hwnd);
//if Pos('Shell Embedding', ClassName) > 0 then begin
if Msg.hwnd = WBHandle then begin
if Msg.message = WM_KEYDOWN then begin
Handled := (Msg.wParam = Ord('P')) and (GetKeyState(VK_CONTROL) <> 0);
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var Win : IOLEWindow;
WinHandle : HWND;
begin
WBHandle := 0;
if WebBrowser.ControlInterface.QueryInterface(IOleWindow, Win) = 0 then begin
if Win.GetWindow(WinHandle) = 0 then begin
WBHandle := WinHandle;
end;
end;
end;
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;