大家来看看啊,我做这个程序的主要目的是,把 XML文件读出来,并在另外一个程序的3个textbox中依次填写。
所以我模拟键盘操作复制内容,但是运行之后在另外程序的Textbox中模拟粘贴时,为什么每次只能粘贴出最后一个数据(5.95)Tab是模拟textbox之间切换。如下XML文件
<bookstore>
<table>
<title>Oberon's Legacy </title>
<author>Corets, Eva </author>
<price>5.95 </price>
</table>
</bookstore>
请告高手帮我改改程序。private void button1_Click(object sender, EventArgs e)
{ //这个button作用从指定目录选择XML文件(这里没问题) Stream myStream; openFileDialog1.InitialDirectory = " "; openFileDialog1.Filter = "txt files (*.xml)|*.xml"; openFileDialog1.RestoreDirectory = true; if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
myStream.Close();
}
} this.textBox1.Text = this.openFileDialog1.FileName; }
private void button2_Click(object sender, EventArgs e)
{ DataSet ds = new DataSet(); ds.ReadXml(this.textBox1.Text); this.dataGridView1.DataSource = ds.Tables[0]; DataTable dataTable = new DataTable();
//以上是将XML数据填充到DataGridView中 XmlDocument xd = new XmlDocument(); xd.Load(this.textBox1.Text); foreach (XmlNode xn in xd.FirstChild.ChildNodes)
{
foreach (XmlNode xxn in xn.ChildNodes)
{
Clipboard.SetText(xxn.InnerText); Thread.Sleep(500);
SendKeys.SendWait("^V"); SendKeys.SendWait("{TAB}");
}
}
//主要是这里遍历XML文件后,我在另一个程序的TextBox中模拟粘贴时,每次粘贴出的内容都是XML文件的最后一个数据“5.95”.
}请C#达人帮我改改,真的是救命程序啊。在做不出来我完完了。
所以我模拟键盘操作复制内容,但是运行之后在另外程序的Textbox中模拟粘贴时,为什么每次只能粘贴出最后一个数据(5.95)Tab是模拟textbox之间切换。如下XML文件
<bookstore>
<table>
<title>Oberon's Legacy </title>
<author>Corets, Eva </author>
<price>5.95 </price>
</table>
</bookstore>
请告高手帮我改改程序。private void button1_Click(object sender, EventArgs e)
{ //这个button作用从指定目录选择XML文件(这里没问题) Stream myStream; openFileDialog1.InitialDirectory = " "; openFileDialog1.Filter = "txt files (*.xml)|*.xml"; openFileDialog1.RestoreDirectory = true; if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
myStream.Close();
}
} this.textBox1.Text = this.openFileDialog1.FileName; }
private void button2_Click(object sender, EventArgs e)
{ DataSet ds = new DataSet(); ds.ReadXml(this.textBox1.Text); this.dataGridView1.DataSource = ds.Tables[0]; DataTable dataTable = new DataTable();
//以上是将XML数据填充到DataGridView中 XmlDocument xd = new XmlDocument(); xd.Load(this.textBox1.Text); foreach (XmlNode xn in xd.FirstChild.ChildNodes)
{
foreach (XmlNode xxn in xn.ChildNodes)
{
Clipboard.SetText(xxn.InnerText); Thread.Sleep(500);
SendKeys.SendWait("^V"); SendKeys.SendWait("{TAB}");
}
}
//主要是这里遍历XML文件后,我在另一个程序的TextBox中模拟粘贴时,每次粘贴出的内容都是XML文件的最后一个数据“5.95”.
}请C#达人帮我改改,真的是救命程序啊。在做不出来我完完了。
{
foreach (XmlNode xxn in xn.ChildNodes)
{
Clipboard.SetText(xxn.InnerText); Thread.Sleep(500);
SendKeys.SendWait("^V"); SendKeys.SendWait("{TAB}");
}
}感觉是这里的问题,这里最后只取了5.59,不知道你其他代码 都没法测试
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Xml;
using System.Threading;
namespace test
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.textBox4 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(48, 24);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(344, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(48, 96);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(264, 21);
this.textBox2.TabIndex = 1;
this.textBox2.Text = "";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(48, 136);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(264, 21);
this.textBox3.TabIndex = 2;
this.textBox3.Text = "";
//
// textBox4
//
this.textBox4.Location = new System.Drawing.Point(48, 168);
this.textBox4.Name = "textBox4";
this.textBox4.Size = new System.Drawing.Size(264, 21);
this.textBox4.TabIndex = 3;
this.textBox4.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(432, 24);
this.button1.Name = "button1";
this.button1.TabIndex = 4;
this.button1.Text = "上传";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(552, 24);
this.button2.Name = "button2";
this.button2.TabIndex = 5;
this.button2.Text = "获取数据";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(744, 541);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox4);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
Stream myStream; openFileDialog1.InitialDirectory = " "; openFileDialog1.Filter = "txt files (*.xml)|*.xml"; openFileDialog1.RestoreDirectory = true; if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
myStream.Close();
}
} this.textBox1.Text = this.openFileDialog1.FileName; } private void button2_Click(object sender, System.EventArgs e)
{
DataSet ds = new DataSet(); ds.ReadXml(this.textBox1.Text); //this.dataGridView1.DataSource = ds.Tables[0]; DataTable dataTable = new DataTable();
//以上是将XML数据填充到DataGridView中 XmlDocument xd = new XmlDocument(); xd.Load(this.textBox1.Text);
textBox2.Focus(); foreach (XmlNode xn in xd.FirstChild.ChildNodes)
{
foreach (XmlNode xxn in xn.ChildNodes)
{
Clipboard.SetDataObject(xxn.InnerText); Thread.Sleep(500);
SendKeys.SendWait("^V"); SendKeys.SendWait("{TAB}");
}
}
}
}
}
{
foreach (XmlNode xxn in xn.ChildNodes)
{
Clipboard.SetText(xxn.InnerText); Thread.Sleep(500);
SendKeys.SendWait("^V"); SendKeys.SendWait("{TAB}");
}
}
//主要是这里遍历XML文件后,我在另一个程序的TextBox中模拟粘贴时,每次粘贴出的内容都是XML文件的最后一个数据“5.95”.
}
在这个程序中:
SendKeys.SendWait("^V"); SendKeys.SendWait("{TAB}");
之前你是否要定义下:文本框是否被选中: 因为你要粘贴到哪个里面先要选中这个文本框吧!