如何在 C# 中使用 mshtml 设置文档第一个表单中第一个输入类型文本的值?

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

我正在尝试在文档的第一种形式中设置第一种输入类型文本的值。请参阅下面的代码。

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

在上面的代码中,我将第一个表单作为表单对象。现在我想在此表单中找到第一个输入类型文本并设置该文本框的值。

如有任何帮助,我们将不胜感激。

在表单中的所有元素中循环

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