Rich Text Box -- word wrap issue

Reyn

Member
Joined
Dec 11, 2008
Messages
14
Programming Experience
1-3
Hello. I am writing a simple text editor, something kind of like Notepad.

The issue is coming with my display of the line and column position. the current line an column appears at the bottom right in a status strip. in one of the menus I have an option for word wrap which toggles the word wrap property of the rich text box. as long as there is nothing in the rich text box (line 1 column 0) the word wrap seems to toggle fine (not sure because there is no text, but the code should be sound). However, if I have text in the box when i toggle the word wrap i get a ArgumentOutOfRange exception in the line/column code.

I'm not sure what is wrong, but here is the code and the error message box.
VB.NET:
' code to determine line and column number
Private Sub Coord()
        Dim intFirstChar As Integer = rtxtEditor.GetCharIndexFromPosition(New Point(0, 0))
        Dim intFirstLine As Integer = rtxtEditor.GetLineFromCharIndex(intFirstChar)
        Dim intCharCount As Integer = (rtxtEditor.SelectionStart + rtxtEditor.SelectionLength) - intFirstChar
        Dim strFromStart As String = rtxtEditor.Text.Substring(intFirstChar, intCharCount) ************
        Dim intLine As Integer = strFromStart.Split(vbLf).Length
        sts_lbl_LCoord.Text = "Ln: " & intFirstLine + intLine
        sts_lbl_CCoord.Text = "Col: " & (intCharCount - strFromStart.LastIndexOf(vbLf))
    End Sub

' code for word wrap
    Private Sub mnuViewWordWarp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuViewWordWarp.Click
        ' TODO: fix the Coord() code. ArgumentsOutOfRange error when this option is used.
        If mnuViewWordWarp.Checked Then
            rtxtEditor.WordWrap = True
        Else
            rtxtEditor.WordWrap = False
        End If
    End Sub

the Coord() sub activates upon form load once, and then every time the rtxtEditor.SelectionChanged event occurs.

the exception detail is shown in the attachment, and occurs at the ******* in the code.
 

Attachments

  • error.jpg
    error.jpg
    45.4 KB · Views: 35
Thanks for the link. I'll take a look at it.

I still do have one question however; why does that code cause the exception error. if the text is wrapped it still sees it as the same line, and switching the status of wrod wrap shouldn't matter.:confused:



**.:EDIT:.**
Ok, i've taken a look at the code, and it is a lot better than what I had.
 
Last edited:
Back
Top