Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2132
  • Last Modified:

MSHTML Drop Down

I'm creating a C# application that will launch a url in Internet Explorer using both the shdocvw and microsoft.mshtml references. After the page loads, I need to automatically enter data into form elements, then press the continue button on the screen. Currently, I'm able to load the page, and populate text fields with data. The program throws a cast exception when I try to set the value for a drop down list. I've looked through the object browser in the mshtml reference, and I'm not able to find anything that looks like it would help me.

I have attached the code below.

using System.Runtime.InteropServices;
using SHDocVw;
using System;
using mshtml;

namespace viClient{
      public class Connector
      {
            public Connector()
            {
            }
            public static void Main(string[] args)
            {
                  object o = null;
                  InternetExplorerClass ie = null;

                  ie = new InternetExplorerClass();      
                  IWebBrowserApp wb = (IWebBrowserApp) ie;
                  wb.Navigate("http://mypage.domain.com?var1=true&var2=1",ref o,ref o,ref o, ref o);

                  while (wb.Busy);
                  HTMLDocument wd = (HTMLDocument)wb.Document;

                  Object input = wd.all.item("SearchCriteria",0);
                  if (input == null )
                        Console.WriteLine("No form SearchCriteria Exists!");
                  // Account Form Element
                  HTMLInputElement polNum = (HTMLInputElement)wd.all.item("Account ",0);
                  Account.value="8736425";
                  // Date of Sale Form Element
                  HTMLInputElement SaleMonth = (HTMLInputElement)wd.all.item("SaleMonth",0);
                  SaleMonth.value="04";
                  // Occurence Form Element
                  HTMLInputElement Occurrence = (HTMLInputElement)wd.all.item("Occurrence",0);
                  Occurrence.value="01";
                  HTMLInputElement Continue = (HTMLInputElement)wd.all.item("theContinueButton",0);
                  Continue.click();                        
            }
      }
0
MischiefMadness
Asked:
MischiefMadness
  • 2
1 Solution
 
aprenotCommented:
The invalid cast is the fact that you are casting a select element (drop-down list) to an input element.  Try this:

HTMLSelectElement SaleMonth = (HTMLSelectElement)wd.all.item("SaleMonth",0);
SaleMonth.value="04";

If this does not work, you will need to iterate through the Select Element's item collection and set the correct Option Element to be selected:

HTMLSelectElement SaleMonth = (HTMLSelectElement)wd.all.item("SaleMonth",0);
for ( int i = 0; i < SaleMonth.Length; i++ )
{
        HTMLOptionElement SaleMonthOption = (HTMLOptionElement)SaleMonth.item("", i);
        if ( SaleMonthOption.text == "December" )
        {
               SaleMonthOption.selected = true;
               break;
        }
}

That should do it for you.
0
 
MischiefMadnessAuthor Commented:
Thanks for the assistance. I somehow missed the HTMLSelectElement class in the object browser. The code you provided, however, was not what I had to implement. The code I used was

HTMLSelectElement SaleMonth = (HTMLSelectElement)wd.all.item("SaleMonth",0);
SaleMonth.selectedIndex=12; // where 12 = December.

I'm going to award 350 points for pointing me in the right direction. I feel this is fair, but I can debate it if there is an issue.

Thanks!
0
 
MischiefMadnessAuthor Commented:
I'm in a giving mood, take the 500! Thanks again.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now