C# 将Winform Textbox数据传输到Html Textbox
我正在尝试将数据从windows窗体文本框控件发送到HTML文本框 场景:C#中的我的windows窗体具有各种文本框控件和一个提交按钮,同时单击底部,文本框数据将传输到HTML文本框。(我不想使用任何查询字符串等) 我的Html代码C# 将Winform Textbox数据传输到Html Textbox,c#,html,winforms,C#,Html,Winforms,我正在尝试将数据从windows窗体文本框控件发送到HTML文本框 场景:C#中的我的windows窗体具有各种文本框控件和一个提交按钮,同时单击底部,文本框数据将传输到HTML文本框。(我不想使用任何查询字符串等) 我的Html代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <
<!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>
测试应用
window.onload=body\u onload;
函数体_Onload(){
document.getElementById(“txtFirstName”).focus();
}
名字:
姓氏:
地址:
流动电话:
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;
}
}
}
}
公共部分类表单1:表单
{
私有SHDocVw.InternetExplorer TargetIE=null;
字符串url;
公共表格1()
{
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();
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
从系统获取对象(“Q_26773800”);
发送文本以激活具有SubmitOptiStart的元素(false);
}
私有void GetTheIEObjectFromSystem(字符串inurl=“.”)
{
SHDocVw.ShellWindows SWs=新的SHDocVw.ShellWindows();
foreach(SWs中的SHDocVw.InternetExplorer InternetExplorer)
{
url=internetExplorer.LocationURL;
TargetIE=internetExplorer;
返回;
}
}
private void SendTextToActivieLementwith SubmitOptiStart(bool btnSubmit)
{
mshtml.IHTMLDocument2 document=null;
document=TargetIE.document为mshtml.IHTMLDocument2;
如果(!document.activeElement.isTextEdit)
{
Show(“活动元素不是文本输入系统”);
}
其他的
{
HTMLInputElement HTMLI;
//HTMLI=document.activeElement作为HTMLInputElement;
HTMLI=document.activeElement作为HTMLInputElement;
var tag=HTMLI.document作为mshtml.HTMLDocumentClass;
mshtml.IHTMLElementCollection a=tag.getElementsByTagName(“输入”);
对于(int i=0;i
使用此代码,只有第一个winform textbox值是passess to HTML textbox,但我想将其他textxbox的值从winform textbox传输到HTML textbox
使用for循环,我得到的长度为4,但我不知道如何使用此循环将数据从winform传输到html?只需粘贴我刚才添加的所有代码即可
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;
}
}
}
}
@劳埃德:是的,我不知道如何在循环中做到这一点。。。为了便于理解,我添加了这一行