终止带有分号的字符串并添加空间VB.Net

时间:2013-04-09 20:37:13

标签: vb.net textbox ascii vb.net-2010

我有一个RichTextBox,用于收集数字数组,但数字必须以分号结束。我想在输入分号后立即自动创建一个空格并继续输入其他数字。我已经尝试过这段代码,但它没有创建空格,而是将光标放在字符串的开头:

Private Sub RichTextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles RichTextBox1.KeyPress
    If (Microsoft.VisualBasic.Asc(e.KeyChar) < 48) _
          Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 57) Then
        e.Handled = True
    End If
    If (Microsoft.VisualBasic.Asc(e.KeyChar) = 8) Then
        e.Handled = False
    End If
    If (Microsoft.VisualBasic.Asc(e.KeyChar) = 59) Then
        e.Handled = False
    End If
    If (Microsoft.VisualBasic.Asc(e.KeyChar) = 45) Then
        e.Handled = False
    End If
    If (Microsoft.VisualBasic.Asc(e.KeyChar) = 59) Then
        RichTextBox1.Text += " "
        RichTextBox1.SelectionStart = RichTextBox1.TextLength
        RichTextBox1.SelectionLength = 0
    End If
End Sub

帮助我陷入困境,尝试在互联网上搜索但是没有结果

1 个答案:

答案 0 :(得分:0)

查看SelectionStart和SelectionLength属性:

http://msdn.microsoft.com/en-us/library/system.windows.forms.textboxbase.selectionstart.aspx

If (Microsoft.VisualBasic.Asc(e.KeyChar) = 59) Then
    RichTextBox1.Text += " "
    RichTextBox1.SelectionStart = SelectionStart.TextLength
    RichTextBox1.SelectionLength = 0
End If