0

我正在尝试将数据从 Windows 窗体文本框控件发送到 HTML 文本框,

场景:我在 C# 中的 Windows 窗体具有各种 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 文本框,但我想将其他文本框的值从 winform 文本框传输到 html 文本框。

使用for循环我得到一个Length = 4,但我不知道如何使用这个循环将数据从winform传输到html?

4

1 回答 1

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;
                        }                        
                    }
            }
        }
于 2013-09-03T15:57:33.750 回答