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

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

TRichEdit - problem with last line

Hi

I have an annoying issue with TRichEdit. I'm not sure how to solve it.

I have a TRichEdit control and the size is h:60 w:240. this should be enough for 4 lines of text.

I execute:

RE.Lines.Add('1');
RE.Lines.Add('2222 2222 222 222 2 2222 222222 2222 2222 222222 22222222 222 2222');
RE.Lines.Add('4');

this means it should extend '222...' into 2 lines, which it does correctly, due to width of control.
The problem is that after inserting '4' into 4th line, it creates NEW LINE! and now it moves all lines up so the first line is not visible anymore! Obviously with new line, there are now 5 lines.

So, instead of :

1
22222 2.2.2....
2222...
4

I see:

2222 22.2...
2222...
4
(and new empty line)

Because of new empty line the first line gest pushed up (out of the visible area).

I can move up I can get the first line to appear in control, but I would like to have it visible from the beginning.

So, it appears that:

RE.Lines.Add('...');

ads new line after the inserted line.

How can I avoid this?

Thank you
0
Delphi_developer
Asked:
Delphi_developer
  • 3
  • 2
2 Solutions
 
developmentguruCommented:
I ran your example in Delphi 2007 on windows XP and it worked fine.  I did clear it before adding the first line if that makes a difference.  I tried just setting one button to always add a line and never could get it to scroll.  One thing you could try is adding the following line to your code after the line that adds to the Rich Edit.

SendMessage(WM_SETSCROLLPOS, RichEdit1.Handle, 0, 0);

This should tell the control to scroll to position 0 (top).

Let me know if it helps.

P.S.  if you can't find the constant for WM_SETSCROLLPOS I found a reference that stets the value to WM_USER + 222.
0
 
SteveBayCommented:
You could scroll the Richedit in your program with something like this:
RE.perform(WM_VSCROLL,MakeWParam(SB_THUMBPOSITION,0)  ,0);

Open in new window

0
 
SteveBayCommented:
developmentguru. You beat me by mere seconds. :)
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.

 
Delphi_developerAuthor Commented:
Thank you, that works.

But, why is this happening? I'm attaching a screenshot of example in completely new project, and you can see that it creates new line and puts the cursor in it, pushing line with '1' up.

I would rather fix why is this happening than using your suggestions, if possible.

Thanx.
richedit-issue.jpg
0
 
SteveBayCommented:
This behavior is caused by the Richedit trying to put the cursor in view. Richedit only have a cursor when it has focus. If remove the focus from the Richedit while populating the lines you will avoid this issue.

     Button1.SetFocus;
     RichEdit1.Clear;
     RichEdit1.Lines.Add('1');
     RichEdit1.Lines.Add('22222 2222222222222222222222222222222.2.2....');
     RichEdit1.Lines.Add('4');
     RichEdit1.SetFocus;

Open in new window

0
 
Delphi_developerAuthor Commented:
Thank you, both. I awarded both suggestions, because I might use one of the other, depending if I will ever need to focus the RichEdit or not and focus another control to avoid this issue.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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