Solved

How to set "Selected" status of a radio in WebBrowser2 Ctrl?

Posted on 2001-08-10
2
316 Views
Last Modified: 2013-11-20
Hi experts:
    I want to set a HTML radio to the status of selected, the html code is here:
    <input type="radio" name="myradio" value="Value1">Radio Box Value1
    <input type="radio" name="myradio" value="value2">Radio Box Value1

You can see that the two radio is unselected. I want to press a button in my program and select the radio which value="value2".
Please note:
1 They have same name because they are in one group.
2 I can set the *value* of text input box, but the "selected" status is not the value of radio. When a radio is selected, the html code will be:
    <input type="radio" name="myradio" value="Value1" selected>
3 I also want to know how to check/uncheck a check box in WebBrowser2 ctrl.

The following can set value of text input box of CWebBrowser2 m_web;

    IHTMLElementCollection *objAllElement=NULL;
    IHTMLDocument2 *objDocument=NULL;
    CComPtr<IDispatch>pDisp;
    IHTMLAnchorElement *objAnchor=NULL;
    CString strUrl;
    strUrl=m_ctrlWeb.GetLocationURL();
    if(strUrl.IsEmpty())
        return;
    objDocument=(IHTMLDocument2 *)m_ctrlWeb.GetDocument();
    objDocument->get_all(&objAllElement);
    objAllElement->item(COleVariant("Name of Textbox"),COleVariant((long)0),&pDisp);
    CComQIPtr<IHTMLInputTextElement, &IID_IHTMLInputTextElement>pElement;
    //If it is a radio box , then use IHTMLOptionButtonEelment here
    pElement=pDisp;
    pElement->put_value(CString("Value of Textbox").AllocSysString());
0
Comment
Question by:Feya
2 Comments
 
LVL 49

Accepted Solution

by:
DanRollins earned 100 total points
ID: 6375205
I think that by 'select' you want to 'put a dot into' (that is make it the selected option).  One must use the set_checked() member of IHTMLOptionButtonElement in order to do this.

<Input type=radio value='1' name='MyRadioBtn' CHECKED>Opt1 description<br>
<Input type=radio value='2' name='MyRadioBtn'>Opt2 description<br>
<Input type=radio value='3' name='MyRadioBtn'>Opt3 description<br>

When you use objAllElement->item(...) you will get back a collection of all radio btns with that name UNLESS you specify an index as the second parameter.  To wit:

#include <Mshtml.h>
#include <atlbase.h>

void CMyDlg::OnButton1()
{
    IHTMLElementCollection *objAllElement=NULL;
    IHTMLDocument2 *objDocument=NULL;
    CComPtr<IDispatch>pDisp;

    objDocument=(IHTMLDocument2 *)m_ctlBrowser.GetDocument();
    objDocument->get_all(&objAllElement);

    COleVariant vBtnIdx((long)2); // actually the third button (0 is first)
    objAllElement->item(COleVariant("MyRadioBtn"),vBtnIdx,&pDisp); // <<< the key

    CComQIPtr<IHTMLOptionButtonElement, &IID_IHTMLOptionButtonElement>pElement;
    pElement=pDisp; // need error checking here
    pElement->put_checked(true);
}
=-=-=-=-=-=-=-
Use the same logic with checkboxes, but use the IHTMLInputElement interface.

You could also use the id='Opt1' syntax to give each radio button a unique ID, then you can access each one individually without needing to specify an index.

-- Dan
0
 

Author Comment

by:Feya
ID: 6375251
Excellent Answer!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
no14 challenge 14 62
modThree challenge 4 79
pre4 challenge 19 94
"Black Box" Testing of Control System Software 2 25
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now