?
Solved

MSHTML Drop Down

Posted on 2004-08-23
3
Medium Priority
?
2,094 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
[X]
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
  • 2
3 Comments
 

Accepted Solution

by:
aprenot earned 1500 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

777 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