1
Public Class Form1
Private Sub Me_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    If e.KeyCode = 49 Then '1st tone, flat
        If InStr(1, TextBox1.Text, "guo") > 0 Then
            TextBox1.Text = TextBox1.Text.Replace("guo", "鍋")
        ElseIf InStr(1, TextBox1.Text, "hao") > 0 Then
            TextBox1.Text = TextBox1.Text.Replace("hao", "薅")
        ElseIf InStr(1, TextBox1.Text, "ma") > 0 Then
            TextBox1.Text = TextBox1.Text.Replace("ma", "妈")
        ElseIf InStr(1, TextBox1.Text, "ni") > 0 Then
            TextBox1.Text = TextBox1.Text.Replace("ni", "妮")
        ElseIf InStr(1, TextBox1.Text, "yi") > 0 Then
            TextBox1.Text = TextBox1.Text.Replace("yi", "一")
        ElseIf InStr(1, TextBox1.Text, "zhong") > 0 Then
            TextBox1.Text = TextBox1.Text.Replace("zhong", "中")
        End If
        TextBox1.SelectionStart = TextBox1.SelectionStart + 1
    ElseIf e.KeyCode = 50 Then '2nd tone, rising
        If InStr(1, TextBox1.Text, "guo") > 0 Then
            TextBox1.Text = TextBox1.Text.Replace("guo", "国")
        ElseIf InStr(1, TextBox1.Text, "hao") > 0 Then
            TextBox1.Text = TextBox1.Text.Replace("hao", "号")
        ElseIf InStr(1, TextBox1.Text, "ma") > 0 Then
            TextBox1.Text = TextBox1.Text.Replace("ma", "麻")
        ElseIf InStr(1, TextBox1.Text, "ni") > 0 Then
            TextBox1.Text = TextBox1.Text.Replace("ni", "貎")
        ElseIf InStr(1, TextBox1.Text, "yi") > 0 Then
            TextBox1.Text = TextBox1.Text.Replace("yi", "仪")
        End If
        TextBox1.SelectionStart = TextBox1.SelectionStart + 1
    ElseIf e.KeyCode = 51 Then '3rd tone, dipping then rising
        If InStr(1, TextBox1.Text, "guo") > 0 Then
            TextBox1.Text = TextBox1.Text.Replace("guo", "果")
        ElseIf InStr(1, TextBox1.Text, "hao") > 0 Then
            TextBox1.Text = TextBox1.Text.Replace("hao", "好")
        ElseIf InStr(1, TextBox1.Text, "ma") > 0 Then
            TextBox1.Text = TextBox1.Text.Replace("ma", "马")
        ElseIf InStr(1, TextBox1.Text, "ni") > 0 Then
            TextBox1.Text = TextBox1.Text.Replace("ni", "你")
        ElseIf InStr(1, TextBox1.Text, "yi") > 0 Then
            TextBox1.Text = TextBox1.Text.Replace("yi", "已")
        ElseIf InStr(1, TextBox1.Text, "zhong") > 0 Then
            TextBox1.Text = TextBox1.Text.Replace("zhong", "塚")
        End If
        TextBox1.SelectionStart = TextBox1.SelectionStart + 1
    ElseIf e.KeyCode = 52 Then '4th tone, dipping
        If InStr(1, TextBox1.Text, "guo") > 0 Then
            TextBox1.Text = TextBox1.Text.Replace("guo", "过")
        ElseIf InStr(1, TextBox1.Text, "hao") > 0 Then
            TextBox1.Text = TextBox1.Text.Replace("hao", "睦")
        ElseIf InStr(1, TextBox1.Text, "ma") > 0 Then
            TextBox1.Text = TextBox1.Text.Replace("ma", "骂")
        ElseIf InStr(1, TextBox1.Text, "ni") > 0 Then
            TextBox1.Text = TextBox1.Text.Replace("ni", "逆")
        ElseIf InStr(1, TextBox1.Text, "yi") > 0 Then
            TextBox1.Text = TextBox1.Text.Replace("yi", "亄")
        ElseIf InStr(1, TextBox1.Text, "zhong") > 0 Then
            TextBox1.Text = TextBox1.Text.Replace("zhong", "众")
        End If
        TextBox1.SelectionStart = TextBox1.TextLength
    ElseIf e.KeyCode = 48 Or e.KeyCode = 53 Then '5th tone, none
        If InStr(1, TextBox1.Text, "ma") > 0 Then
            TextBox1.Text = TextBox1.Text.Replace("ma", "吗")
        End If
        TextBox1.SelectionStart = TextBox1.TextLength
        'MsgBox(TextBox1.SelectionStart)
    End If
End Sub

Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
    TextBox1.Text = TextBox1.Text.Replace("0", "")
    TextBox1.Text = TextBox1.Text.Replace("1", "")
    TextBox1.Text = TextBox1.Text.Replace("2", "")
    TextBox1.Text = TextBox1.Text.Replace("3", "")
    TextBox1.Text = TextBox1.Text.Replace("4", "")
    TextBox1.Text = TextBox1.Text.Replace("5", "")
    TextBox1.Text = TextBox1.Text.Replace(".", "。")
    TextBox1.Text = TextBox1.Text.Replace(",", ",")
    TextBox1.Text = TextBox1.Text.Replace(":", ":")
    TextBox1.Text = TextBox1.Text.Replace(";", ";")
    TextBox1.Text = TextBox1.Text.Replace("?", "?")
    TextBox1.Text = TextBox1.Text.Replace("!", "!")
End Sub

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
    Clipboard.SetDataObject(TextBox1.Text)
End Sub

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    TextBox1.Text = ""
End Sub
End Class

这是我的代码。

每当我使用 TextBox1.Text.Replace 时,我的光标都会不断移回 TextBox 的开头,以防止任何阻止它的尝试,例如

TextBox1.SelectionStart = TextBox1.SelectionStart + 1

和东西。此外,除非我使用 + 或 - 更改它,否则 SelectionStart 始终为 0。

4

1 回答 1

0

您可以在覆盖文本之前获取位置,然后在完成替换后将其设置回该位置:

Dim pos As Integer = TextBox1.SelectionStart
TextBox1.Text = TextBox1.Text.Replace(...)
TextBox1.SelectionStart = pos
于 2012-12-17T18:53:48.263 回答