Page 1 of 1

Need help with code

Posted: Tue Oct 23, 2012 1:14 am
by skyteam
Hi guys I am have some issues with a code. The code is retreving a peice of code from a web site

I am using the code to retrive the output
Code: Select all
textbox.text = WebBrowser2.Document.GetElementById("OutputHistory").InnerHtml


site code
Code: Select all
 <td width="25%">
            <select size="4" name="OutputHistory" id="OutputHistory" style="height:300px;width:100%;">
	<option value=""></option>
	<option value=""></option>
	<option value=""></option>
	<option value=""></option>
	<option value="">C0</option>
	<option value=""></option>
	<option value=""></option>
	<option value=""></option>
	<option selected="selected" value="A1">A1</option>
now i want to retreve just the C0 in the code to a text box in visual
now the problem os that the C0 can be diferent things like a,b,c,d,e,f and tose are from 1 to 20

It would be nice if the text box actuly only showed the selected one in this example the textbox.text would be equal to A1

Re: eed help with code

Posted: Tue Oct 23, 2012 1:29 am
by comathi
Try this code:
Code: Select all
Dim outputhistory As HtmlElement = WebBrowser1.Document.GetElementById("OutputHistory")
        Dim optionList As HtmlElementCollection = outputhistory.GetElementsByTagName("option")
        TextBox1.Text = optionList(8).InnerText
This will get the text in the 9th <option> tag (in this case, A1). However, if you want to get C0, use this:
Code: Select all
Dim outputhistory As HtmlElement = WebBrowser1.Document.GetElementById("OutputHistory")
        Dim optionList As HtmlElementCollection = outputhistory.GetElementsByTagName("option")
        TextBox1.Text = optionList(4).InnerText

Re: eed help with code

Posted: Tue Oct 23, 2012 4:07 am
by skyteam
this works grate now if i have more the one i want it to always get the selected one any way of doing that? hey cool tu est francais = ) moi aussi je vien du quebec

Re: Need help with code

Posted: Tue Oct 23, 2012 10:41 am
by comathi
Sorry, I don't understand what you mean by that. The first bit of code I gave you will return the value of the selected item, as long as there are 9 <option> tags and that the selected one is last.

Do you mean the number of tags will vary?

Re: Need help with code

Posted: Tue Oct 23, 2012 9:47 pm
by mandai
skyteam wrote:
i want it to always get the selected one any way of doing that?
You can add this code so it gets the selected text:
Code: Select all
        Dim strIndex As String = outputhistory.GetAttribute("selectedIndex")
        Dim index As Integer = Integer.Parse(strIndex)

        Dim selectedText As String = optionList(index).InnerText
        TextBox1.Text = selectedText