Solved

MSHTML Drop Down

Posted on 2004-08-23
3
2,046 Views
Last Modified: 2008-01-09
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
Comment
Question by:MischiefMadness
  • 2
3 Comments
 

Accepted Solution

by:
aprenot earned 500 total points
ID: 11875397
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
 

Author Comment

by:MischiefMadness
ID: 11880569
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
 

Author Comment

by:MischiefMadness
ID: 11880577
I'm in a giving mood, take the 500! Thanks again.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How do I get the up & down arrow keys to navigate in an Autocomplete textbox list. 3 49
Amazon S3 .Net error 5 43
Visual Studio 2015 auto inserted code 12 71
c#, case, if 4 21
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question