VS 2008 [RESOLVED] Combobox - Website problem.-VBForums
Results 1 to 7 of 7

Thread: [RESOLVED] Combobox - Website problem.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2012
    Posts
    103

    Resolved [RESOLVED] Combobox - Website problem.

    Hey.

    I want my program to copy all the options in a combobox. and paste it into ComboBox2 in my program.

    Here is the combobox i want to copy. http://www.itslearning.com/welcome.aspx

    After the user has selected an option from the box, and pressed button1, the program should enter the value of combobox1 into the combobox at the website.

    I'm ofcorse doing all this using WebBrowser1.

    Can someone please give me a code i can use. I'm a noob and need help.

    Thanks!

  2. #2
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    18,713

    Re: Combobox - Website problem.

    try this:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         'fill combobox
    5.         If WebBrowser1.ReadyState <> WebBrowserReadyState.Complete Then Return
    6.         Dim element As HtmlElement = WebBrowser1.Document.GetElementById("ctl00_ContentPlaceHolder1_LoginSection1_ChooseSite_site_input")
    7.         Dim options() As String = element.GetElementsByTagName("option").Cast(Of HtmlElement).Select(Function(el) el.InnerText).ToArray
    8.         ComboBox1.Items.AddRange(options)
    9.     End Sub
    10.  
    11.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    12.         'open website in wb
    13.         WebBrowser1.Navigate("http://www.itslearning.com/welcome.aspx")
    14.     End Sub
    15.  
    16.     Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    17.         'select combobox item in webpage
    18.         If ComboBox1.SelectedIndex <> -1 Then
    19.             Dim element As HtmlElement = WebBrowser1.Document.GetElementById("ctl00_ContentPlaceHolder1_LoginSection1_ChooseSite_site_input")
    20.             Dim optionsElement As HtmlElement = element.GetElementsByTagName("option").Cast(Of HtmlElement).First(Function(el) el.InnerText = ComboBox1.Text)
    21.             optionsElement.SetAttribute("selected", "selected")
    22.         End If
    23.     End Sub
    24.  
    25. End Class

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2012
    Posts
    103

    Re: Combobox - Website problem.

    Doesn't seem to work... I don't get any error messages, but the combox1 doesn't seem to load the options.

    I press combobox1 (When in debug mode) and there only comes a tiny rectangle down with nothing in it... Where's all the options?? Please help

  4. #4
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    18,713

    Re: Combobox - Website problem.

    can you post the exact code you're using?

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2012
    Posts
    103

    Re: Combobox - Website problem.

    Quote Originally Posted by .paul. View Post
    can you post the exact code you're using?
    Sure. Here you go. (Note: In my last post i said combobox1. It's actually combobox2)

    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dialog1.ShowDialog()
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'open website in wb
            WebBrowser1.Navigate("http://www.itslearning.com/welcome.aspx")
            'fill combobox
            If WebBrowser1.ReadyState <> WebBrowserReadyState.Complete Then Return
            Dim element As HtmlElement = WebBrowser1.Document.GetElementById("ctl00_ContentPlaceHolder1_LoginSection1_ChooseSite_site_input")
            Dim options() As String = element.GetElementsByTagName("option").Cast(Of HtmlElement).Select(Function(el) el.InnerText).ToArray
            ComboBox2.Items.AddRange(options)
        End Sub
    
        Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
            'select combobox item in webpage
            If ComboBox2.SelectedIndex <> -1 Then
                Dim element As HtmlElement = WebBrowser1.Document.GetElementById("ctl00_ContentPlaceHolder1_LoginSection1_ChooseSite_site_input")
                Dim optionsElement As HtmlElement = element.GetElementsByTagName("option").Cast(Of HtmlElement).First(Function(el) el.InnerText = ComboBox2.Text)
                optionsElement.SetAttribute("selected", "selected")
            End If
        End Sub
    End Class

  6. #6
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    18,713

    Re: Combobox - Website problem.

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         Dialog1.ShowDialog()
    5.     End Sub
    6.  
    7.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    8.         'open website in wb
    9.         WebBrowser1.Navigate("http://www.itslearning.com/welcome.aspx")
    10.     End Sub
    11.  
    12.     Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
    13.         'select combobox item in webpage
    14.         If ComboBox2.SelectedIndex <> -1 Then
    15.             Dim element As HtmlElement = WebBrowser1.Document.GetElementById("ctl00_ContentPlaceHolder1_LoginSection1_ChooseSite_site_input")
    16.             Dim optionsElement As HtmlElement = element.GetElementsByTagName("option").Cast(Of HtmlElement).First(Function(el) el.InnerText = ComboBox2.Text)
    17.             optionsElement.SetAttribute("selected", "selected")
    18.         End If
    19.     End Sub
    20.  
    21.     Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
    22.         'fill combobox
    23.         If WebBrowser1.ReadyState <> WebBrowserReadyState.Complete Then Return
    24.         Dim element As HtmlElement = WebBrowser1.Document.GetElementById("ctl00_ContentPlaceHolder1_LoginSection1_ChooseSite_site_input")
    25.         Dim options() As String = element.GetElementsByTagName("option").Cast(Of HtmlElement).Select(Function(el) el.InnerText).ToArray
    26.         ComboBox2.Items.AddRange(options)
    27.     End Sub
    28.  
    29. End Class

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jan 2012
    Posts
    103

    Re: Combobox - Website problem.

    Awesome! Thanks man!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.