6

TextBoxのキーストロークを抑制したい。Backspace以外のすべてのキーストロークを抑制するために、私は以下を使用します。

    private void KeyBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
    {
        e.Handled = true;
    }

ただし、押されたキーがBackspaceの場合にのみ、キーストロークを抑制したいと思います。私は以下を使用します:

        if (e.Key == System.Windows.Input.Key.Back)
        {
            e.Handled = true;
        }

ただし、これは機能しません。選択開始の後ろの文字はまだ削除されています。出力に「TRUE」が表示されるので、Backキーが認識されています。ユーザーがバックスペースを押さないようにするにはどうすればよいですか?(これは、文字ではなく単語を削除したい場合があるため、戻るキーを自分で押す必要があるためです)。

4

7 に答える 7

3

Silverlightでは、バックスペースなどのシステムキーイベントを処理する方法はありません。したがって、それを検出することはできますが、手動で処理することはできません。

于 2013-03-09T23:46:07.887 に答える
1

これには、キーダウンイベントの前にテキストボックスの値を保存する必要があります。残念ながら、バックスペースはそのイベントが発生する前に処理されるため、発生する前にキャプチャする必要があり、キーアップイベントが処理された後に再度更新できます。

    private string textBeforeChange;

    private void TextBox1_OnKeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Back)
        {
            e.Handled = true;
            textBox1.Text = textBeforeChange;
        }
    }

    private void TextBox1_OnKeyUp(object sender, KeyEventArgs e)
    {
        textBeforeChange = textBox1.Text;
    }

    private void MainPage_OnLoaded(object sender, RoutedEventArgs e)
    {
        textBox1.AddHandler(TextBox.KeyDownEvent, new KeyEventHandler(TextBox1_OnKeyDown), true);
        textBox1.AddHandler(TextBox.KeyUpEvent, new KeyEventHandler(TextBox1_OnKeyUp), true);
        textBox1.AddHandler(TextBox.ManipulationStartedEvent, new EventHandler<ManipulationStartedEventArgs>(TextBox1_OnManipulationStarted), true);
    }

    private void TextBox1_OnManipulationStarted(object sender, ManipulationStartedEventArgs e)
    {
        textBeforeChange = textBox1.Text;
    }
于 2013-03-11T04:47:04.237 に答える
0

このシナリオを処理する簡単な方法がないことは事実ですが、可能です。

KeyDown、TextChanged、および KeyUp イベント間をホップするときに、入力テキストの状態、カーソル位置、および戻るキーが押された状態を格納するために、クラスにいくつかのメンバー変数を作成する必要があります。

コードは次のようになります。

    string m_TextBeforeTheChange;
    int m_CursorPosition = 0;
    bool m_BackPressed = false;

    private void KeyBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
    {
        m_TextBeforeTheChange = KeyBox.Text;
        m_BackPressed = (e.Key.Equals(System.Windows.Input.Key.Back)) ? true : false;
    }

    private void KeyBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        if (m_BackPressed)
        {
            m_CursorPosition = KeyBox.SelectionStart;
            KeyBox.Text = m_TextBeforeTheChange;
        }
    }

    private void KeyBox_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
    {
        KeyBox.SelectionStart = (m_BackPressed) ? m_CursorPosition + 1 : KeyBox.SelectionStart;
    }
于 2013-03-10T08:13:09.267 に答える
0
    string oldText = "";
    private void testTextBlock_TextChanged(object sender, TextChangedEventArgs e)
    {
        if (testTextBlock.Text.Length < oldText.Length)
        {
            testTextBlock.Text = oldText;
            testTextBlock.SelectionStart = oldText.Length;
        }
        else
        {
            oldText = testTextBlock.Text;
        }
    }
于 2013-03-10T08:47:45.450 に答える