Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 781
  • Last Modified:

Text receiving window...

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
ejla51
Asked:
ejla51
  • 5
  • 2
  • 2
  • +1
2 Solutions
 
rfwoolfCommented:
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
 
Ephraim WangoyaCommented:
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
 
epasquierCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
ejla51Author Commented:
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
 
rfwoolfCommented:
So that's why you use SelLength. Also, any new lines should be APPENDED at the end of the component.
0
 
ejla51Author Commented:
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
 
ejla51Author Commented:
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
 
ejla51Author Commented:
Sorry typo... row 17 have unnecessary "-8" in testing purpose :(
must be:  
17: SetScrollPos(Memo1.Handle, SB_HORZ,ScrollPosHoriz ,True)
0
 
epasquierCommented:
Yep, that seems complete to me :o) Great Team Work !!
0
 
ejla51Author Commented:
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!

  • 5
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now