C#如何在带有sendkeys的循环中读取第一行、第二行、第三行?

C#如何在带有sendkeys的循环中读取第一行、第二行、第三行?,c#,C#,我想创建一个程序,将多行文本框中的键发送到外部程序。 文本框中有多行文本,但我需要第一行, 并将其发送到外部程序 我们的想法是,在循环中进行,当它到达最后一行时停止 我已经编写了一些代码,但它不能像我需要的那样工作,我不是这类编程语言中最好的 多行文本框中的文本: Hello im here Here to create Create for honor honor for all all for hello Hello im here Here to create Create

我想创建一个程序,将多行文本框中的键发送到外部程序。 文本框中有多行文本,但我需要第一行, 并将其发送到外部程序

我们的想法是,在循环中进行,当它到达最后一行时停止

我已经编写了一些代码,但它不能像我需要的那样工作,我不是这类编程语言中最好的

多行文本框中的文本:

Hello im here 
Here to create 
Create for honor 
honor for all 
all for hello 
Hello im here 
Here to create 
Create for honor 
honor for all 
all for hello
代码:

但我需要这样:

Hello im here 
//first line -> Timer1 ends -> Start Timer1 agian to read second line
Here to create
//Second line -> Timer1 ends -> Start Timer1 agian to read third line
Create for honor
//Third line -> Timer1 ends -> Start Timer1 agian to read fourth line

等等,直到循环到达最后一行并在最后一行停止。

你的计时器实际上没有做任何事情,因为你正在使用
线程睡觉。睡眠
而不是等待计时器事件-因此你在开始时睡一次5秒,然后再也不睡了

只需将代码更改为:

for (int i = 0; i < richTextBox1.Lines.Length; i++)
{
    System.Threading.Thread.Sleep(5000);
    SendKeys.Send(richTextBox1.Lines[i] + "\r\n");
}
for(int i=0;i
这样,在进入下一行之前,每次迭代都会休眠5秒

如果您得到了示例中所示的多个换行符,那么请检查
字符串是否已经包含终止换行符(在这种情况下,您将使每个字符串以两个换行符结尾)



可能值得记住的是,除非这发生在UI线程上(不建议),否则用户可以在您执行此操作时愉快地编辑文本框的文本。您应该做一些UI工作来阻止它,或者在函数的开头复制一个
成员并使用副本。

明白了!但是怎样才能只拿到第一条线呢?我获取了您的代码,并将richTextBox1.Lines[I]更改为richTextBox1.Lines[0],因此它将只拾取第一行。它发送第一行!:但文本框中有117行,它在第一行发送117x。。如何只选择第一行,而不选择第一行multiply@AlJuicebox你只想发第一行?这不是你最初的问题所要求的。在这种情况下,只需删除
for
循环并执行
线程.Sleep
,然后执行
SendKeys.Send(richTextBox1.Lines[0]+“\r\n”)。我让它工作了。对不起,我一开始没有问这个问题。。我为(int I=0;I,为(int I=117;I,并通过发送键编写了
sendkeys.Send(richTextBox1.Lines[0]+“\n”)。现在它只选择第一行。
Hello im here 
//first line -> Timer1 ends -> Start Timer1 agian to read second line
Here to create
//Second line -> Timer1 ends -> Start Timer1 agian to read third line
Create for honor
//Third line -> Timer1 ends -> Start Timer1 agian to read fourth line
for (int i = 0; i < richTextBox1.Lines.Length; i++)
{
    System.Threading.Thread.Sleep(5000);
    SendKeys.Send(richTextBox1.Lines[i] + "\r\n");
}