Components not being found by FindComponent() - delphi

I have created a login form which holds buttons corresponding to users' names held in an Access file. The buttons are created in the OnCreate event because I don't want to have to create them each time the screen is shown.
The buttons display as expected, and I have created LogOn and LogOff procedures, which both work as I expected.
The next step was to only display the buttons for users who are currently logged on to the system. To do this, I have created the following code in the OnActivate event:
procedure TUserInForm.FormActivate(Sender: TObject);
var
btn : TLBButton;
begin
With UserQuery do begin;
first;
while (not eof) do begin
BtnName := FieldByName('UserName').AsString;
Btn := TLBButton(FindComponent(BtnName));
if (Btn <> nil) then
if (FieldByName('LoggedIn').AsBoolean = True) then Btn.Visible := True else Btn.Visible := False;
next;
end;
end;
end;
However, it doesn't find any of the buttons - they are all nil. The code throws an Access Violation exception if I remove the nil check. But, at no point in the code do I destroy the buttons or the form itself. The buttons exist, because I can see them on the form. The BtnName variable is global within the unit. I've checked that the BtnName variable is populated correctly from the table.
I've used code similar to this to find components before, with no problems. In fact, I "stole" the code shown above from another procedure (with the obvious changes) in which it works fine. The logs show no errors.
Can anyone suggest some approach to fixing this problem? It's very frustrating!

FindComponent() searches the owned-components-list of the component that it is called on. I’m assuming your OnCreate handler creates the buttons with the Form as their Owner. But the with block will cause FindComponent() to be called on the UserQuery component instead of the Form. That would explain why the buttons are not being found.
So, you can either:
use Self.FindComponent() instead, since the OnActivate handler is being called on the Form, so Self will point at the Form:
procedure TUserInForm.FormActivate(Sender: TObject);
var
Btn : TLBButton;
BtnName : string;
begin
with UserQuery do begin
First;
while (not Eof) do begin
BtnName := FieldByName('UserName').AsString;
Btn := TLBButton(Self.FindComponent(BtnName));
if (Btn <> nil) then
Btn.Visible := FieldByName('LoggedIn').AsBoolean;
Next;
end;
end;
end;
get rid of the with block altogether, as it is generally considered to be bad practice to use with anyway in non-trivial cases (this situation is a good example of why):
procedure TUserInForm.FormActivate(Sender: TObject);
var
Btn : TLBButton;
BtnName : string;
begin
UserQuery.First;
while (not UserQuery.Eof) do begin
BtnName := UserQuery.FieldByName('UserName').AsString;
Btn := TLBButton(FindComponent(BtnName));
if (Btn <> nil) then
Btn.Visible := UserQuery.FieldByName('LoggedIn').AsBoolean;
UserQuery.Next;
end;
end;
if you want to keep using the with block, you can move the button search to a separate function that does not exist in the UserQuery component, so the with won’t be confused about which component to call the function on:
procedure TUserInForm.FormActivate(Sender: TObject);
var
Btn : TLBButton;
BtnName : string;
begin
with UserQuery do begin
First;
while (not Eof) do begin
BtnName := FieldByName('UserName').AsString;
Btn := FindButton(BtnName);
if (Btn <> nil) then
Btn.Visible := FieldByName('LoggedIn').AsBoolean;
Next;
end;
end;
end;
function TUserInForm.FindButton(const BtnName: string): TLBButton;
begin
Result := TLBButton(FindComponent(BtnName));
end;
Now, that being said, it would be a better design to add the created buttons to a list that you manage for yourself, rather than a list that the VCL manages on your behalf. Then you will always know exactly where to find the buttons. For example:
type
TUserInForm = class(TForm)
UserQuery: TQuery;
...
procedure FormActivate(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
...
private
Buttons: TList;
function FindButton(const BtnName: string): TLBButton;
...
end;
...
procedure TUserInForm.FormCreate(Sender: TObject);
var
Btn : TLBButton;
begin
Buttons := TList.Create;
...
Btn := TLBButton.Create(Self);
Btn.Name := ...
Buttons.Add(Btn);
...
end;
procedure TUserInForm.FormDestroy(Sender: TObject);
begin
Buttons.Free;
end;
procedure TUserInForm.FormActivate(Sender: TObject);
var
Btn : TLBButton;
BtnName : string;
begin
UserQuery.First;
while (not UserQuery.Eof) do begin
BtnName := UserQuery.FieldByName('UserName').AsString;
Btn := FindButton(BtnName);
if (Btn <> nil) then begin
Btn.Visible := UserQuery.FieldByName('LoggedIn').AsBoolean;
end;
UserQuery.Next;
end;
end;
function TUserInForm.FindButton(const BtnName: string): TLBButton;
var
i: integer;
begin
for i := 0 to Buttons.Count-1 do begin
Result := TLBButton(Buttons[i]);
if Result.Name = BtnName then Exit;
end;
Result := nil;
end;

Related

How to dynamically create an event OnClick for a Groupbox? [duplicate]

Before I start I must state that no other stack overflow post on this topic had helped me yet
I have a dynamic button called by btnApply
It is created dynamically on a dynamic form frmSort by a on click event of static button btnSort on static form frmTable
Under the global scope var of frmTable is declared
btnApply: TButton;
Procedure btnApplyClick(Sender:TObject);
//other vars
Under the btnSort on click
//other code
btnApply:= TButton.create(frmSort);
//all its properties
BtnApply.onclick:= btnApplyClick;
//other code
Then later
Procedure btnApplyClick(Sender:TObject);
Begin
//it's code it has to execute
End;
I get an error message at the "BtnApply.onclick:= btnApplyClick;"
Line of incompatible types between method pointer and regular procedure
How do I make this work?
Thanks in advance
Your btnApplyClick needs to be a method of an object. Since the button has to be on a form to be useful anyway, make it a method of the form itself:
type
TfrmSort = class(TForm)
// UI controls listed here
public
procedure btnApplyClick(Sender: TObject);
end;
implementation
procedure TfrmSort.btnApplyClick(Sender: TObject);
begin
(Sender as TButton).Caption := 'You clicked me';
end;
procedure TfrmSort.FormCreate(Sender: TObject);
var
Btn: TButton;
begin
Btn := TButton.Create(Self);
Btn.Parent := Self;
Btn.Top := 100;
Btn.Left := 100;
Btn.OnClick := btnApplyClick;
end;
If for some reason you can't make it a form method (although I can't see how this would be the case for a visual control), you can make it a method of any object, like this:
implementation
// You must use StdCtrls in order to have the types available if
// it's not already in your uses clause
type
TDummyButtonClickObj = class
class procedure ButtonClickHandler(Sender: TObject);
end;
{ TDummyButtonClickObj }
class procedure TDummyButtonClickObj.ButtonClickHandler(Sender: TObject);
begin
(Sender as TButton).Caption := 'You clicked me.';
end;
procedure TForm2.FormCreate(Sender: TObject);
begin
with TButton.Create(Self) do
begin
Parent := Self;
Top := 100;
Left := 100;
Caption := 'Click here';
OnClick := TDummyButtonClickObj.ButtonClickHandler;
end;
end;
As others have stated, the event handler should be a member of a class. That is what the event is expecting. However, it is also possible to use a non-member procedure as the event handler. It just takes a couple of extra steps to set up.
Add an extra explicit parameter to account for the Self pointer:
procedure btnApplyClick(Self: Pointer; Sender: TObject);
Use the TMethod record to assign the procedure to the button:
var
btnApply: TButton;
M: TMethod;
//other vars
Procedure btnApplyClick(Self: Pointer; Sender: TObject);
...
btnApply := TButton.create(frmSort);
//all its properties
M.Code := #btnApplyClick;
M.Data := nil; // can be anything you want passed to the Self parameter
BtnApply.onclick := TNotifyEvent(M);
//other code
...
procedure btnApplyClick(Self: Pointer; Sender: TObject);
Begin
// code to execute
End;

delphi custom component with default popupmenu item

I use a custom listview component and I need it to have a popupmenu item "copy data to clipboard". If there is no assigned popup, I create one and add the menuitem, if there is already a menu assigned, add the item to the current popup. Tried to put the code in the constructor, but then I realized, that popupmenu is still not created or associated to my listview. So any idea when to create my default item?
constructor TMyListView.Create(AOwner: TComponent);
var
FpopupMenu: TPopupMenu;
begin
inherited;
.....
FPopUpMenuItem := TMenuItem.Create(self);
FPopUpMenuItem.Caption := 'Copy data to clipboard';
FPopUpMenuItem.OnClick := PopupMenuItemClick;
if assigned(PopupMenu) then begin
popupMenu.Items.Add(FPopUpMenuItem);
end
else begin
FpopupMenu := TPopupMenu.Create(self);
FpopupMenu.Items.Add(FPopUpMenuItem);
PopupMenu := FpopupMenu;
end;
...
end;
Override the virtual TControl.DoContextPopup() method, eg:
type
TMyListView = class(TListView)
protected
...
procedure DoContextPopup(MousePos: TPoint; var Handled: Boolean); override;
...
end;
procedure TMyListView.DoContextPopup(MousePos: TPoint; var Handled: Boolean);
var
LPopupMenu: TPopupMenu;
LItem: TMenuItem;
function IsSameEvent(const E1, E2: TNotifyEvent): Boolean;
begin
Result := (TMethod(E1).Code = TMethod(E2).Code) and
(TMethod(E1).Data = TMethod(E2).Data);
end;
begin
inherited DoContextPopup(MousePos, Handled);
if Handled then Exit;
LPopupMenu := PopupMenu;
if not Assigned(LPopupMenu) then
begin
LPopupMenu := TPopupMenu.Create(Self);
PopupMenu := LPopupMenu;
end;
for I := 0 to LPopupMenu.Items.Count-1 do
begin
LItem := LPopupMenu.Items[I];
if IsSameEvent(LItem.OnClick, PopupMenuItemClick) then
Exit;
end;
LItem := TMenuItem.Create(Self);
LItem.Caption := 'Copy data to clipboard';
LItem.OnClick := PopupMenuItemClick;
LPopupMenu.Items.Add(LItem);
end;
The accepted answer indeed works perfectly - unless you add keyboard shortcuts to your menu item. If you do, these won't work before the popup menu has been accessed in some other way, because the items will not have been created.
If you need shortcuts, it may therefore be preferable to move the code from DoContextPopup to Loaded. Most simply,
procedure Loaded; override;
...
procedure Loaded;
var
MI: TMenuItem;
ItemCovered: boolean;
i: integer;
begin
inherited;
if not Assigned(PopupMenu) then
PopupMenu:=TPopupMenu.Create(self);
ItemCovered:=false;
for i := 0 to PopupMenu.Items.Count-1 do
if IsSameEvent(PopupMenu.Items[I].OnClick, CopyDataToClipboardClick) then begin
ItemCovered:=true;
break;
end;
if not ItemCovered then begin
MI:=TMenuItem.Create(PopupMenu);
MI.Caption:='Copy data to clipboard';
MI.OnClick:=CopyDataToClipboardClick;
MI.ShortCut:=ShortCut(Ord('C'),[ssShift,ssCtrl]);
PopupMenu.Items.Add(MI);
end;
end;
This won't check for popup menus added on runtime, but probably serve most cases better.

Delphi IDE Menu integration - INTAServices

I successfully installed my menu item inside Delphi using INTAServices40 but the problem is - menu is missing the next time Delphi starts?! Actually, two menu items are installed; One under Help menu which is ALWAYS shown (IOTAWizardMenu), but the one under Tools menu (TEST menu item) is missing the next time Delphi starts. How to fix this?
unit TESTMENU;
interface
uses
ToolsAPI, Classes, Windows, vcl.Menus, vcl.dialogs;
type
TCustomMenuItem = class(TNotifierObject, IOTAWizard, IOTAMenuWizard)
function GetIDString: string;
function GetName: string;
function GetState: TWizardState;
// Launch the AddIn
procedure Execute;
function GetMenuText: string;
end;
TCustomMenuHandler = class(TObject)
// Handle custom menu
procedure HandleClick(Sender: TObject);
end;
procedure Register;
implementation
var
mnuitem: TMenuItem;
CustomMenuHandler: TCustomMenuHandler;
procedure TCustomMenuItem.Execute;
begin
ShowMessage('IOTAWizardMenu based menu item');
end;
function TCustomMenuItem.GetIDString: string;
begin
Result := 'TMS.MenuSample';
end;
function TCustomMenuItem.GetMenuText: string;
begin
Result := 'IOTAWizardMenu';
end;
function TCustomMenuItem.GetName: string;
begin
Result := 'TMSMenuSample';
end;
function TCustomMenuItem.GetState: TWizardState;
begin
Result := [wsEnabled];
end;
procedure TCustomMenuHandler.HandleClick(Sender: TObject);
begin
ShowMessage('INTAServices40.MainMenu based menu item');
end;
procedure AddIDEMenu;
var
NTAServices: INTAServices40;
begin
NTAServices := BorlandIDEServices as INTAServices40;
// avoid inserting twice
if NTAServices.MainMenu.Items[9].Find('TEST') = nil then
begin
CustomMenuHandler := TCustomMenuHandler.Create;
mnuitem := TMenuItem.Create(nil);
mnuitem.Caption := 'TEST';
mnuitem.OnClick := CustomMenuHandler.HandleClick;
NTAServices.MainMenu.Items[9].Add(mnuitem)
end;
end;
procedure RemoveIDEMenu;
var
NTAServices: INTAServices40;
begin
if Assigned(mnuitem) then
begin
NTAServices := BorlandIDEServices as INTAServices40;
NTAServices.MainMenu.Items[9].Remove(mnuitem);
mnuitem.Free;
if Assigned(CustomMenuHandler) then
CustomMenuHandler.Free;
end;
end;
procedure Register;
begin
AddIDEMenu;
RegisterPackageWizard(TCustomMenuItem.Create);
end;
initialization
mnuitem := nil;
CustomMenuHandler := nil;
finalization
RemoveIDEMenu;
end.
So, my first problem is how to get menu item TEST shown each time Delphi starts.. Also, I would like to add icon next to the menu item TEST. Any directions?
Thank you
EDIT:
I just found out my package is delayed loading. Reading the Internet people say ForceDemandLoadState(dlDisable) should be called. But, this is not helping me also....
NTAServices.MainMenu.Items[9] may return different things at different times as the IDE is loading its packages, also there are menu items whose sub-items are managed by the IDE at runtime (e.g. the Window menu).
You could look up the Help menu item component by name:
procedure AddIDEMenu;
var
HelpMenu: TComponent;
begin
HelpMenu := Application.MainForm.FindComponent('HelpMenu');
if (HelpMenu is TMenuItem) and (TMenuItem(HelpMenu).Find('TEST') = nil) then
begin
CustomMenuHandler := TCustomMenuHandler.Create;
mnuitem := TMenuItem.Create(nil);
mnuitem.Caption := 'TEST';
mnuitem.OnClick := CustomMenuHandler.HandleClick;
TMenuItem(HelpMenu).Add(mnuitem);
end;
end;
procedure RemoveIDEMenu;
var
HelpMenu: TComponent;
begin
if Assigned(mnuitem) then
begin
HelpMenu := Application.MainForm.FindComponent('HelpMenu');
if HelpMenu is TMenuItem then
TMenuItem(HelpMenu).Remove(mnuitem);
mnuitem.Free;
CustomMenuHandler.Free;
end;
end;

How can a control be notified when its parent receives and loses focus in Delphi?

As the title says, I'd like a component (say, a label) to be notified when it's parent (say, a panel) receives and loses focus. I wandered a bit in Delphi source, in hope of using TControl.Notify, but it's only used to notify child controls of some property changes like font and color. Any suggestions?
Whenever the active control in an application changes, a CM_FOCUSCHANGED message is broadcast to all controls. Simply intercept it, and act accordingly.
Also, I assumed that by when it's parent (say, a panel) receives and loses focus you mean whenever a (nested) child control on that parent/panel receives or loses focus.
type
TLabel = class(StdCtrls.TLabel)
private
function HasCommonParent(AControl: TWinControl): Boolean;
procedure CMFocusChanged(var Message: TCMFocusChanged);
message CM_FOCUSCHANGED;
end;
procedure TLabel.CMFocusChanged(var Message: TCMFocusChanged);
const
FontStyles: array[Boolean] of TFontStyles = ([], [fsBold]);
begin
inherited;
Font.Style := FontStyles[HasCommonParent(Message.Sender)];
end;
function TLabel.HasCommonParent(AControl: TWinControl): Boolean;
begin
Result := False;
while AControl <> nil do
begin
if AControl = Parent then
begin
Result := True;
Break;
end;
AControl := AControl.Parent;
end;
end;
If you don't like to subclass TJvGradientHeader, then it is possible to design this generically by the use of Screen.OnActiveControlChange:
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
FHeaders: TList;
procedure ActiveControlChanged(Sender: TObject);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
FHeaders := TList.Create;
FHeaders.Add(Label1);
FHeaders.Add(Label2);
Screen.OnActiveControlChange := ActiveControlChanged;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
FHeaders.Free;
end;
function HasCommonParent(AControl: TWinControl; AMatch: TControl): Boolean;
begin
Result := False;
while AControl <> nil do
begin
if AControl = AMatch.Parent then
begin
Result := True;
Break;
end;
AControl := AControl.Parent;
end;
end;
procedure TForm1.ActiveControlChanged(Sender: TObject);
const
FontStyles: array[Boolean] of TFontStyles = ([], [fsBold]);
var
I: Integer;
begin
for I := 0 to FHeaders.Count - 1 do
TLabel(FHeaders[I]).Font.Style :=
FontStyles[HasCommonParent(Screen.ActiveControl, TLabel(FHeaders[I]))];
end;
Note that I chose TLabel to demonstrate this works also for TControl derivatives.

How can I filter the contents of a combo box based on what's been typed?

We have a combo box with more than 100 items.
We want to filter out the items as we enter characters in combo box. For example if we entered 'ac' and click on the drop down option then we want it to display items starting with 'ac' only.
How can I do this?
Maybe you'd be happier using the autocompletion features built in to the OS. I gave an outline of how to do that here previously. Create an IAutoComplete object, hook it up to your combo box's list and edit control, and the OS will display a drop-down list of potential matches automatically as the user types. You won't need to adjust the combo box's list yourself.
To expand on Rob's answer about using the OnChange event, here is an example of how to do what he suggests.
procedure TForm1.FormCreate(Sender: TObject);
begin
FComboStrings := TStringList.Create;
FComboStrings.Add('Altair');
FComboStrings.Add('Alhambra');
FComboStrings.Add('Sinclair');
FComboStrings.Add('Sirius');
FComboStrings.Add('Bernard');
FComboStrings.Sorted := True;
ComboBox1.AutoComplete := False;
ComboBox1.Items.Text := FComboStrings.Text;
ComboBox1.Sorted := True;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
FreeAndNil(FComboStrings);
end;
procedure TForm1.ComboBox1Change(Sender: TObject);
var
Filter: string;
i: Integer;
idx: Integer;
begin
// Dropping down the list puts the text of the first item in the edit, this restores it
Filter := ComboBox1.Text;
ComboBox1.DroppedDown := True;
ComboBox1.Text := Filter;
ComboBox1.SelStart := Length(Filter);
for i := 0 to FComboStrings.Count - 1 do
if SameText(LeftStr(FComboStrings[i], Length(ComboBox1.Text)), ComboBox1.Text) then
begin
if ComboBox1.Items.IndexOf(FComboStrings[i]) < 0 then
ComboBox1.Items.Add(FComboStrings[i]);
end
else
begin
idx := ComboBox1.Items.IndexOf(FComboStrings[i]);
if idx >= 0 then
ComboBox1.Items.Delete(idx);
end;
end;
My brief contribution working with objects in the combobox:
procedure FilterComboBox(Combo: TComboBox; DefaultItems: TStrings);
function Origin: TStrings;
begin
if Combo.Tag = 0 then
begin
Combo.Sorted := True;
Result := TStrings.Create;
Result := Combo.Items;
Combo.Tag := Integer(Result);
end
else
Result := TStrings(Combo.Tag);
end;
var
Filter: TStrings;
I: Integer;
iSelIni: Integer;
begin
if(Combo.Text <> EmptyStr) then
begin
iSelIni:= Length(Combo.Text);
Filter := TStringList.Create;
try
for I := 0 to Origin.Count - 1 do
if AnsiContainsText(Origin[I], Combo.Text) then
Filter.AddObject(Origin[I], TObject(Origin.Objects[I]));
Combo.Items.Assign(Filter);
Combo.DroppedDown:= True;
Combo.SelStart := iSelIni;
Combo.SelLength := Length(Combo.Text);
finally
Filter.Free;
end;
end
else
Combo.Items.Assign(DefaultItems);
end;
You can handle the combo box's OnChange event. Keep a master list of all items separate from the UI control, and whenever the combo box's edit control changes, adjust the combo box's list accordingly. Remove items that don't match the current text, or re-add items from the master list that you removed previously.
As Rob already answered, you could filter on the OnChange event, see the following code example. It works for multiple ComboBoxes.
{uses}
Contnrs, StrUtils;
type
TForm1 = class(TForm)
ComboBox1: TComboBox;
ComboBox2: TComboBox;
procedure FormCreate(Sender: TObject);
procedure ComboBoxChange(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
FComboLists: TList;
procedure FilterComboBox(Combo: TComboBox);
end;
implementation
{$R *.dfm}
procedure TForm1.ComboBoxChange(Sender: TObject);
begin
if Sender is TComboBox then
FilterComboBox(TComboBox(Sender));
end;
procedure TForm1.FilterComboBox(Combo: TComboBox);
function Origin: TStrings;
begin
if Combo.Tag = 0 then
begin
Combo.Sorted := True;
Result := TStringList.Create;
Result.Assign(Combo.Items);
FComboLists.Add(Result);
Combo.Tag := Integer(Result);
end
else
Result := TStrings(Combo.Tag);
end;
var
Filter: TStrings;
I: Integer;
begin
Filter := TStringList.Create;
try
for I := 0 to Origin.Count - 1 do
if AnsiStartsText(Combo.Text, Origin[I]) then
Filter.Add(Origin[I]);
Combo.Items.Assign(Filter);
Combo.SelStart := Length(Combo.Text);
finally
Filter.Free;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
FComboLists := TObjectList.Create(True);
// For Each ComboBox, set AutoComplete at design time to false:
ComboBox1.AutoComplete := False;
ComboBox2.AutoComplete := False;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
FComboLists.Free;
end;

Resources