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 TComboEditLink Itemwechsel abfangen (https://www.delphipraxis.net/196767-tcomboeditlink-itemwechsel-abfangen.html)

StepByStep 18. Jun 2018 07:52

TComboEditLink Itemwechsel abfangen
 
Guten Morgen zusammen,

ich habe gerade versucht zu recherchieren, bin aber nicht wirklich weiter gekommen.

Auf meinem Formular liegt ein VirtualStringTree, wo eine Spalte als ComboBox-Spalte über OnCreateEditor deklariert ist. Jetzt würde ich gerne darauf reagieren, sobald ein Eintrag darin ausgewählt/gewechselt wird. Die ComboBox ist vom Typen TComboEditLink und leider hat diese Klasse kein OnChange-Event, ebenso wenig die Elternklasse TCustomEditLink. Mein Versuch einer GetMessages-Prozedur, wo ich dann CBN_SELCHANGE abfrage ist missglückt, da diese nie ankommt.

Hat vielleicht jemand einen Geistesblitz und kann mir da weiter helfen?

Gruß

KodeZwerg 18. Jun 2018 09:40

AW: TComboEditLink Itemwechsel abfangen
 
Da man nur Raten könnte wie Du es bei Dir realisiert hast, hier ein Link VirtualTreeview Example for Lazarus schau da im Abschnitt "Adding A Combobox", wenn Du so vorgehst mit Deinem VirtualStringTree dann sollte es klappen.
Zitat:

Adding A Combobox
I guess you have an open project on Lazarus IDE having VST on it and can edit the nodes. If not see the "Basic Tree Listview With 3 Columns" above, and atleast complete steps 1 to 21.
Below there is an unit file named combo. Copy that unit and save as combo.pas inside the project directory. Under your program's uses clause add combo. So it may look like:
Delphi-Quellcode:
uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs,
  VirtualStringTree, VirtualTrees, combo;
The combo.pas unit
unit combo;
 
{$mode delphi}
 
interface
 
uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs,
  VirtualStringTree, VirtualTrees, messages, windows, StdCtrls;
 
type
  TStringEditLink = class(TInterfacedObject, IVTEditLink)
  private
    FEdit: TWinControl;
    FTree: TVirtualStringTree;
    FNode: PVirtualNode;
    FColumn: Integer;
  protected
    procedure EditKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  public
    destructor Destroy; override;
    function BeginEdit: Boolean; stdcall;
    function CancelEdit: Boolean; stdcall;
    function EndEdit: Boolean; stdcall;
    function GetBounds: TRect; stdcall;
    function PrepareEdit(Tree: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex): Boolean; stdcall;
    procedure ProcessMessage(var Message: TMessage); stdcall;
    procedure SetBounds(R: TRect); stdcall;
  end;
 
implementation
 
destructor TStringEditLink.Destroy;
begin
  FEdit.Free;
  inherited;
end;
 
procedure TStringEditLink.EditKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  case Key of
    VK_ESCAPE:
      begin
        FTree.CancelEditNode;
        Key := 0;
        FTree.setfocus;
      end;
    VK_RETURN:
      begin
       PostMessage(FTree.Handle, WM_KEYDOWN, VK_DOWN, 0);
       Key := 0;
       FTree.EndEditNode;
       FTree.setfocus;
      end;
  end; //case
end;
 
function TStringEditLink.BeginEdit: Boolean;
begin
  Result := True;
  //FEdit.Height := (FTree.DefaultNodeHeight - 1); //Needed for editbox. Not combo
  FEdit.Show;
  TComboBox(FEdit).DroppedDown := True;
  FEdit.SetFocus;
end;
 
function TStringEditLink.CancelEdit: Boolean;
begin
  Result := True;
  FEdit.Hide;
end;
 
function TStringEditLink.EndEdit: Boolean;
var
  s: String;
begin
  Result := True;
  s := TComboBox(FEdit).Text;
  FTree.Text[FNode, FColumn] := s;
 
  FTree.InvalidateNode(FNode);
  FEdit.Hide;
  FTree.SetFocus;
end;
 
function TStringEditLink.GetBounds: TRect;
begin
  Result := FEdit.BoundsRect;
end;
 
function TStringEditLink.PrepareEdit(Tree: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex): Boolean;
begin
  Result := True;
  FTree := Tree as TVirtualStringTree;
  FNode := Node;
  FColumn := Column;
 
  FEdit.Free;
  FEdit := nil;
 
  FEdit := TComboBox.Create(nil);
  with FEdit as TComboBox do
    begin
      Visible := False;
      Parent := Tree;
      Items.Add('Google');
      Items.Add('Yahoo');
      Items.Add('Altavista');
      OnKeyDown := EditKeyDown;
    end;
end;
 
procedure TStringEditLink.ProcessMessage(var Message: TMessage);
begin
  FEdit.WindowProc(Message);
end;
 
procedure TStringEditLink.SetBounds(R: TRect);
var
  Dummy: Integer;
begin
  FTree.Header.Columns.GetColumnBounds(FColumn, Dummy, R.Right);
  FEdit.BoundsRect := R;
end;
 
end.
After saving the file, on the Form Designer select VST and on Object Inspector's Properties, scroll to TreeOptions -> MiscOptions, set toEditable to True. Then get to TreeOptions -> SelectionOptions, set toExtendedFocus to True.
Switch to Object Inspector's Events tab. Scroll to OnCreateEditor, double click and paste:
Delphi-Quellcode:
procedure TForm1.VSTCreateEditor(Sender: TBaseVirtualTree;
  Node: PVirtualNode; Column: TColumnIndex; out EditLink: IVTEditLink);
begin
  EditLink := TStringEditLink.Create;
end;
On Object Inspector's Events tab. Scroll to OnNewText, double click and paste:
Delphi-Quellcode:
procedure TForm1.VSTNewText(Sender: TBaseVirtualTree; Node: PVirtualNode;
  Column: TColumnIndex; NewText: WideString);
var
  Data: PTreeData;
begin
  Data := VST.GetNodeData(Node);
  case Column of
    0: Data^.Column0 := NewText;
    1: Data^.Column1 := NewText;
    2: Data^.Column2 := NewText;
  end;
end;
Run program, select a node and press F2 to get combobox. On pressing Enter new value should appear on the node.

StepByStep 19. Jun 2018 10:16

AW: TComboEditLink Itemwechsel abfangen
 
Hallo KodeZwerg,

danke für deine Antwort, auch wenn sie mich nicht sonderlich weiter gebracht hat in meinem Problem, aber ich konnte mein Problem trotzdem lösen. :)

Gruß


Alle Zeitangaben in WEZ +1. Es ist jetzt 19:58 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