Displaying the current location (Line, Col) of a TextBox in a StatusBar

Any ideas?
[11 byte] By [ai2003bc] at [2007-12-16]
# 1
This generally works, unless you click past the last character, then the program assumes you are on the next line...

However, i'll leave it to you to tweak the rest, a simple loop to add vbnewline at end of sentence and remove from the start should fix it.

2 boxes on the form. textbox1, textbox2. both have multiline set = true



Private Sub TextBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Click
Dim mousepos As Long = TextBox1.SelectionStart
Dim currentpos As Long = 0
Dim str As String() = TextBox1.Text.Split(New Char() {vbNewLine})
For i As Integer = 0 To str.GetUpperBound(0)
If (str(i).Length + currentpos) > mousepos Then
TextBox2.Text = "Row:" & i + 1 & " Clm:" & str(i).Length - (str(i).Length + currentpos - mousepos)
Exit Sub
Else
currentpos += str(i).Length
End If
Next
End Sub

Dustin_H at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2
The TextBox class already has a Lines property so the call to Split is superfluous.

Private Function GetCursorPosition(ByVal target As TextBox) As Point
Dim column As Integer = target.SelectionStart
Dim row As Integer = 0
While column > target.Lines(row).Length
'Remove the length of the current line and account for carriage return and line feed.
column -= target.Lines(row).Length + 2
'Advance to next line.
row += 1
End While
'Return a Point object that represents (column, row).
Return New Point(column, row)
End Function


jmcilhinney at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3
That's a much more effiecient way of doing it Big Smile Good job!
Dustin_H at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 4
mmmmmm. maybe something i forgot to add. im a vb5 user. :)
ai2003bc at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 5
If you read the description of this forum on the main page you'll see that it is aimed at VB.NET 2005 users. There may be people here with knowledge of older versions but, unless you state otherwise, people will assume you are using the the latest version of the .NET Framework. There are other forums on the Web that are aimed at VB6 and earlier. I'd suggest you look at some of those. Try here and here.
jmcilhinney at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...