Chasing a quick answer for TMemo ??

Hi,
   Just a quick question about TMemo components and how I can align text in the memo. Right I will start at the beginning....

If I have a long line of text that I want to add to a TMemo and I have word wrap on, how can I format the memo so that the second line is indented a set amount of characters. Okay that sounds easy doesn't it. But there is more. If I have for example the following set of characters at the start of the line with no preceding spaces and 2 spaces after the 1000

eg  500 - 1000+'  '+'The text starts here'
                   
The second line of text needs to start underneath 'The on the first line. I have tried using #161 for the length of the initial string but it ends up being about 5 characters short. There is obviously some width difference between a space and a normal character so what is the answer ??
I have also tried adding to the second line a string such as '          ' to whatever the length of 500 - 1000+2spaces is, but it always is too short. Anyone have a clue on this one??


Need a quick answer so will offer some good points.

Regards,

 WESTY :)
westy100697Asked:
Who is Participating?
 
ITugayConnect With a Mentor Commented:
This is working sample you can try. Sample show text formatting you need.


procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
   with Richedit1 do
   begin
      Lines.Clear;
      Paragraph.TabCount:=2; // number of tabs positions
      Paragraph.Tab[0]:=70;    // first position (in pixels)
      Paragraph.Tab[1]:=200;  // second position
      Lines.Add('0-500'+#9+'first string'+#9+'1');
      Lines.Add(#9+'comment for first string');
      Lines.Add('1000-2000'+#9+'second string'+#9+'2');
      Lines.Add(#9+'comment for second string');
   end;
end;

------
Cheers,
Igor.
0
 
GurkanCommented:
Hi,
If it is not a big problem, you can use a fixed size font, and try again.
0
 
ITugayCommented:
You may try use TRichEdit instead TMemo, it has more possibilities for text formatting.
-----
Igor
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.

 
westy100697Author Commented:
Will try this idea and see how it goes.

WESTY :|
0
 
rwilson032697Commented:
Listening
0
 
jeurkCommented:
Maybe you could use tabs instead of space
and set the wanttabs property to true...
add the car #9 instead to your text...
0
 
westy100697Author Commented:
The ideas here are fine so far except for one thing. I need the place where the first tab is put to be more dynamic as the first part of the line will not always be the same width. The best way I can think of doing this is to find the width of the text in pixels rather than characters. Anyone know of a function to do this ? I will also keep looking. ITugay said some good stuff there but just hard coding the Tabs is not what I need. I need to make it determinable based on the width of that first piece of text plus 2 spaces. This means it could be '0 - 50' or '1500 - 10000'. As you can see the widths of these 2 are different and just finding the string length of these and formatting the next lines start with that many blanks will not work. Pixel widths will but how do I get them other than using OnMouseMove which is definitely not ideal.

WESTY :)
0
 
westy100697Author Commented:
Also it doesn't really matter if I use a RichEdit or Memo as either will work fine for this project I am working on. Need ideas though ASAP on how to format that text.

Cheers,

 WESTY :)
0
 
westy100697Author Commented:
Canvas.Textwidth is the sort of routine I am after except it needs to be for a String or PChar.

WESTY :)
0
 
westy100697Author Commented:
Okay I have come up with a way to get the Pixel Width but does anyone have a better suggestion. Currently I just added the text to a Hidden Comboboxes' canvas and used the TextWidth property to get the width in pixels based on current font size and type. This works okay but is there a better way ??

By the way I am using a RichEdit or Memo so the data entered can be saved to a Memofield in a database table.

WESTY :)
0
 
westy100697Author Commented:
Okay I have solved it. I used a hidded TImage and then used TextOut based on the RichEdit Font Size. This gives me the Pixel width of the text and then I set Tab[0] to that width. Works Well.
Thanks people. Will award ITugay the points later today unless someone can come up with a better solution. The TRichEdit is definitely better than TMemo with more options for formatting.
Well I will check this again this evening and if no-one has got a better solution ITugay gets the points.

PS.(It is currently 9:00am here so plenty of time to see if anyone can better the solution)

Thanks,

 WESTY :)
0
 
yk030299Commented:
listening
0
 
LischkeCommented:
Hi westy,

I don't know what you consider a better solution, but for the text size determination I'd recommend this way:

var
  DC: HDC;
  S: String;
  SIze: TSize;

begin
  DC := GetDC(RichEdit1.Handle);
  S := TextToGetWidthFor;
  GetTextExtentPoint32(DC, PChar(S), Length(S), Size);
  RichEdit.Paragraph.Tab[0]:= Size.cx;
  ReleaseDC(RichEdit1.Handle, DC);
end;

Ciao, Mike
0
 
ITugayCommented:
Look at TStringGrid too. It may be good solution to show text in cells.
----
Igor.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.