Components

et focus to DBGrid for editing

Posted in Components

Use SetFocus method and EditorMode property of DBGrid. Also, you need to send EM_SETSEL message.

procedure TForm1.Button1Click(Sender: TObject);
var
  CurrentWindowFocus: THandle;
begin
  DbGrid1.SetFocus;
  DbGrid1.EditorMode:=True;
  CurrentWindowFocus:=Windows.GetFocus;
  SendMessage(CurrentWindowFocus, EM_SETSEL, 0, 0);
end;

Set columns in a TListBox

Posted in Components

Use Columns property for ListBox. For Columns values greater than 0, multiple columns accommodate the items as they wrap beyond the bottom of the list box. The Columns property specifies the number of columns that are visible without having to horizontally scroll the list box.

procedure TForm1.Button1Click(Sender: TObject);
begin
  ListBox1.Columns:=1;
end;

Set caret position in RichEdit

Posted in Components

Use standard properties of the RichEdit component. SelStart is a new position of the caret. SelLength is the number of characters that are selected. This parameter should be 0, if we want to work with caret only.

procedure TForm1.Button1Click(Sender: TObject);
begin
  with RichEdit1 do
  begin
    SetFocus;
    SelStart:=30;
    SelLength:=0;
  end;
end;

Set button multi-line caption

Posted in Components

You may use SetWindowLong function with BS_MULTILINE parameter to set multi line caption of some button.

procedure TForm1.Button1Click(Sender: TObject);
var
  TempButton: LongInt;
begin
  TempButton:=GetWindowLong(Button1.Handle, GWL_STYLE);
  SetWindowLong(Button1.Handle, GWL_STYLE, TempButton or BS_MULTILINE);
end;

Set background image in ListBox

Posted in Components

You should use onDrawItem event of ListBox component, if you want to create ListBox with some background picture. Pay attention to the IntersectRect function.

procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
var
  ImageRect1, NewRect, ImageRect2, ResRect: TRect;
begin
  ImageRect1 := Classes.Rect(
    0,
    0,
    Image1.Picture.Bitmap.Width,
    Image1.Picture.Bitmap.Height);

  ImageRect2 := ImageRect1;
  IntersectRect(ResRect, ImageRect2, Rect);

  NewRect.Left:=ImageRect1.Left;
  NewRect.Top:=ImageRect1.Top+ResRect.Top-ImageRect2.Top;
  NewRect.Right:=ImageRect1.Right;
  NewRect.Bottom:=ImageRect1.Bottom+
                    ResRect.Bottom-
                    ImageRect2.Bottom;

  ListBox1.Canvas.CopyRect(
    ResRect,
    Image1.Picture.Bitmap.Canvas,
    NewRect);

  ListBox1.Canvas.Brush.Style:=bsClear;
  ListBox1.Canvas.TextOut(
    Rect.Left+2,
    Rect.Top,
    ExtractFileName(ListBox1.Items[Index]));