在 vb.net 的文本框中查找光标位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1408005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Finding cursor location in a textbox in vb.net
提问by Cyclone
How do you find the location of the little blinking cursor where the user can type, column and row? I couldn't find this anywhere.
您如何找到用户可以键入的小闪烁光标的位置,列和行?我在任何地方都找不到这个。
回答by Patrik Svensson
If you mean WinForms, use the SeletionStart
property to access the current position of the carret. Here is code to get the index, current line and current column.
如果您指的是 WinForms,请使用该SeletionStart
属性访问插入符号的当前位置。这是获取索引、当前行和当前列的代码。
int index = myTextBox.SelectionStart;
int currentLine = myTextBox.GetLineFromCharIndex(index);
int currentColumn = index - myTextBox.GetFirstCharIndexFromLine(currentLine);
回答by ZipFileX
'
'
'
'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