TextArea to DrawInto graphics object issue

I would like to draw a textarea directly into a canvas … and I can. But I want to split the text in half in order to draw two columns of text … and I can. But the second column does not begin with the correct character. If I manually move the splitting point back (derived from getting the linenumber and then charater position), I can get it to work but not programmatically.

Working on Windows 10 Pro. Here’s my code:

  dim i, LineNumber, Halfway as integer
  dim txtSize as integer = 10
  
  TextArea1.FontSize = txtSize
  LineNumber = TextArea1.LineNumber(99999) ' set to last line number in TextArea1
  Halfway = Ceiling((LineNumber / 2)) + 1 ' find midpoint
  i = TextArea1.CharacterPosition(Halfway-1) ' find charpos of the first pos on line halfway
  
  TextArea1.Height = Halfway * (txtSize + 2) ' reset TextArea1 height to only half of text
  TextArea1.DrawInto(g, 30, 300) ' Draw that half onto screen
  
  TextArea1.text = TextArea1.text.Middle(i - 25) ' delete first half of text
  TextArea1.DrawInto(g, 300, 300) ' draw latter half of text over a column

I’ve narrowed the problem to the .LineNumber method of a textarea and the .CharacterPosition of the same. The results are not correct. I found a bug for LineNumber addressed but it only mentioned the count is wrong if the last line is 2 characters or less in length.

I’m consistently getting LineNumber counts that are less than what the textarea actually displays.

Anyone encountered this?

can you use TextShape for painting if your textarea is maybe only plaintext (non RTF)?

I found those functions to be more reliable when the string assigned to the TextArea was not UTF-8.

Before assigning the string to the control, try converting it to UTF-16 on macOS or UTF-32 on MS-Windows.

If the user is typing into the TextArea you could possibly copy the re-encoded text into another (hidden) TextArea.

I’m still unclear as to why you’re using DrawInto for this. Wouldn’t it be easier just to use DrawString and specify a width?

@GregO
DrawString (now DrawText) is great but I don’t know how to make two columns of it from one string. Is there a way to measure the height of a DrawString result that I could then split in two?

@Kevin g

Before assigning the string to the control, try converting it to UTF-16 on macOS or UTF-32 on MS-Windows … If the user is typing into the TextArea you could possibly copy the re-encoded text into another (hidden) TextArea.

This interests me. I will dig into it a bit. There’s no doubt, in my mind at least, that the LineNumber function isn’t accurate as is.

@Marcus R

can you use TextShape for painting if your textarea is maybe only plaintext (non RTF)?

No wordwrap in TextShape.

I’ve written my own wordwrap methods which work but they are cumbersome to employ in various settings. DrawString/DrawText does the work but I can’t determine how long a ribbon of text it will create. TextArea also does wordwrap automatically and supposedly has a mechanism for this: LineNumber. But it fails, at least as far as I’ve been able to tell.

@Kevin
I tried the convert encoding on the text to no avail. Well, maybe to some avail as it changed the result of the LineNumber/CharacterPosition results … it just didn’t change them correctly.