Click here to Skip to main content
12,999,744 members (42,180 online)
Rate this:
Please Sign up or sign in to vote.
See more:

I am working on a customized editor pad in where I need to wrap line after particular number of characters.

This is done but now my problem is that when I remove a character in the middle of an already wrapped line it is not adjusting the line as a default editor should behave.
I wrote my algorithm for this purpose but I need some code to help solve my problem.

I need to find the previous/next carriage return position from current cursor position in the RichTextBox.

Any help would be appreciated.

Posted 1-Jun-11 20:00pm
Updated 2-Jun-11 14:13pm
Dalek Dave433.3K
SAKryukov 2-Jun-11 2:01am
Tag it: Forms, WPF, what?!
R. S. Verma 2-Jun-11 8:10am
In Form, am developing editor in using forms
and condition is when i type in rich text box, it should be terminited every after 45 character.
this part is done , now my requirement is when i remove/ add character in between above line then it should also be get terminated/ add next line in the current line, means not any line should go beyond 45 character length.
Dalek Dave 2-Jun-11 20:13pm
Edited for Readability.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

If I understand correctly, you have it working to automatically put in carriage returns while a user is typing but if they go back and edit a line it doesn't work. Well, how is it supposed to work? Is it supposed to redo analyze the entire textbox? Or just add another carriage return on the current line?

Have you tried the RichTextBox.Find[^]?

If you've tried that but isn't working, maybe you could post a snippet of code to better illustrate what you are trying to do.
R. S. Verma 3-Jun-11 1:45am
ya sure i would like to share the code...

Private Sub rtbDoc_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles rtbDoc.KeyPress
'Author : Ravi

If LineCharCounter >= 45 And e.KeyChar <> Chr(8) Then
LastSpacePos = InStrRev(rtbDoc.Text, " ", -1) ' find last space position
LastTabPos = InStrRev(rtbDoc.Text, Chr(9), -1) ' find last tab position
rtbDoc.WordWrap = True
If LastSpacePos > LastTabPos Then
rtbDoc.Text = rtbDoc.Text.Insert(LastSpacePos, Chr(13) & Chr(10))
rtbDoc.Text = rtbDoc.Text.Insert(LastTabPos, Chr(13) & Chr(10))
End If
'getting last char when this condition gets satisfied
lastChar = e.KeyChar.ToString()
totLen = rtbDoc.Text.Length
rtbDoc.Text = rtbDoc.Text.Insert(totLen, lastChar.ToString())
chkGarbage = True ' this line use less
LineCounter = LineCounter + 1 ' count lines manually
LineCharCounter = totLen - LastSpacePos ' getting exact character at each new line
ElseIf e.KeyChar = Chr(13) Then ' return key handling
LineCharCounter = 0
LineCounter = LineCounter + 1
ElseIf e.KeyChar = Chr(8) Then ' backspace handling
If LineCharCounter > 0 And LineCounter > 0 Then
LineCharCounter = LineCharCounter - 1
ElseIf LineCounter > 1 Then
LineCharCounter = 45
LineCounter = LineCounter - 1
End If
' Handling Backspace here
'MessageBox.Show("Current Cursur :" & caretPosition & " Next Returnkey Pos:" & BackSpace(caretPosition), "String Caret Position")
ElseIf e.KeyChar = Chr(9) Then ' Tab key- Tab key is not passed to controls by default as it is needed to provide navigation in a form.
LineCharCounter = LineCharCounter + 8 ' Mark AcceptTab key as True in property window
LineCharCounter = LineCharCounter + 1

End If

End Sub


Private Sub rtbDoc_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles rtbDoc.KeyUp
' Author : Ravi
If chkGarbage = True Then
rtbDoc.Text = rtbDoc.Text.Remove(0, 1)
chkGarbage = False
'System.Windows.Forms.SendKeys.Send("{PGDN}")' no use now
'These two line always keep caret at the End of File
rtbDoc.SelectionStart = Len(rtbDoc.Text)
rtbDoc.SelectionLength = 0
End If
End Sub
[End Code]

Now here i terminate the line on keypress and when i go back from previous line it is not maintain 45 character limit...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170624.1 | Last Updated 2 Jun 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100