Components

Control behind the other ones

Posted in Components

If you have some control and want to stay this control on top of the other ones, then you may put this control in front of the other controls by using BringToFront property. Use SendToBack property to reverse order of controls.

procedure TForm1.Button1Click(Sender: TObject);
begin
  Memo1.SendToBack;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Memo1.BringToFront;
end;

x

Check if ComboBox is dropped down

Posted in Components

Use SendMessage function with CB_GETDROPPEDSTATE message. If Result is true, then the list box of ComboBox is dropped down. You may send this message every second by using Timer component.

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  if SendMessage(ComboBox1.Handle,CB_GETDROPPEDSTATE,0,0)=1 then
    Caption:='ComboBox is opened'
  else Caption:='ComboBox is closed';
end;

Change focus to other component

Posted in Components

This example shows changing of focus between three components on a button click.

var
  i: Integer = 2;
...
procedure TForm1.Button1Click(Sender: TObject);
begin
  case (i mod 3) of
    1: Edit1.SetFocus;
    2: Memo1.SetFocus;
    0: ListBox1.SetFocus;
  end;
  Inc(i);
end;

Bold style for TreeView items

Posted in Components

Add CommCtrl string in uses chapter and use TV_ITEM structure for setting attributes of TreeView items.

uses
  CommCtrl;
...
procedure TForm1.TreeView1GetSelectedIndex(Sender: TObject;
  Node: TTreeNode);
var
  Item: TTVItem;
begin
  FillChar(Item, Sizeof(Item), 0);
  Item.hItem:=Node.ItemId;
  Item.Mask:=TVIF_STATE;
  Item.StateMask:=TVIS_BOLD or TVIS_CUT;
  Item.State:=TVIS_BOLD;
  TreeView_SetItem(Node.Handle, Item);
end;

Align menu text to the right

Posted in Components

If you want align menu items text to the right, then you may use this little deceit.

procedure TForm1.FormCreate(Sender: TObject);
var
  i: Integer;
begin
  with MainMenu1 do
    for i:=0 to Items.Count-1 do
      Items[i].Caption:=Chr(8)+Items[i].Caption;
end;