csharp United States
  • 13 years ago
    Hi there,

    I am trying to browse a website using MSHTML. On a webpage, I want to select an item in dropdown box. This is the code that I use to do it:
    HTMLSelectElement eventCat = (HTMLSelectElement) myDoc.all.item("EventCategory",0);
    eventCat.selectedIndex = 13;

    IHTMLElement eventCatElem = eventCat as IHTMLElement;
    IHTMLDocument4 doc = eventCatElem.document as IHTMLDocument4;
    object dummy = null;
    object eventObj=doc.CreateEventObject(ref dummy);
    HTMLSelectElementClass se = eventCat as HTMLSelectElementClass;

    eventCat.selectedIndex succesfully selected an item, but it doesn't fire the "onchange" event, so I added the next lines of code to fire the event but to no avail...
    Does anyone know how to solve this?
    Thanks in advanced.
  • 13 years ago

    I am very new to MSHTML and DOM.  I know this is a C# forum, but my problem may have a similar solution to the original posting by Irma.

    I am trying to write a test harness that automates actions to our product's web-based management server.  I am writing the code using Visual C++ 6.0.  I also have IE 6.0.* installed, so I presume I have the "latest" MSHTML.dll.

    I am able to do the login screen, and programmatically click on all the <a> anchor links.  I can also programmatically click on Button elements that I find.  However, I am having difficulty with no events firing when I change values for SelectOption elements, radio buttons, etc.  

    Here is an example of some HTML that I try to programmatically select and set:

    <SELECT VALIGN=TOP class='data' id=RecPerPage onchange='ChangeRecPerPage()'>
    <OPTION Selected= 10 VALUE=10>10</OPTION>
    <OPTION  VALUE=100>100</OPTION>
    <OPTION  VALUE=10000>All</OPTION>

    I am able to get the DOM object for the Select element and the Option elements.  I can set the Option, and the Web Browser does show that I set the option.  Here is a code excerpt:

    SetOptionElement(IHTMLSelectElement& prSelect, const char pOptionName)

           BSTR text;
           CString text_str = text;

           if(textstr.Find(pOptionName) >= 0)
               BSTR value;
               return true;

    However, the "ChangeRecPerPage()" function does not get called, so the frame does not refresh (with the new rows per page listing).  I did an Ethereal trace and found that no HTML was posted, as it happens when one manually selects an option in that Select.

    Does anybody have ideas about what is going wrong?  I would think this is a very basic type of operation to automate this type of input element setting.


  • 12 years ago

    I have solved the problem! by using the dom document to fire the event and not the element.

    It works!!

    Thanks for your example... you can mail me on marc.platvoet my gmail account.


  • 12 years ago

    Can you attach the final code version?

    Also I have a problem to fire link using MSHTML .

    I am using this code but from some reason this doesn’t work.


    public static bool FireEvent(mshtml.IHTMLElement elt, string eventName)


       mshtml.IHTMLDocument4 docEv = elt.document as mshtml.IHTMLDocument4;

       object dummy = null;

       object oEvt = docEv.CreateEventObject(ref dummy);

        mshtml.IHTMLEventObj2 evt = (mshtml.IHTMLEventObj2)oEvt;

        evt.button = 1;

        return docEv.FireEvent(eventName,ref oEvt);


    Do you know what is a problem.

  • 8 years ago


    Facing similar issue. Can you modify your original code for dom document to fire the event and post it?

Post a reply

Enter your message below

Sign in or Join us (it's free).


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“There's no test like production” - Anon