Results 1 to 4 of 4

Thread: [RESOLVED] Help with combobox / web browser controls

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    159

    Resolved [RESOLVED] Help with combobox / web browser controls

    I'm trying to play around with combo boxes and trying to figure out how to navigate to a certain url in webbrowser1 depending on the text selected in combobox1. Can anyone tell me what I'm doing wrong with this?

    VB Code:
    1. Imports System.Windows.Forms
    2. Imports System.Net
    3. Imports System.IO
    4. Imports System.Text
    5. Public Class Form1
    6.     Class MyList
    7.         Public MyId As Integer
    8.         Public MyStr As String
    9.  
    10.         Public Sub New(ByVal s As String, ByVal i As Integer)
    11.             MyStr = s
    12.             MyId = i
    13.         End Sub
    14.  
    15.         '//VB.net will decide DisplayMember using this function
    16.         Public Overrides Function ToString() As String
    17.             ToString = MyStr
    18.         End Function
    19.     End Class
    20.  
    21.     Sub LoadComboBox()
    22.         ComboBox1.Items.Add(New MyList("Learn Webmail", 1))
    23.         ComboBox1.Items.Add(New MyList("Learn File Manager", 2))
    24.         ComboBox1.Items.Add(New MyList("Learn Redirects", 3))
    25.         ComboBox1.Items.Add(New MyList("Learn Fantastico", 4))
    26.         ComboBox1.Items.Add(New MyList("Learn Error Pages", 5))
    27.         ComboBox1.Items.Add(New MyList("Learn MySQL", 6))
    28.         ComboBox1.Items.Add(New MyList("Learn Addon Domains", 7))
    29.         ComboBox1.Items.Add(New MyList("Learn To Protect Directories", 8))
    30.         ComboBox1.Items.Add(New MyList("Learn Subdomains", 9))
    31.         ComboBox1.Items.Add(New MyList("Learn Statistics", 10))
    32.         ComboBox1.Items.Add(New MyList("Learn FrontPage Setup, FTP Access, And Change Passwords", 11))
    33.         ComboBox1.Items.Add(New MyList("Learn Domain Registration", 12))
    34.         ComboBox1.Items.Add(New MyList("Learn Web Hosting Setup", 13))
    35.         ComboBox1.Items.Add(New MyList("Using CPanel To Sell Resell Rights Products Part 1", 14))
    36.         ComboBox1.Items.Add(New MyList("Using CPanel To Sell Resell Rights Products Part 2", 15))
    37.         ComboBox1.Items.Add(New MyList("Using CPanel To Sell Resell Rights Products Part 3", 16))
    38.         ComboBox1.Items.Add(New MyList("Using CPanel To Sell Resell Rights Products Part 4", 17))
    39.         ComboBox1.Items.Add(New MyList("Using CPanel To Install A PHP/MySQL Script Part 1", 18))
    40.         ComboBox1.Items.Add(New MyList("Using CPanel To Install A PHP/MySQL Script Part 2", 19))
    41.         ComboBox1.Items.Add(New MyList("Using CPanel To Install A PHP/MySQL Script Part 3", 20))
    42.         ComboBox1.SelectedIndex = 0
    43.     End Sub
    44.  
    45.     Sub ShowSelected()
    46.         MsgBox("Text=" & ComboBox1.SelectedItem.Mystr & " ItemData=" & CStr(ComboBox1.SelectedItem.MyId))
    47.     End Sub
    48.  
    49.  
    50.     Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
    51.  
    52.     End Sub
    53.  
    54.     Private Sub ComboBox1_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedValueChanged
    55.  
    56.     End Sub
    57.  
    58.     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    59.         LoadComboBox()
    60.         If ComboBox1.SelectedItem = 1 Then
    61.             WebBrowser1.Navigate("urlhere")
    62.         End If
    63.     End Sub
    64.  
    65. End Class

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Help with combobox / web browser controls

    Instead of this:
    Code:
    If ComboBox1.SelectedItem = 1 Then
                WebBrowser1.Navigate("urlhere")
    End If
    I think you either want to do this (if you are trying to navigate based on the selected index of the combobox, which starts at 0):
    Code:
    If ComboBox1.SelectedIndex = 1 Then
                WebBrowser1.Navigate("urlhere")
    End If
    or you want to do this (if you want to use the number stored with each instance of your MyList class, which seems to start at 1) :
    Code:
    If DirectCast(ComboBox1.SelectedItem, Mylist).MyId = 1 Then
                WebBrowser1.Navigate("urlhere")
    End If
    Having said that, I think what might be a better idea would be to store the URL with your class instances - then you dont need to use an IF statement to check every possible selected index or ID. So you would add another property to your class called URL or something and would populate that in the same way that you are currently populating the MyID and MyStr objects, then in your form load event instead of doing what you are currently doing (or what I suggested above) you would do something like this:
    Code:
    WebBrowser1.Navigate(DirectCast(ComboBox1.SelectedItem, MyList).Url)
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    159

    Re: Help with combobox / web browser controls

    Thank you Chris!
    Last edited by cmmorris1; Nov 6th, 2009 at 09:06 PM.

  4. #4
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [RESOLVED] Help with combobox / web browser controls

    no problem
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


Posting Permissions

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



Click Here to Expand Forum to Full Width