在vb.net的文本框中查找光标位置

时间:2009-09-10 22:10:45

标签: vb.net textbox

如何找到用户可以键入,列和行的小闪烁光标的位置?我无处可寻。

2 个答案:

答案 0 :(得分:15)

如果您的意思是WinForms,请使用SeletionStart属性访问carret的当前位置。这是获取索引,当前行和当前列的代码。

int index = myTextBox.SelectionStart;
int currentLine = myTextBox.GetLineFromCharIndex(index);
int currentColumn = index - myTextBox.GetFirstCharIndexFromLine(currentLine);

答案 1 :(得分:1)

'
'
'
'Imports 
Public Class frmMain
    '
    '- See more at: http://www.visual-basic-tutorials.com/Tutorials/Strings/count-how-many-times-a-string-occurs-in-visual-basic.htm#sthash.zPPNxNjl.dpuf
    ' But I heavily modidied it to suit My style and needs. VS2012
    '
    'Used by the Sub StripPath(ByRef sPath As String)
    Public PathOnly As String = Nothing 'Global
    Public FileOnly As String = Nothing 'Global
    '
    Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        '
        Label1.Text = Nothing
        '
        Me.Text = "Text Operations"
        '
    End Sub
    '
    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
        '
        MsgBox("The word ''" & tbxSearch.Text & "'' occurs: " & vbCrLf & FindWords(tbxText.Text, tbxSearch.Text) & " time(s).")
        '
    End Sub
    '

    '
    Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenToolStripMenuItem.Click
        '
        Dim RetVal As String = GetOpenFileName()
        '
        If RetVal <> Nothing Then
            StripPath(RetVal)
            ReadAllText(RetVal)
            Me.Text = FileOnly + " - " + "Text Operations"
            Label1.Text = PathOnly
            'MsgBox(PathOnly)
        End If
        '
    End Sub
    '
    Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
        '
        Application.Exit()
        '
    End Sub
    '
    Private Sub WordCountToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles WordCountToolStripMenuItem.Click
        '
        Dim Prompt As String = "Type the word that you wish to search for."
        Dim Title As String = "Word Count"
        Dim DefaultStr As String = "ico"
        Dim RetVal As String = InputBox(Prompt, Title, DefaultStr)
        '
        If RetVal <> Nothing Then
            '
            tbxSearch.Text = RetVal
            '
            MsgBox("The word ''" & tbxSearch.Text & "'' occurs: " & vbCrLf & FindWords(tbxText.Text, tbxSearch.Text) & " time(s).")
            '
        End If
        '
    End Sub
    '
    Private Function FindWords(ByVal TextSearched As String, ByVal Paragraph As String) As Integer
        '
        Dim location As Integer = 0
        '
        Dim occurances As Integer = 0
        '
        Do
            '
            location = TextSearched.IndexOf(Paragraph, location)
            '
            If location <> -1 Then
                '
                occurances += 1
                '
                location += Paragraph.Length
                '
            End If
            '
        Loop Until location = -1
        '
        Return occurances
        '
    End Function
    '
    Private Sub StripPath(ByRef sPath As String)
        '
        'oFileInfo.DirectoryName returns only the path without the "\"
        'oFileInfo.Name returns only the filename.and extension. "some file.???"
        Dim sFile As String = sPath '"c:\mydir\subdir\temp\myfile.txt"
        Dim ioFileInfo As New System.IO.FileInfo(sFile)
        PathOnly = ioFileInfo.DirectoryName 'Global
        FileOnly = ioFileInfo.Name 'Global
        '
    End Sub 'StripPath()
    '
    Private Sub tbxText_Click(sender As Object, e As EventArgs) Handles tbxText.Click
        'Your Code
        ''int index = myTextBox.SelectionStart;
        ''int currentLine = myTextBox.GetLineFromCharIndex(index);
        ''int currentColumn = index - myTextBox.GetFirstCharIndexFromLine(currentLine);
        'End Your Code
        '
        Dim index = tbxText.SelectionStart
        Dim currentLine = tbxText.GetLineFromCharIndex(index)
        Dim currentColumn = index - tbxText.GetFirstCharIndexFromLine(currentLine)
        tsslLine.Text = "Ln: " & currentLine
        tsslCol.Text = "Col: " & currentColumn
        '
    End Sub
    '
    Private Sub tbxText_DoubleClick(sender As Object, e As EventArgs) Handles tbxText.DoubleClick
        '
        tbxSearch.Text = tbxText.SelectedText
        '
    End Sub
    '
End Class