Link to home
Start Free TrialLog in
Avatar of inthedark
inthedarkFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Webbrowser MSHTML Problem

One step forward and 2 back.  I normally mainly do server side coding but I was asked to convert a Server based IIS DLL to run as a "detached from the internet" client. It all works well except:

getAttribute("Value") does not seem to work for <select> elements.

To reproduce the problem:

1) Create a new Standared EXE project.
2) Set a Project component: Microsoft Internet Controls.
3) Set a Project Reference to: Microsoft HTML Object Library.
4) Create a Webbrowser1 control on your form.

Paste the following code & run.

The click on the Submit button.

The code marked with ## does not return the value of the select box.

How can I change the code to find the the Select element value?

------Form Code Starts Here----------
Option Explicit


Dim WithEvents HFORM As MSHTML.HTMLFormElement

Private Sub Form_Load()

DoEvents

' Place a dummy page in the browser
WebBroweser1.Navigate "about:blank"
DoEvents

Dim m$

' create a HTML form with a Select box, Text Input box and submit button.
m$ = "<HTML><BODY><Form ID=""MyForm"" Name=""PRB"" Action="""" Method=""Post"">" _
    + "Change the select box to Item 1 then click Submit<BR>Select Box<Select ID=""IDV1"" Name=""V1"">" _
    + "<Option><Option>Item 1<Option Selected>Item 2</Select>" _
    + "<Input ID=""IDV2"" Type=""Text"" Name=""V2"" Value=""This value is ok"">" _
    + "<Input ID=""IDV3"" Type=""Submit"" Name=""V3"" Value=""Submit"">" _
    + "</Form></Body></html>"
   
' load the html into the browser
WebBroweser1.Document.write m$
DoEvents

' link the form element to that the Submit button can be trapped.

Set HFORM = WebBroweser1.Document.getElementById("MyForm")
End Sub

Private Function HFORM_onsubmit() As Boolean

Dim HELEMENT As MSHTML.HTMLGenericElement
Dim SELEL As MSHTML.HTMLSelectElement

' display the data from the form

Dim m$
m$ = "The values found were as follows:" + vbCrLf

Set SELEL = WebBroweser1.Document.getElementById("IDV1")

' The following line fails to get the value
' of the select box
m$ = m$ + "IDV1=" + SELEL.getAttribute("Value") + " Why is this value empty?" + vbCrLf ' ## This is strange
Set HELEMENT = WebBroweser1.Document.getElementById("IDV2")
m$ = m$ + "IDV2=" + HELEMENT.getAttribute("Value") + vbCrLf
Set HELEMENT = WebBroweser1.Document.getElementById("IDV3")
m$ = m$ + "IDV3=" + HELEMENT.getAttribute("Value") + vbCrLf
Set HELEMENT = Nothing
HFORM_onsubmit = False
MsgBox m$

End Function




ASKER CERTIFIED SOLUTION
Avatar of AzraSound
AzraSound
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of inthedark

ASKER

Thanks, it works:

m$ = m$ + "IDV1=" + SELEL.Options(SELEL.selectedIndex).Text
I am very new to DHTML, but I have done quite a bit of JavaScript.  I realy found that it was hard debugging the scripts, compared to VB. AzraSound what tool do you use for debugging your DHTML