阻止用户在生成文本之前键入内容

时间:2015-06-18 15:13:27

标签: c# wpf richtextbox user-input

我正在WPF中编写一个文本编辑器(使用C#),它使用RichTextBox模拟聊天程序。当用户按Enter键时,相关的用户名将自动插入下一行。但是,如果用户键入足够快,在输入和其他按键之间交替,则他们的文本可以出现在生成的用户名之前。以下是可能更好地展示这一点的屏幕截图:http://oi62.tinypic.com/fusv1j.jpg

这个问题过去更糟糕了,在搜索之后我尝试在插入后手动将插入位置设置到最后;不幸的是,如果你足够快,它仍然可以在首字母之前获得文本。

以下是RichTextBox的KeyUp事件的C#以及相关的帮助方法:

private void textBoxEnterPressed(object sender, KeyEventArgs e)
{
    if (e.Key != Key.Enter || initialsCheckBox.IsChecked == false)
        return;

    Chumhandle active = getActiveHandleBox().SelectedItem as Chumhandle;
    AppendText(mainTextBox, active.Initials + ": ", active.HexCode);

    TextPointer caretPos = mainTextBox.CaretPosition;
    caretPos = caretPos.DocumentEnd;
    mainTextBox.CaretPosition = caretPos;
}

private ComboBox getActiveHandleBox()
{
    if (activeBox == 1)
        return handleBox1;
    else
        return handleBox2;
}

public static void AppendText(RichTextBox box, string text, string color)
{
    BrushConverter bc = new BrushConverter();
    TextRange tr = new TextRange(box.Document.ContentEnd, box.Document.ContentEnd);
    tr.Text = text;
    try
    {
        tr.ApplyPropertyValue(TextElement.ForegroundProperty, bc.ConvertFromString(color));
    }
    catch (FormatException) { }

    box.Selection.ApplyPropertyValue(RichTextBox.ForegroundProperty, bc.ConvertFromString(color));
}

RichTextBox的XAML:

<RichTextBox Name="mainTextBox" Grid.Row="3" FontFamily="Courier New" AcceptsReturn="True" VerticalScrollBarVisibility="Visible" BorderThickness="0" KeyUp="textBoxEnterPressed">
    <RichTextBox.Resources>
        <Style TargetType="{x:Type Paragraph}">
            <Setter Property="Margin" Value="0" />
        </Style>
    </RichTextBox.Resources>
</RichTextBox>

不可否认,我不确定这是否可以解决,我应该希望用户不会那么快......

2 个答案:

答案 0 :(得分:1)

Adriano Repetti在评论中得到了它;我不得不关闭文本框的AcceptsReturn,并自己在输入印刷机上插入新行。

  

你的盒子里有一场比赛。两者都想处理Enter键:你在textBoxEnterPressed和RichTextBox中添加一个新行。我将此竞赛设置为AcceptsReturn停止为False。它不应该在这里改变任何东西(除非你的对话框中有一个默认按钮),但我也将e.Handled设置为true。

答案 1 :(得分:0)

我玩了一些代码,这就是我管理的内容:

    private void textBoxEnterPressed(object sender, KeyEventArgs e)
    {
        if (e.Key != Key.Enter)
            return;

        // ADDED THIS TO SIMULATE AcceptsReturn = True
        if (initialsCheckBox.IsChecked == false)
        {
            AppendText(mainTextBox, Environment.NewLine, "#000000");
            return;
        }

        Chumhandle active = getActiveHandleBox().SelectedItem as Chumhandle;

        // ADDED Environment.NewLine TO INSERT LINE BREAKS
        AppendText(mainTextBox, Environment.NewLine + active.Initials + ": ", active.HexCode);

        // COMMENTED THIS BECAUSE IT WAS FORCING UNWANTED BEHAVIOR
        //TextPointer caretPos = mainTextBox.CaretPosition;
        //caretPos = caretPos.DocumentEnd;
        //mainTextBox.CaretPosition = caretPos;
    }

    public static void AppendText(RichTextBox box, string text, string color)
    {
        BrushConverter bc = new BrushConverter();

        // INSTEAD OF USING box.Document, I'VE USED box.Selection TO INSERT
        // THE TEXT WHEREVER THE CURSOR IS (OR IF YOU HAVE TEXT SELECTED)
        TextRange tr = new TextRange(box.Selection.Start, box.Selection.End);
        tr.Text = text;
        try
        {
            tr.ApplyPropertyValue(TextElement.ForegroundProperty, bc.ConvertFromString(color));
        }
        catch (FormatException) { }

        // I DON'T UNDERSTAND WHAT THIS IS DOING SO I KEPT IT -_^
        box.Selection.ApplyPropertyValue(RichTextBox.ForegroundProperty, bc.ConvertFromString(color));

        // FINALLY, I SET THE CARET TO THE END OF THE INSERTED TEXT
        box.CaretPosition = tr.End;
    }