嗨,伟大的大师!
我正在使用WinForms应用程序,该应用程序具有允许粘贴CSV文本数据的文本框,并对其进行一些格式化。我有我需要的格式。但是,我最终得到的是原始粘贴文本(CSV)和格式化文本。
如何截取粘贴的文字,进行格式化,然后附加文本框中已有的文字?
这是我在文本框的KeyDown事件中的Ctrl + V代码:
void txtNote_KeyDown(object sender, KeyEventArgs e)
{
TextBox box = (TextBox)sender;
if (e.Control && e.KeyCode == Keys.V)
{
string[] strtextlines = Clipboard.GetText().ToString().Split(new char[] { '\n' });
strtextlines[0] = strtextlines[0].Replace("\r\n", "");
string strtextheader = "||" + strtextlines[0].Replace(",", "||");
strtextheader = strtextheader + "||\r\n";
string strttextbody = "";
for (int i = 1; i <= strtextlines.Length-1; i++)
{
strtextlines[i] = strtextlines[i].Replace("\r\n", "");
strttextbody = strttextbody + "|" + textlines[i].Replace(",", "|");
strttextbody = strttextbody + "|\r\n";
}
box.Text = box.Text + "\r\n" + strtextheader + strttextbody;
}
}