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を停止しようとしない限り、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 に答える