我想将每个项目从我的列表框发送到记事本,但我的逻辑有点击败我。使用进程将数据发送到记事本
private void send_Click(object sender, EventArgs e)
{
var notepad = Process.GetProcessesByName("Notepad").FirstOrDefault(p => p.MainWindowTitle == "Untitled - Notepad");
if (notepad != null)
{
if (IsIconic(notepad.MainWindowHandle))
ShowWindow(notepad.MainWindowHandle, 9);
SetForegroundWindow(notepad.MainWindowHandle);
string text = "";
foreach (var item in listBox1.Items)
{
text = item.ToString();
Clipboard.SetText(text);
SendKeys.Send("^V");
SendKeys.Send("{ENTER}");
}
}
}
在我的逻辑这应该每一个项目从列表框中每个项目在不同的line.But不发生每次发送到记事本,有时它只是从列表框中为发送唯一的最后一个项目列表框中有很多项目。我错过了什么吗?
尽量放慢粘贴部分,可能是睡眠的效果,我觉得像你想粘贴太快。 –
没有想到,真的很好的提示:) –
而不是发送单个项目,只需使用'String.Join'在单个字符串中组合所有它们,例如'String.Join(“\ n”,listBox1。项目)' –