Thema: Delphi ComboBox Hint onItem?

Einzelnen Beitrag anzeigen

BerndS

Registriert seit: 8. Mär 2006
Ort: Jüterbog
491 Beiträge
 
Delphi 12 Athens
 
#9

AW: ComboBox Hint onItem?

  Alt 22. Dez 2021, 10:11
Basierend auf dem Beispiel von Holger könnte man auch die Komponente erweitern.
Delphi-Quellcode:
unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type

  TComboBox = class(Vcl.StdCtrls.TComboBox)
  private
    FcbHintIndex: Integer;
    FHintWindow: THintWindow;
  protected
    procedure Change; override;
    procedure DropDown; override;
    procedure CloseUp; override;
    procedure InitiateAction; override;
  end;

  TForm1 = class(TForm)
    cb: TComboBox;
    procedure cbChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
  protected
  public
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.cbChange(Sender: TObject);
var
  P: TPoint;
begin
  case (Sender as TComboBox).ItemIndex of
    0:
      (Sender as TComboBox).Hint := 'null';
    1:
      (Sender as TComboBox).Hint := 'eins';
    2:
      (Sender as TComboBox).Hint := 'zwei';
    3:
      (Sender as TComboBox).Hint := 'drei';
    4:
      (Sender as TComboBox).Hint := 'vier';
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  I: Integer;
begin
  for I := 0 to 4 do
    cb.Items.Add(I.ToString);
end;

{ TComboBox }

procedure TComboBox.Change;
var
  P: TPoint;
begin
  inherited;
  if (Hint <> '') and DroppedDown and GetCursorPos(P) then
    FHintWindow.ActivateHint(Rect(P.X + 10, P.Y + 20, P.X + 100, P.Y + 40), Hint);
end;

procedure TComboBox.CloseUp;
begin
  inherited;
  FHintWindow.Hide;
  ControlStyle := ControlStyle - [csActionClient];
end;

procedure TComboBox.DropDown;
begin
  inherited;
  if not Assigned(FHintWindow) then
    FHintWindow := THintWindow.Create(Self);
  FcbHintIndex := -1;
  ControlStyle := ControlStyle + [csActionClient];
end;

procedure TComboBox.InitiateAction;
var
  Idx: Integer;
begin
  inherited;
  Idx := ItemIndex;
  if Idx <> FcbHintIndex then
  begin
    FcbHintIndex := ItemIndex;
    Change;
  end;
end;

end.
Code:
object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 220
  ClientWidth = 471
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -12
  Font.Name = 'Segoe UI'
  Font.Style = []
  ShowHint = True
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 15
  object cb: TComboBox
    Left = 32
    Top = 24
    Width = 145
    Height = 23
    Style = csDropDownList
    ParentShowHint = False
    ShowHint = True
    TabOrder = 0
    OnChange = cbChange
  end
end
Besser wäre natürlich eine neue abzuleiten und in Delphi zu registrieren.
  Mit Zitat antworten Zitat