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

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...