Well, I put the code on resized and not resizing and I did it on Windows because of the bug I mentioned above. Also Windows has a bug with line endings on TextAreas, instead of 0D0A they are just 0D, that’s why I use EndOfLine.Macintosh there.
I’m sure you can make a much better code, basically what I did:
- Use InStr(TextArea2.Text, EndOfLine.Macintosh) to find the first char that is EndOfLine
- Check in what row (line) is in the TextArea
- Put EOL on TextArea1 (left) to have blanks
- Have a counter to know when is paragraph 2, 3 and so on
The code is:
[code]TextArea1.Text = “”
// using EndOfLine.Macintosh because the TextAreas on Windows just use 0D and not 0D0A
Dim charPos As Integer = InStr(TextArea2.Text, EndOfLine.Macintosh)
Dim cp As Integer = 1
Dim linea As Integer
Dim ln As Integer = 1
// write 1 in the first row always
TextArea1.AppendText(cp.ToText)
While charPos > 0
// found and EOL, we have another paragraph
cp = cp + 1
// find in which line (row) is the EndOfLine (0 based)
ln = TextArea2.LineNumAtCharPos(charPos) + 1
// I start at linea + 1 to Append an EOL to the number on TextArea1
For j As Integer = linea + 1 To ln
// add EOL
TextArea1.AppendText(EndOfLine.Macintosh)
linea = j
Next
// Append the paragraph number
TextArea1.AppendText(cp.ToText)
// find another EOL
charPos = InStr(charPos+1,TextArea2.Text,EndOfLine.Macintosh)
Wend[/code]
I just changed the code to make it work around the mac bug, it makes sure that is not putting 2 numbers in the same line (at least an EOL between them):
[code]TextArea1.Text = “”
Dim charPos As Integer = InStr(TextArea2.Text, EndOfLine)
Dim cp As Integer = 1
Dim linea,lineaorig As Integer
Dim ln As Integer = 1
Dim ciclo As Integer
TextArea1.AppendText(cp.ToText)
While charPos > 0
cp = cp + 1
ln = TextArea2.LineNumAtCharPos(charPos) + 1
// make sure we don’t append a number to another number, must be at least 1 EOL (mac bug)
If ln = lineaorig Then ln = ln +1
lineaorig = ln
For j As Integer = linea + 1 To ln
TextArea1.AppendText(EndOfLine)
linea = j
Next
TextArea1.AppendText(cp.ToText)
charPos = InStr(charPos+1,TextArea2.Text,EndOfLine)
Wend[/code]
Because Linux start with 1 instead of 0, the code will need to change to make it work. I’ll report the bug tomorrow.
Please be kind, the code works (in my tests), but I don’t have much experience, I bet it can be cleaned/fixed.
Edit: I put the code on a Method, now I call that too from TextArea2 TextChange event. On Linux when I paste the paragraphs the numbers on the left are in wrong position, if I add a return after the paste or resize the window then the numbers are set correctly. This doesn’t happen with Windows or Mac.