文本框中当前按键位置

4
在winform的文本框中,如何获取当前输入位置?
例如 -
textbox.text = 3056.98
如果在'0'后面键入'7',则当前输入位置为3。
在vb.net中,哪个文本框事件或关键字可以获取此位置值?
2个回答

2
  1. Get the index of the caret in the TextBox:

    C#

    int caretIndex = textBox.SelectionStart;
    

    VB.NET

    Dim caretIndex As Integer = textBox.SelectionStart
    
  2. Get the line number from the caret index:

    C#

    int lineNumber = textBox.GetLineFromCharIndex(caretIndex);
    

    VB.NET

    Dim lineNumber As Integer = textBox.GetLineFromCharIndex(caretIndex)
    
  3. Get the character index in the current line:

    C#

    Point characterXY = textBox.GetPositionFromCharIndex(caretIndex);
    int characterIndex = textBox.GetCharIndexFromPosition(characterXY);
    

    VB.NET

    Dim characterXY As Point = textBox.GetPositionFromCharIndex(caretIndex)
    Dim characterIndex As Integer = textBox.GetCharIndexFromPosition(characterXY)
    

我猜你可以从这里继续...


请参考如何在状态栏上显示TextBox中的行位置?


亲爱的@AkramShahda,这是用于多行文本框吗?谢谢。 - soclose
@soclose:确实如此。但是,您可以使用第一步在单行文本框中获取位置。 - Akram Shahda

2

对于Winforms的TextBox,您可以使用SelectionStart属性获取当前光标位置。

在WPF中,可以使用CaretIndex属性找到相同的内容。

不确定ASP.NET - 我怀疑您无法从服务器端检索此内容。


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