VB.net关于Textbox文本控件全选的小技巧

前端之家收集整理的这篇文章主要介绍了VB.net关于Textbox文本控件全选的小技巧前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

#Region "搜索框的全选"

Dim counter As Integer = 0 '点击次数

Dim isSelectAll As Boolean = False '是否全选

Dim selectPoint As Integer = 0 '当前光标位置

Private Sub TextBox1_Click(ByVal sender As Object,ByVal e As System.EventArgs) Handles TextBox1.Click

counter += 1

If counter = 1 Then '判断是否首次点击

selectPoint = TextBox1.SelectionStart

TextBox1.SelectAll()

isSelectAll = True

ElseIf counter > 1 Then

If TextBox1.SelectionLength = 0 Then '判断选择字符的长度是否为0

If TextBox1.SelectionStart = selectPoint Then '判断是否点击上次同一位置

If isSelectAll = True Then

isSelectAll = False

Else

TextBox1.SelectAll()

isSelectAll = True

End If

Else

If TextBox1.SelectionStart = TextBox1.TextLength AndAlso isSelectAll = False Then '判断光标位置在最末

TextBox1.SelectAll()

isSelectAll = True

Else

selectPoint = TextBox1.SelectionStart '不是上次同一位置,则记录当前位置

isSelectAll = False

End If

End If

End If

End If

End Sub

Private Sub TextBox1_LostFocus(ByVal sender As Object,ByVal e As System.EventArgs) Handles TextBox1.LostFocus

counter = 0

End Sub

Private Sub TextBox1_DoubleClick(ByVal sender As Object,ByVal e As System.EventArgs) Handles TextBox1.DoubleClick

isSelectAll = True

End Sub

#End Region

猜你在找的VB相关文章