C#文本选中及ContextMenuStrip菜单使用
'文本框选中显示
'TextBox1.SelectAll()选择所有文本
'textBox1.Text.Insert(start,strInsertText)指定位置添加文本
1 Private Sub TextBox1_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseClick 2 Dim start As Integer = TextBox1.SelectionStart '光标开始位置索引 3 Dim SelectValue As String = TextBox1.SelectedText '选中文本的值 4 Dim length As Integer = TextBox1.SelectedText.Trim.Length '选中文本的长度 5 If length > 0 Then 6 TextBox1.Select(start, length) '选中文本从光标到指定长度 7 End If 8 End Sub
'选中文本后右键弹出菜单栏
1 Private Sub TextBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown 2 3 If TextBox1.SelectedText.Trim.Length > 0 Then 4 If e.Button = Windows.Forms.MouseButtons.Right Then 5 TextBox1.Enabled = False 6 TextBox1.ContextMenuStrip = ContextMenuStrip1 7 TextBox1.Enabled = True 8 End If 9 End If 10 End Sub
好的代码就和美食一样,都是需要时间烹饪出来的!