Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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 1-Jun-11 21:00pm
Edited 2-Jun-11 15:13pm
v3
Comments
SAKryukov at 2-Jun-11 2:01am
   
Tag it: Forms, WPF, what?!
--SA
R. S. Verma at 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 at 2-Jun-11 20:13pm
   
Edited for Readability.

1 solution

Rate this: bad
good
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.
  Permalink  
Comments
R. S. Verma at 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)



Advertise | Privacy | Mobile
Web01 | 2.8.150326.1 | Last Updated 2 Jun 2011
Copyright © CodeProject, 1999-2015
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