Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   GUI-Design mit VCL / FireMonkey / Common Controls (https://www.delphipraxis.net/18-gui-design-mit-vcl-firemonkey-common-controls/)
-   -   Delphi Caret in ComboBox (https://www.delphipraxis.net/52542-caret-combobox.html)

trockentaucher 30. Aug 2005 17:04


Caret in ComboBox
 
Hallo Gemeinde...

Ich möchte per btn-Click eine Zeichenfolge in ComboBox.Text einfügen. Der einzufügene Text soll an die Cursor- bzw. Caret-Position eingefügt werden. ComboBoxen haben aber die Eigenschaft, SelStart=0 anzugeben, wenn der Focus nicht auf der ComboBox liegt. Bei dem btn-Click kann ich also mit SelStart nichts anfangen.
Nun habe ich bei dem OnKeyUp-Ereignis eine Zeile eingefügt, die SelStart in eine Variable wegsichert, die ich bei dem btn-Click auswerten kann... klappt auch soweit ganz gut. Wenn ich nun aber mit der Maus auf eine bestimmte Stelle im Text klicke, wird kein Ereignis ausgeführt, und somit auch die CaretPos nicht aktualisiert, leider... also auch keine Ideale Lösung.

Hat noch jemand eine Idee, wie ich CaretPos vom Text einer ComboBox abfragen kann bzw. wie ich einen Text bei einem btn-Click bei der CaretPos in ComboBox.Text eintragen kann?

-----------------------

Mir wurde mittlerweile geholfen; Anstelle der ComboBox die Komp. ComboBoxEx verwenden und dann einfach
Delphi-Quellcode:
ComboBoxEx.SelText := 'Text';
verwenden... Manchmal sieht man den Wald vor lauter Bäumen nicht...

DBR 30. Aug 2005 18:57

Re: Caret in ComboBox
 
Ich könnte mir vorstellen, dass das mit einem MouseHook innerhalb des eigenen Threads funktioniert.
Oder?

Gruß DBR

trockentaucher 31. Aug 2005 09:54

Re: Caret in ComboBox
 
Zitat:

Zitat von DBR
Ich könnte mir vorstellen, dass das mit einem MouseHook innerhalb des eigenen Threads funktioniert.

Ich stehe auf dem Schlauch. Was meinst du damit genau?!

DBR 31. Aug 2005 17:03

Re: Caret in ComboBox
 
Delphi-Quellcode:
const
  Hook: Hhook = 0;

var
  pos: integer;

procedure TForm1.ComboBox1KeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  pos := Combobox1.selstart;
end;

function MouseProc(HookCode: Integer; WParam: WPARAM;
  LParam: LPARAM): LRESULT; stdcall;
var pt: TPoint;
begin
  if (HookCode = HC_ACTION) and (WParam = WM_LBUTTONUP) then begin
    with Form1.Combobox1 do begin
      pt := ScreenToClient(PMOUSEHOOKSTRUCT(LParam)^.pt);
      if ptinrect(rect(0, 0, width, height), pt) then begin
        pos := selstart;
        result := 0;
        exit;
      end;
    end;
  end;
  result := CallNextHookEx(Hook, HookCode, WParam, LParam);
end;

procedure HookInst;
begin
  if Hook <> 0 then exit;
  Hook := SetWindowsHookEx(WH_MOUSE, MouseProc,
    HInstance, GetCurrentThreadId);
  if Hook = 0 then
    MessageBox(0, 'Der Hook wurde nicht installiert!',
      'FEHLER', MB_ICONERROR);
end;

procedure HookUninst;
begin
  if Hook <> 0 then UnhookWindowsHookEx(Hook);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
// ---- zum Testen ---------------
  combobox1.items[0] := 'Zeile 1';
  combobox1.items[1] := 'Zeile 2';
  combobox1.items[2] := 'Zeile 3';
  combobox1.items[3] := 'Zeile 4';
  combobox1.items[4] := 'Zeile 5';
// ------------------------------
  combobox1.itemindex := 0;
  HookInst;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  HookUninst;
end;

procedure TForm1.Button1Click(Sender: TObject);
var s: string;
begin
  s := Combobox1.text;
  insert('###', s, pos);
  Combobox1.text := s;
end;
Gruß DBR


Alle Zeitangaben in WEZ +1. Es ist jetzt 03:54 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz