Solved

TRichEdit - problem with last line

Posted on 2009-04-01
6
330 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
[X]
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
  • 3
  • 2
6 Comments
 
LVL 21

Accepted Solution

by:
developmentguru earned 125 total points
ID: 24041610
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
ID: 24041611
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
ID: 24041623
developmentguru. You beat me by mere seconds. :)
0
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!

 

Author Comment

by:Delphi_developer
ID: 24043025
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
ID: 24043677
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
ID: 31565412
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

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

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…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

734 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