Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
3.20/5 (5 votes)
See more:
Hi,

I am working on a customized editor pad in vb.net 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.

Thanks
Posted
Updated 2-Jun-11 14:13pm
v3
Comments
Sergey Alexandrovich Kryukov 2-Jun-11 2:01am    
Tag it: Forms, WPF, what?!
--SA
R. S. Verma 2-Jun-11 8:10am    
In Form, am developing editor in vb.net 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.
thanks...
Dalek Dave 2-Jun-11 20:13pm    
Edited for Readability.

1 solution

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.
 
Share this answer
 
Comments
R. S. Verma 3-Jun-11 1:45am    
ya sure i would like to share the code...

[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))
Else
rtbDoc.Text = rtbDoc.Text.Insert(LastTabPos, Chr(13) & Chr(10))
End If
System.Windows.Forms.SendKeys.Send("{PGDN}")
'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
Else
LineCharCounter = LineCharCounter + 1

End If



End Sub


[Code]

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...
thnkx..

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900