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

7

如何找到用户可以输入的小闪烁光标的位置,包括列和行?我无法在任何地方找到这个信息。


你到底是指什么?WinForms、WebForms、WPF、控制台应用程序还是其他什么? - Vinay Sajip
2个回答

18

如果你是指的WinForms,请使用SeletionStart属性来访问光标当前的位置。以下是获取索引、当前行和当前列的代码。

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

以下代码在我这里运行得更好(VS 2017,VB.NET)。currentColumn = charIndex - myTextBox.GetFirstCharIndexOfCurrentLine() + 1 - CrazyIvan1974

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

我的转换到VS2012..它在- Private Sub tbxText_Click(sender As Object, e As EventArgs) Handles tbxText.Click - ZipFileX

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接