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

本文关键字:文本 用户 | 更新日期: 2023-09-27 17:57:02

我正在用WPF(使用C#)编写一个文本编辑器,它使用RichTextBox模拟聊天程序。当用户按 Enter 时,相关用户名将自动插入下一行。但是,如果用户键入速度足够快,在 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>

诚然,我不确定这是否可以修复,我应该只希望用户不会那么快......

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

阿德里亚诺·雷佩蒂在评论中得到了它;我不得不关闭文本框的"接受返回",并自己处理在输入按时插入新行。

你和你的盒子赛跑。两者都想处理回车键:你在textBoxEnterPressed和RichTextBox中添加一个新行。我会停止这个比赛设置接受返回为假。它不应该在这里更改任何内容(除非您的对话框中有一个默认按钮),但我也会将 e.Handle 设置为 true。

我已经玩弄了一下你的代码,这就是我管理的:

    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;
    }