Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

MSHTML Drop Down

Posted on 2004-08-23
3
2,032 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
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…

856 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