Board index » delphi » Intercepting Windows Messages - Was TTreeView - Not...

Intercepting Windows Messages - Was TTreeView - Not...

Re-Post

In Windows Explorer TreeView, when an invalid character is placed in the
edit box, and the user kills the focus to the edit box, an error message
comes up, then returns to the edit box.
Why can't we do that with Delphi's implementation of the TreeView?

I've Overridden the WndProc Function of the TreeView.

But, I need help.

Here is what I have so far: (My questions are in the code, as comments)

procedure TListenTreeView.WndProc(var Message: TMessage);
Var
  EditControl : HWND;
  s : String;
  SLen : Integer;
  NotifyNum : Integer;
  TempElement : TElement;
begin

// If Msg is not WM_COMMAND, then I don't care about it, Let Inherited do
its work.
  IF (Message.Msg <> WM_COMMAND) Then Begin
    Inherited;
    Exit;
  End;

  NotifyNum := Message.WParamHi;

  // This piece will not allow the First character in the edit box to be a
Numeric.
 // If the user types it, it just won't show up.  This piece works.
  If (NotifyNum = EN_UPDATE) Then Begin
    EditControl := Message.LParam;
    SetLength(s,60); // Max Characters are 60... Change to a Const!
    SLen := SendMessage(EditControl,WM_GETTEXT,60,Integer(pChar(S)));
    SetLength(s,SLen);
    IF (Length(s) > 0) and (S[1] in ['0'..'9']) Then Begin
        SendMessage(EditControl,WM_SETTEXT,0,Integer(PChar(S))+1);
    End;
    Inherited;
  End;

  // ***** Here is where I get into trouble...... *******
// ******************************************
  IF (NotifyNum = EN_KILLFOCUS) Then Begin

        // *********************************************************
        // If the EditBox is Empty or The Name is the same as another node
at the same level, I have to be able to cancel any pending messages after
this one, and refocus the Node.Edit control...
        // If the user clicked on another VCL control, then a SetFocus
Message may have been sent too.
        // Or, the user may have pressed the 'Return' key to send the
KillFocus message.
        // How Do I check for this?
        // ***********************************************************

    EditControl := Message.LParam;
    SetLength(s,60);
    SLen := SendMessage(EditControl,WM_GETTEXT,60,Integer(pChar(S)));
    SetLength(s,SLen);
    IF SLen = 0 Then Begin

        // ******* How do Get Back to the EditBox from Here??????
        // *********************************************

    End
    Else Begin
      TempElement := TElement(Self.Selected.Data);
      if Assigned(TempElement.Parent) and
          (not TempElement.IsUnique(TempElement.Parent.Elements,S)) then
begin

        // ******* How do Get Back to the EditBox from Here??????
        // ***************************************************8

      End
      Else Begin
        Inherited;
      End;
    End;

I really appreciate any help!!!!
Thank You!
Joe E.

 

Re:Intercepting Windows Messages - Was TTreeView - Not...


Quote
In article <87ieqc$6...@bornews.borland.com>, Joe Enfield wrote:
> In Windows Explorer TreeView, when an invalid character is placed in the
> edit box, and the user kills the focus to the edit box, an error message
> comes up, then returns to the edit box.
> Why can't we do that with Delphi's implementation of the TreeView?

Who says we can't?

Quote

> I've Overridden the WndProc Function of the TreeView.

WAY to much work. The treeview has a OnEdited event, this is called when the
user tries to end the editing operation by hitting enter or moving focus to
another item. In this event you see the new caption for the item and can
reject it by setting the S parameter back to Node.Text. To show a message
and then get then node back into edit mode you have to solve a minor timing
issue. Here is an older message that shows how to do this:

<quote>
You cannot set the node back to
edit mode in the onEdited event. So post a user message to the form
and set the node to edit mode in the handler for this message:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,
  StdCtrls, ComCtrls;

const
  UM_EDITNODE = WM_USER;
type
  TUmEditNode= record
    msg: Cardinal;
    node: TTreenode;
    unused: Longint;
    result: longint;
  end;

  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    TreeView1: TTreeView;
    procedure TreeView1Edited(Sender: TObject; Node: TTreeNode;
      var S: String);
  private
    { Private declarations }
    Procedure UMEditNode( Var msg: TUMeditNode); message UM_EDITNODE;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.TreeView1Edited(Sender: TObject; Node: TTreeNode;
  var S: String);
var
  n: TTreenode;
begin
  n:= (Sender as TTreeview).Items[0];
  while n <> Nil do begin
    if n <> Node then begin
      if AnsiCompareText( n.Text, S ) = 0 then begin
        ShowMessage('Item already exists, use different text');
        S:= node.text;
        PostMessage( handle, UM_EDITNODE, integer(node), 0);
        break;
      end;
    end;
    n:= n.GetNext;
  end;  
end;

procedure TForm1.UMEditNode(var msg: TUMeditNode);
begin
  If assigned( msg.node ) then
    msg.node.edittext;
end;

end.
</quote>

Peter Below (TeamB)  100113.1...@compuserve.com)
No replies in private e-mail, please, unless explicitly requested!

Sent using Virtual Access 5.00 - download your freeware copy now
http://www.atlantic-coast.com/downloads/vasetup.exe

Re:Intercepting Windows Messages - Was TTreeView - Not...


Thank You!!!!
Joe E.

"Peter Below (TeamB)" <100113.1...@compuXXserve.com> wrote in message
news:VA.0000471a.0164284d@antispam.compuserve.com...

Quote
> In article <87ieqc$6...@bornews.borland.com>, Joe Enfield wrote:
> > In Windows Explorer TreeView, when an invalid character is placed in the
> > edit box, and the user kills the focus to the edit box, an error message
> > comes up, then returns to the edit box.
> > Why can't we do that with Delphi's implementation of the TreeView?

> Who says we can't?

> > I've Overridden the WndProc Function of the TreeView.

> WAY to much work. The treeview has a OnEdited event, this is called when
the
> user tries to end the editing operation by hitting enter or moving focus
to
> another item. In this event you see the new caption for the item and can
> reject it by setting the S parameter back to Node.Text. To show a message
> and then get then node back into edit mode you have to solve a minor
timing
> issue. Here is an older message that shows how to do this:

> <quote>
> You cannot set the node back to
> edit mode in the onEdited event. So post a user message to the form
> and set the node to edit mode in the handler for this message:

> unit Unit1;

> interface

> uses
>   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
> Dialogs,
>   StdCtrls, ComCtrls;

> const
>   UM_EDITNODE = WM_USER;
> type
>   TUmEditNode= record
>     msg: Cardinal;
>     node: TTreenode;
>     unused: Longint;
>     result: longint;
>   end;

>   TForm1 = class(TForm)
>     Edit1: TEdit;
>     Edit2: TEdit;
>     TreeView1: TTreeView;
>     procedure TreeView1Edited(Sender: TObject; Node: TTreeNode;
>       var S: String);
>   private
>     { Private declarations }
>     Procedure UMEditNode( Var msg: TUMeditNode); message UM_EDITNODE;
>   public
>     { Public declarations }
>   end;

> var
>   Form1: TForm1;

> implementation

> {$R *.DFM}

> procedure TForm1.TreeView1Edited(Sender: TObject; Node: TTreeNode;
>   var S: String);
> var
>   n: TTreenode;
> begin
>   n:= (Sender as TTreeview).Items[0];
>   while n <> Nil do begin
>     if n <> Node then begin
>       if AnsiCompareText( n.Text, S ) = 0 then begin
>         ShowMessage('Item already exists, use different text');
>         S:= node.text;
>         PostMessage( handle, UM_EDITNODE, integer(node), 0);
>         break;
>       end;
>     end;
>     n:= n.GetNext;
>   end;
> end;

> procedure TForm1.UMEditNode(var msg: TUMeditNode);
> begin
>   If assigned( msg.node ) then
>     msg.node.edittext;
> end;

> end.
> </quote>

> Peter Below (TeamB)  100113.1...@compuserve.com)
> No replies in private e-mail, please, unless explicitly requested!

> Sent using Virtual Access 5.00 - download your freeware copy now
> http://www.atlantic-coast.com/downloads/vasetup.exe

Other Threads