我正在尝试将数据从Windows窗体文本框控件发送到HTML文本框
场景:我的Windows窗体在C#中具有各种TextBox控件和一个提交按钮,同时单击按钮,文本框数据将传输到HTML Tetxtboxes。 (我不想使用任何查询字符串等)
我的Html代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test Application</title>
<script type="text/javascript" language="javascript">
window.onload = body_Onload;
function body_Onload() {
document.getElementById("txtFirstName").focus();
}
</script>
</head>
<body>
First Name:
<input type="text" id="txtFirstName" /><br />
Last Name:
<input type="text" id="txtLastName" /><br />
Address:
<input type="text" id="txtAddress" /><br />
Mobile:
<input type="text" id="txtMobile" /><br />
</body>
</html>
C#Winform代码
public partial class Form1 : Form
{
private SHDocVw.InternetExplorer TargetIE = null;
string url;
public Form1()
{
this.button1 = new System.Windows.Forms.Button();
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();
}
private void button1_Click(object sender, EventArgs e)
{
GetTheIEObjectFromSystem("Q_26773800");
SendTextToActiveElementWithSubmitOptionSet(false);
}
private void GetTheIEObjectFromSystem(string inurl = ".")
{
SHDocVw.ShellWindows SWs = new SHDocVw.ShellWindows();
foreach (SHDocVw.InternetExplorer internetExplorer in SWs)
{
url = internetExplorer.LocationURL;
TargetIE = internetExplorer;
return;
}
}
private void SendTextToActiveElementWithSubmitOptionSet(bool btnSubmit)
{
mshtml.IHTMLDocument2 document = null;
document = TargetIE.Document as mshtml.IHTMLDocument2;
if (!document.activeElement.isTextEdit)
{
MessageBox.Show("Active element is not a text-input system");
}
else
{
HTMLInputElement HTMLI;
//HTMLI = document.activeElement as HTMLInputElement;
HTMLI = document.activeElement as HTMLInputElement;
var tag = HTMLI.document as mshtml.HTMLDocumentClass;
mshtml.IHTMLElementCollection a = tag.getElementsByTagName("input");
for (int i = 0; i< a.length; i++) // a.length = 4
{
}
HTMLI.value = textBox1.Text;
}
}
}
}
使用此代码只有第一个winform文本框值传递给HTML Textbox,但我想将其他textxbox的值从winform文本框传输到html文本框。
使用for循环我得到一个Length = 4但是我不知道如何使用这个循环将数据从winform传输到html?
答案 0 :(得分:0)
只需粘贴我刚刚添加的所有代码功能
SendTextToActiveElementWithSubmitOptionSet(bool btnSubmit)
我在我的代码
下解决了我的问题 private void SendTextToActiveElementWithSubmitOptionSet(bool btnSubmit)
{
mshtml.IHTMLDocument2 document = null;
document = TargetIE.Document as mshtml.IHTMLDocument2;
if (!document.activeElement.isTextEdit)
{
MessageBox.Show("Active element is not a text-input system");
}
else
{
HTMLInputElement HTMLI;
HTMLI = document.activeElement as HTMLInputElement;
var tag = HTMLI.document as mshtml.HTMLDocumentClass;
mshtml.IHTMLElementCollection hTMLElementCollection = tag.getElementsByTagName("input");
foreach (mshtml.HTMLInputElement el in hTMLElementCollection)
{
switch (el.id)
{
case "txtFirstName":
el.value = textBox1.Text;
break;
case "txtLastName":
el.value = textBox2.Text;
break;
case "txtAddress":
el.value = textBox3.Text;
break;
case "txtMobile":
el.value = textBox4.Text;
break;
}
}
}
}