0

我有一个 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

求助 我被卡住了,已经尝试在互联网上搜索但没有结果

4

1 回答 1

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
于 2013-04-09T20:42:07.940 回答