Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Text receiving window...

Posted on 2010-09-22
12
Medium Priority
?
779 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
ID: 33739715
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:
Ephraim Wangoya earned 1000 total points
ID: 33740082
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 1000 total points
ID: 33741702
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:ejla51
ID: 33743373
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
ID: 33743401
So that's why you use SelLength. Also, any new lines should be APPENDED at the end of the component.
0
 

Author Comment

by:ejla51
ID: 33743765
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
ID: 33744082
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
ID: 33744169
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
ID: 33744229
Yep, that seems complete to me :o) Great Team Work !!
0
 

Author Closing Comment

by:ejla51
ID: 33745073
Very nice concept but a little bit open
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
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 synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

972 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