0
votes

I am trying to implent send data from windows form text box control to HTML Text Box,

Scenario: My windows form in C# having various TextBox Control and one submit button while click on the botton the textbox data is transfer to HTML Tetxtboxes. (I dont want to use any query string etc)

My Html Code

<!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 Code

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;

            }
        }
    }
}

Using this code only the first winform textbox value is passess to HTML Textbox but i want to transfer the values for other textxbox from winform textbox to html textbox.

using the for loop I get an Length = 4 but I dont know how to use this loop to transfer the data from winform to html?

1
@Lloyd Yes I dont know how to do this in loop... just for the understanding I add that lineIMMORTAL

1 Answers

0
votes

Just paste the all the code I just add function

SendTextToActiveElementWithSubmitOptionSet(bool btnSubmit) 

I solve my issue below my code

 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;
                        }                        
                    }
            }
        }