Search code examples
c#htmlmshtml

How to set the value of first input type text in document's first form using mshtml in C#?


I am trying to set the value of first input type text in document's first form. See the code below.

HTMLDocument htmldoc = new HTMLDocumentClass();  
htmldoc = (HTMLDocument)WebBrowser.Document;  
HTMLFormElement fm = (HTMLFormElement)htmldoc.forms.item(0); 

In the above code I am getting first form as a form object. Now I want to find the first input type text in this form and set the value of that textbox.

Any help would be appreciated.


Solution

  • loop in all elements in form

       private void button5_Click(object sender, EventArgs e)
        {
            var htmldoc = (HTMLDocument)webBrowser1.Document.DomDocument;
            HTMLFormElement fm = (HTMLFormElement)htmldoc.forms.item(0);
            foreach (IHTMLElement item in (IHTMLElementCollection)fm.all)
            {
                var textbox = item as IHTMLInputElement;
                if (textbox != null)
                {
                    textbox.value = "your text";
                }
            }
        }