C# 当我试图在鼠标焦点上键入文本时,会出现不必要的空行

C# 当我试图在鼠标焦点上键入文本时,会出现不必要的空行,c#,winforms,textbox,C#,Winforms,Textbox,我使用的是一个多行文本框,我可以在其中连续键入消息或键入文本,同时按几次enter键来键入消息 因此,我的输入可能如下所示: This is some text this is come other text and some other text 并使用SendKeys.Send(txtText.Text+“{enter}”)我的输出如下所示: This is some text this is come other text and some other text 如何删除不必要的新

我使用的是一个多行文本框,我可以在其中连续键入消息或键入文本,同时按几次enter键来键入消息

因此,我的输入可能如下所示:

This is some text
this is come other text
and some other text
并使用
SendKeys.Send(txtText.Text+“{enter}”)我的输出如下所示:

This is some text

this is come other text

and some other text
如何删除不必要的新行?

试试

  string text=txtText.Text;
  txtText.Text=string.Join(Environment.NewLine, 
                    text.Split(new string[] { Environment.NewLine }, 
                    StringSplitOptions.RemoveEmptyEntries));