【问题标题】:How can i get number of clicked line in TextBox?如何获取 TextBox 中的点击行数?
【发布时间】:2019-02-09 19:30:45
【问题描述】:

我有启用 MultiLine 的 TextBox。 如何获取 TextBox.Click 事件中的点击行数?

【问题讨论】:

    标签: .net vb.net winforms


    【解决方案1】:

    TextBoxBase 类(TextBox 派生自该类)具有许多可以帮助您完成此任务的方法。

    假设您为 TextBox1 控件的 MouseDown 事件提供了此事件处理程序

    Sub TextBox1_MouseDown(sender as Object, e as MouseEventArgs) Handles TextBox1.MouseDown
    
        '  In the MouseEventArgs we have the click location
        '  We can use that point to get the CharIndex from the TextBox
        Dim charIndex as Integer = TextBox1.GetCharIndexFromPosition(e.Location)
    
        ' Now the charIndex could be passed to another method to get the line index
        Dim line As Integer = TextBox1.GetLineFromCharIndex(charIndex)
    
        ' Lines start from 0
        MessageBox.Show("Click on line=" + line + ", Text=" + TextBox1.Lines(line))
    End Sub
    

    【讨论】:

      【解决方案2】:

      一种解决方案是通过文本框的行高来确定它,例如:

      Dim LineHeight = 16 ' you may change this value
      
      Private Sub richTextBox1_MouseClick(sender As Object, e As MouseEventArgs)
      
        'the number of the selected line
        Dim i = e.Location.Y/LineHeight
      
        'get the value of the number line.
        MessageBox.Show(Me.richTextBox1.Lines(i))
      End Sub
      

      【讨论】:

        【解决方案3】:

        Steve 有一个很好的答案,教会了我一些东西,但由于他使用的是 MouseDown 而不是 Click,我想我可以提供另一种选择,并稍微扩大两者之间的区别。

        我会向你展示我的代码,以防万一你想要,但如果你想要更多信息,那么你可以继续阅读。

        代码(按要求使用 Click):

        Private Sub mainTextBox_Click(sender As Object, e As EventArgs) Handles mainTextBox.Click
            Dim line As Integer = mainTextBox.GetLineFromCharIndex(mainTextBox.SelectionStart)
        End Sub
        

        这会获取选择光标的位置,它会移动到您单击的位置,而不是鼠标位置。由于此单击事件在 TextBox 本身上,因此光标每次都应该移动,因此它应该相对相当于史蒂夫使用鼠标位置的答案,但我还没有彻底测试它以确保所有边缘情况(是的,选择光标在点击事件被调用之前移动,我已经测试了很多)。

        哦,别忘了它给出了行的索引,所以第一行实际上将返回 0,第二行返回 1,等等。


        额外的位:

        自从 Steve 使用 MouseDown 以来,我想我会提到它与 Click/MouseClick 的不同之处,以防您(或某些未来的读者)不知道。按下鼠标按钮时立即调用 MouseDown,释放时调用 MouseUp,然后调用 Click/MouseClick。如果确切的时间对您的使用无关紧要,那么您应该可以使用任何您想要的,如果它确实很重要,那么那里有一些很好的答案可以更详细地解释差异(这里有一个解释 @987654321 @例如)。

        您可以在此处查看如何使用 MouseClick 和 MouseDown 执行相同的技巧:

        Private Sub mainTextBox_MouseClick(sender As Object, e As MouseEventArgs) Handles mainTextBox.MouseClick
            Dim line As Integer = mainTextBox.GetLineFromCharIndex(mainTextBox.SelectionStart)
        End Sub
        
        Private Sub mainTextBox_MouseDown(sender As Object, e As MouseEventArgs) Handles mainTextBox.MouseDown
            Dim line As Integer = mainTextBox.GetLineFromCharIndex(mainTextBox.SelectionStart)
        End Sub
        

        我能够进行快速测试,因为我目前正在做的事情中有一个 TextBox,但我还没有对 Steve 的方法或我自己的方法进行广泛的测试,并且不能说一个肯定比另一个更好.您可能想同时尝试两种方法,看看哪种方法最适合您。

        【讨论】:

        • 哇。我并没有打算写那么多,但是当我开始的时候,我意识到我也可以学习一些新东西,并开始测试一些东西并且有点忘乎所以。 :)(阅读:如果有人觉得他们可以在不改变基本答案的情况下将其精简为更简洁的内容,那就去吧)
        • if someone wants to change this C# in to VB I would be most grateful 如你所愿 :-) 这是一个非常简单的转换——最大的不同是使用 Handles 关键字连接事件处理程序。
        • @InteXX 谢谢!我从字面上从来没有写过一行VB,所以我不确定它涉及到什么。看着史蒂夫的回答,我正在考虑自己尝试转换并查看您将其更改为的内容,我想我会非常接近!我想我唯一会错过的是 ByVal。无论如何,再次感谢。我想你可以说我今天学了一些 VB。 :-)
        • ByVal 实际上,那是我的“哎呀”。 ByVal 是默认推断的,不再需要。 Telerik's converter 自动插入,忘记取出,仅此而已。 I guess you could say I learned some VB today好人:-)
        • 不错的电动车你已经到了那里:-)
        【解决方案4】:

        对于一个名为 tbData 的文本框(行从零开始,因此需要加 1):

        Private Sub tbData_MouseClick(ByVal sender As System.Object, ByVal e As MouseEventArgs) Handles tbData.MouseClick
            Dim msg As String
            Dim lineClicked As Int32
            lineClicked = tbData.GetLineFromCharIndex(tbData.GetCharIndexFromPosition(e.Location)) + 1
            msg = "clicked line " + lineClicked.ToString()
            MsgBox(msg)
        End Sub
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-03-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-09-12
          相关资源
          最近更新 更多