Solved

TRichEdit - problem with last line

Posted on 2009-04-01
6
326 Views
Last Modified: 2012-05-06
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
Comment
Question by:Delphi_developer
  • 3
  • 2
6 Comments
 
LVL 21

Accepted Solution

by:
developmentguru earned 125 total points
Comment Utility
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
 
LVL 14

Expert Comment

by:SteveBay
Comment Utility
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
 
LVL 14

Expert Comment

by:SteveBay
Comment Utility
developmentguru. You beat me by mere seconds. :)
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:Delphi_developer
Comment Utility
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
 
LVL 14

Assisted Solution

by:SteveBay
SteveBay earned 125 total points
Comment Utility
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
 

Author Closing Comment

by:Delphi_developer
Comment Utility
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

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

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 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

16 Experts available now in Live!

Get 1:1 Help Now