阻止用户在生成的文本之前输入
Stop user from typing before generated text
我正在用 WPF(使用 C#)编写一个文本编辑器,它使用 RichTextBox 模拟聊天程序。当用户按下回车键时,相关的用户名会自动插入到下一行。但是,如果用户输入的速度足够快,在回车和其他按键之间交替,他们的文本可能会出现在生成的用户名之前。这是一个可能更好地证明这一点的屏幕截图: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>
诚然,我不确定这是否可以解决,我只是希望用户不要那么快...
Adriano Repetti 在评论中得到它;我必须关闭文本框的 AcceptsReturn 并自己处理在输入键上插入新行。
You have a race with your box. Both want to handle Enter key: you in textBoxEnterPressed and RichTextBox to add a new line. I'd stop this competition setting AcceptsReturn to False. It shouldn't change anything here (unless you have a default button in your dialog) but I'd also set e.Handled to 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;
}
我正在用 WPF(使用 C#)编写一个文本编辑器,它使用 RichTextBox 模拟聊天程序。当用户按下回车键时,相关的用户名会自动插入到下一行。但是,如果用户输入的速度足够快,在回车和其他按键之间交替,他们的文本可能会出现在生成的用户名之前。这是一个可能更好地证明这一点的屏幕截图: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>
诚然,我不确定这是否可以解决,我只是希望用户不要那么快...
Adriano Repetti 在评论中得到它;我必须关闭文本框的 AcceptsReturn 并自己处理在输入键上插入新行。
You have a race with your box. Both want to handle Enter key: you in textBoxEnterPressed and RichTextBox to add a new line. I'd stop this competition setting AcceptsReturn to False. It shouldn't change anything here (unless you have a default button in your dialog) but I'd also set e.Handled to 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;
}