当按 C# 句号时添加额外的 space

Add additional space when fullstop were press C#

我有一个富文本框,我想在每次按“.”(句号)时在中间添加一个 space。

它应该在我按下句号后自动 add/insert 一个 space(无需按 space 栏)。

您可以创建一个方法来处理 richTextBox.OnKeyUp 事件,这样如果按下的键是句号,那么在您的文本中附加 space.

private void RichtextBox1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
    // Determine whether the key entered is the period key. Append a space to the textbox if it is.
    if(e.KeyCode == Keys.OemPeriod)
    {
        RichTextBox1.Text += " ";
    }
}

显然,您必须为自己的 richTextBox 创建此事件,而不是我 "RichTextBox1"

的示例

这将在按下 句点 (.) 之后添加一个 space。您需要使用 KeyUp 事件。

private void richTextBox1_KeyUp(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.OemPeriod)
        richTextBox1.Text += " ";
        richTextBox1.SelectionStart = richTextBox1.Text.Length;
}