Solved

Text receiving window...

Posted on 2010-09-22
12
761 Views
Last Modified: 2013-11-23
I need a customized control such as TMemo, TRichEdit or equivalent ... This is used as a window that receives text from the chat- or teletype socket.
I want to scroll up the earlier received text, may select some words and copy it to the other controls.
The problem comes up when new text is received during the time when I read the text further up ... when new text is received, the focus will be moved to caret position and user made selection loses.
How would you avoid this "focus stealing" problem?
I'm using Delphi 7 & Windows 32 & 64 OS
0
Comment
Question by:ejla51
  • 5
  • 2
  • 2
  • +1
12 Comments
 
LVL 13

Expert Comment

by:rfwoolf
Comment Utility
Hmmm not too sure but you could try the seltext, sellength functions, like this:
1. Remember where the cursor was and what was selected (seltext and sellength).
2. Append the new information at the end of the editor
3. Reset the cursor to what was the seltext and sellength etc
0
 
LVL 32

Accepted Solution

by:
ewangoya earned 250 total points
Comment Utility
I'm using a simple thread to simulate real time data entry. the idead here is to store the caret pos and restore it after insertion, Hope this helps you

unit Unit1;

interface

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

type
  TAddProc = procedure of object;

  TMyThread = class(TThread)
  public
    FAddProc: TAddProc;
    constructor Create(AddProc: TAddProc);
    procedure Execute; override;
  end;

  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    FThread: TMyThread;
    procedure DoAddProc;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


{ TMyThread }

constructor TMyThread.Create(AddProc: TAddProc);
begin
  inherited Create(True);
  FreeOnTerminate := True;
  FAddProc := AddProc;
  Resume;
end;

procedure TMyThread.Execute;
begin
  while not Terminated do
  begin
    Synchronize(FAddProc);
    Sleep(1000)
  end;
end;

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin
  FThread := TMyThread.Create(DoAddProc);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  FThread.Terminate;
end;

procedure TForm1.DoAddProc;
var
  P: TPoint;
begin
  Memo1.Lines.BeginUpdate;
  P := Memo1.CaretPos;
  Memo1.Lines.Add(TimeToStr(Now));
  Memo1.CaretPos := P;
  Memo1.Lines.EndUpdate;
end;

end.
0
 
LVL 25

Assisted Solution

by:epasquier
epasquier earned 250 total points
Comment Utility
You would have also to get the scroll position, otherwise it might be annoying to see the window moving
Use the GetScrollPos & SetScrollPos windows functions
procedure TForm1.DoAddProc;

var

  P: TPoint;

  SelectLen, ScrollPosVert , ScrollPosHoriz :Integer;

begin

  Memo1.Lines.BeginUpdate;

  P := Memo1.CaretPos;

  SelectLen:=Memo1.SelLength;

  ScrollPosVert :=GetScrollPos(Memo1.Handle, SB_VERT);

  ScrollPosHoriz :=GetScrollPos(Memo1.Handle, SB_HORZ);

  Memo1.Lines.Add(TimeToStr(Now));

  Memo1.CaretPos := P;

  Memo1.SelLength:=SelectLen;

  SetScrollPos(Memo1.Handle, SB_VERT,ScrollPosVert,True);

  SetScrollPos(Memo1.Handle, SB_HORZ,ScrollPosHoriz ,True);

  Memo1.Lines.EndUpdate;

end;

Open in new window

0
 

Author Comment

by:ejla51
Comment Utility
thanks for all the good comments, but I just found the remains undesirable features.
ewangoya: Caret position is OK, but the text selection will disappear when the new line has been added.
epsquier:  selection does not retain its original position when the new line added in the Memo1.
unit RxEditor;



interface



uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls;



type

  TAddProc = procedure of object;



  TMyThread = class(TThread)

  public

    FAddProc: TAddProc;

    constructor Create(AddProc: TAddProc);

    procedure Execute; override;

  end;



  TForm1 = class(TForm)

    Memo1: TMemo;

    Button1: TButton;

    Button2: TButton;

    procedure Button1Click(Sender: TObject);

    procedure Button2Click(Sender: TObject);

  private

    FThread: TMyThread;

    procedure DoAddProc;

  public

    { Public declarations }

  end;



var

  Form1: TForm1;



implementation



{$R *.dfm}



{ TMyThread }

// -----------------------------------------------------------------------------

constructor TMyThread.Create(AddProc: TAddProc);

begin

  inherited Create(True);

  FreeOnTerminate := True;

  FAddProc := AddProc;

  Resume;

end;

// -----------------------------------------------------------------------------

procedure TMyThread.Execute;

begin

  while not Terminated do

  begin

    Synchronize(FAddProc);

    Sleep(1000)

  end;

end;



{ TForm1 }

// -----------------------------------------------------------------------------

procedure TForm1.Button1Click(Sender: TObject);

begin

  FThread := TMyThread.Create(DoAddProc);

end;

// -----------------------------------------------------------------------------

procedure TForm1.Button2Click(Sender: TObject);

begin

  FThread.Terminate;

end;

// -----------------------------------------------------------------------------

procedure TForm1.DoAddProc;

var

  P: TPoint;

  SelectLen, ScrollPosVert , ScrollPosHoriz :Integer;

begin

  Memo1.Lines.BeginUpdate;

  P := Memo1.CaretPos;

  SelectLen:=Memo1.SelLength;

  ScrollPosVert :=GetScrollPos(Memo1.Handle, SB_VERT);

  ScrollPosHoriz :=GetScrollPos(Memo1.Handle, SB_HORZ);

  Memo1.Lines.Add(TimeToStr(Now));

  Memo1.CaretPos := P;

  Memo1.SelLength:=SelectLen;

  SetScrollPos(Memo1.Handle, SB_VERT,ScrollPosVert,True);

  SetScrollPos(Memo1.Handle, SB_HORZ,ScrollPosHoriz ,True);

  Memo1.Lines.EndUpdate;

end;



// -----------------------------------------------------------------------------

end.

Open in new window

0
 
LVL 13

Expert Comment

by:rfwoolf
Comment Utility
So that's why you use SelLength. Also, any new lines should be APPENDED at the end of the component.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:ejla51
Comment Utility
In reality, the text will enter one character at a time - as the chat terminal window ...
New series are fed only if received character is a carriage return chr (13).
0
 

Author Comment

by:ejla51
Comment Utility
Added SelectStart variable to store start position and now the selection seems to "stay tuned".
The first step of my "OnRxChar" procedure is now completed.
I think it's fair play to share the points. I'm waiting for a while anyway for any possible comments.


  procedure TForm1.DoAddProc;

var

  P: TPoint;

  SelectStart, SelectLen, ScrollPosVert , ScrollPosHoriz :Integer;

begin

  Memo1.Lines.BeginUpdate;

  P := Memo1.CaretPos;

  SelectLen:=Memo1.SelLength;

  SelectStart := Memo1.SelStart;

  ScrollPosVert :=GetScrollPos(Memo1.Handle, SB_VERT);

  ScrollPosHoriz :=GetScrollPos(Memo1.Handle, SB_HORZ);

  Memo1.Lines.Add(TimeToStr(Now));

  Memo1.CaretPos := P;

  Memo1.SelStart := SelectStart;

  Memo1.SelLength:=SelectLen;

  SetScrollPos(Memo1.Handle, SB_VERT,ScrollPosVert,True);

  SetScrollPos(Memo1.Handle, SB_HORZ,ScrollPosHoriz-8 ,True);

  Memo1.Lines.EndUpdate;

end;

Open in new window

0
 

Author Comment

by:ejla51
Comment Utility
Sorry typo... row 17 have unnecessary "-8" in testing purpose :(
must be:  
17: SetScrollPos(Memo1.Handle, SB_HORZ,ScrollPosHoriz ,True)
0
 
LVL 25

Expert Comment

by:epasquier
Comment Utility
Yep, that seems complete to me :o) Great Team Work !!
0
 

Author Closing Comment

by:ejla51
Comment Utility
Very nice concept but a little bit open
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now