.net - 从光标位置的文本框中获取文本.net

标签 .net winforms textbox cursor textselection

我需要从winforms中的文本框中获取文本,我需要获取光标所在的文本,例如

Hello or posit|ion or look

这应该返回单词position(注意这里我使用管道作为光标)

你知道我可以使用什么技术吗

最佳答案

我很快就测试了这个,看起来它一直有效

Private Function GetCurrentWord(ByRef txtbox As TextBox) As String
    Dim CurrentPos As Integer = txtbox.SelectionStart
    Dim StartPos As Integer = CurrentPos
    Dim EndPos As Integer = txtbox.Text.ToString.IndexOf(" ", StartPos)

    If EndPos < 0 Then
        EndPos = txtbox.Text.Length
    End If

    If StartPos = txtbox.Text.Length Then
        Return ""
    End If

    StartPos = txtbox.Text.LastIndexOf(" ", CurrentPos)
    If StartPos < 0 Then
        StartPos = 0
    End If

    Return txtbox.Text.Substring(StartPos, EndPos - StartPos).Trim
End Function

关于.net - 从光标位置的文本框中获取文本.net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8465699/

相关文章:

c# - 我如何解决 'Timeout period elapsed prior to obtaining a connection from the pool' ?

c# - foreach 循环 GroupBox 中的 TextBox 以相反的顺序迭代?

winforms - 防止 ToolStripMenuItem 上下文菜单在单击时关闭

jquery - 如何根据在 jquery 文本框中扫描的值在行列表中交换特定行

html - 如何改变输入框的大小?

c# - 将 HttpConfigurationExtensions 添加到 ASP.NET 项目

c# - .Net 混淆器问题

.net - .Net/Visual Studio 是否支持非英语文化的外语关键字?

c# - 在表单上的列表中显示文件名和错误

javascript - 如何在 CSS 中的文本框上放置对 Angular 线?