Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Text receiving window...

Posted on 2010-09-22
12
773 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:
ewangoya earned 250 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 250 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

856 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