Combobox Question

daveofgv

Well-known member
Joined
Sep 17, 2008
Messages
218
Location
Dallas, TX
Programming Experience
1-3
Hello all

I have a combobox..... When a user clicks on the list inside the combobox my webbrowser opens to the selected items website.

Does anyone know how to make the combobox go back to the original text of the combobox?

Example: Combobox list
---- Select Item ----
Royals
Mets
Yankees
Red Sox
White Sox

When the user see's the screen the ---- Select Item ---- is visible. When a user clicks a teams name it opens the teams website and the name stays in the box. I want it to go back to ---- Select Item ---- immediately after they click a list item.

Thanks in advanced

daveofgv
 
Dosn't work

I put it under each statement and also on top. Everytime I select an item it automatically jumps back to the first item and opens it's webpage instead of the item I selected.

VB.NET:
  Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
        ComboBox2.SelectedIndex = 0I

        If ComboBox2.Text = "123-Email" Then
            internetbrowser.Show()
            CType(internetbrowser.TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("www.123-email.com")

        End If
        If ComboBox2.Text = "AOL" Then
            internetbrowser.Show()
            CType(internetbrowser.TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("https://my.screenname.aol.com/_cqr/login/login.psp?sitedomain=sns.webmail.aol.com&lang=en&locale=us&authLev=0&siteState=ver%3a4|rt%3aSTANDARD|ac%3aWS|at%3aSNS|ld%3awebmail.aol.com|uv%3aAOL|lc%3aen-us|mt%3aAOL|snt%3aScreenName&offerId=mail-first-en-us&seamless=novl&xchk=false")


        End If
        If ComboBox2.Text = "AmpliMail" Then
            internetbrowser.Show()
            CType(internetbrowser.TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("www.amplimail.com")

        End If
        If ComboBox2.Text = "AT&T" Then
            internetbrowser.Show()
            CType(internetbrowser.TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("http://webmail.att.net/")

        End If
        If ComboBox2.Text = "Bluebottle Freemail" Then
            internetbrowser.Show()
            CType(internetbrowser.TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("www.bluebottle.com")

        End If
        If ComboBox2.Text = "Fastmail" Then
            internetbrowser.Show()
            CType(internetbrowser.TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("http://www.fastmail.fm/")

        End If
        If ComboBox2.Text = "Gmail" Then
            internetbrowser.Show()
            CType(internetbrowser.TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("www.gmail.com")

        End If

        If ComboBox2.Text = "MSN HotMail" Then
            internetbrowser.Show()
            CType(internetbrowser.TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("www.hotmail.com")

        End If

        If ComboBox2.Text = "Hushmail" Then
            internetbrowser.Show()
            CType(internetbrowser.TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("www.hushmail.com")

        End If
        If ComboBox2.Text = "Inbox.com" Then
            internetbrowser.Show()
            CType(internetbrowser.TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("www.inbox.com")


        End If
        If ComboBox2.Text = "Lycos Mail" Then
            internetbrowser.Show()
            CType(internetbrowser.TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("http://mail.lycos.com/lycos/Index.lycos")

        End If
        If ComboBox2.Text = "Mail2World" Then
            internetbrowser.Show()
            CType(internetbrowser.TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("http://www.mail2world.com/")

        End If
        If ComboBox2.Text = "Mailpuppy" Then
            internetbrowser.Show()
            CType(internetbrowser.TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("www.mailpuppy.com")

        End If
        If ComboBox2.Text = "MyRealBox" Then
            internetbrowser.Show()
            CType(internetbrowser.TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("www.myrealbox.com")

        End If
        If ComboBox2.Text = "MyWay" Then
            internetbrowser.Show()
            CType(internetbrowser.TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("www.myway.com")

        End If

        If ComboBox2.Text = "SoftHome" Then
            internetbrowser.Show()
            CType(internetbrowser.TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("www.softhome.net")

        End If

        If ComboBox2.Text = "Yahoo Mail" Then
            internetbrowser.Show()
            CType(internetbrowser.TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("https://login.yahoo.com/config/login_verify2?&.src=ym")

        End If


    End Sub

Any suggestions?

Thanks in advanced

daveofgv
 
That's because first thing in the event you change it to 0, which means whatever was selected isn't anymore and you also will want a check for SelectedIndex <> 0 too:
VB.NET:
    Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
        If ComboBox2.SelectedIndex <> 0I Then
            internetbrowser.Show()
            Select Case ComboBox2.Text
                Case "123-Email" : CType(internetbrowser.TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("www.123-email.com")
                Case "AOL" : CType(internetbrowser.TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("https://my.screenname.aol.com/_cqr/login/login.psp?sitedomain=sns.webmail.aol.com&lang=en&locale=us&authLev=0&siteState=ver%3a4|rt%3aSTANDARD|ac%3aWS|at%3aSNS|ld%3awebmail.aol.com|uv%3aAOL|lc%3aen-us|mt%3aAOL|snt%3aScreenName&offerId=mail-first-en-us&seamless=novl&xchk=false")
                Case "AmpliMail" : CType(internetbrowser.TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("www.amplimail.com")
                Case "AT&T" : CType(internetbrowser.TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("http://webmail.att.net/")
                Case "Bluebottle Freemail" : CType(internetbrowser.TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("www.bluebottle.com")
                Case "Fastmail" : CType(internetbrowser.TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("http://www.fastmail.fm/")
                Case "Gmail" : CType(internetbrowser.TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("www.gmail.com")
                Case "MSN HotMail" : CType(internetbrowser.TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("www.hotmail.com")
                Case "Hushmail" : CType(internetbrowser.TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("www.hushmail.com")
                Case "Inbox.com" : CType(internetbrowser.TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("www.inbox.com")
                Case "Lycos Mail" : CType(internetbrowser.TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("http://mail.lycos.com/lycos/Index.lycos")
                Case "Mail2World" : CType(internetbrowser.TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("http://www.mail2world.com/")
                Case "Mailpuppy" : CType(internetbrowser.TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("www.mailpuppy.com")
                Case "MyRealBox" : CType(internetbrowser.TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("www.myrealbox.com")
                Case "MyWay" : CType(internetbrowser.TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("www.myway.com")
                Case "SoftHome" : CType(internetbrowser.TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("www.softhome.net")
                Case "Yahoo Mail" : CType(internetbrowser.TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("https://login.yahoo.com/config/login_verify2?&.src=ym")
            End Select
            ComboBox2.SelectedIndex = 0I
	End If
    End Sub
I also shortened the code by a lot too
 
JuggaloBrotha,

You sure went above and beyond. Thank you so much. I never thought of using select case. For some reason I have always been a if...then man.

Thank you again and people like you make vb.net world fun to work in....


daveofgv
 
Last edited by a moderator:
I might suggest a Dictionary(Of String, String) to reduce some of the duplicate code.

VB.NET:
Private mailSites As New Dictionary(Of String, String)

VB.NET:
	Private Sub InitializeMailSites()

		With mailSites
			.Add("123-Email", "www.123-email.com")
			.Add("AmpliMail", "www.amplimail.com")
			'...etc
		End With

	End Sub

VB.NET:
	Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
	Handles ComboBox2.SelectedIndexChanged

		If ComboBox2.SelectedIndex <> 0 Then
			internetbrowser.Show()
			CType(internetbrowser.TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate(mailSites(ComboBox2.Text))
			ComboBox1.SelectedIndex = 0
		End If

	End Sub
 
or using a variable, even if all lines are copy-paste it is also about readability:
VB.NET:
Dim url As String
Select Case ComboBox2.Text
    Case "123-Email" : url = "www.123-email.com"
    Case "AOL" : url = "https://my.screenname.aol.com/blah blah"
    Case "AmpliMail" : url = "www.amplimail.com"
    Case "AT&T" : url = "http://webmail.att.net/"
    Case "Bluebottle Freemail" : url = "www.bluebottle.com"
    Case "Fastmail" : url = "http://www.fastmail.fm/"
    Case "Gmail" : url = "www.gmail.com"
    Case "MSN HotMail" : url = "www.hotmail.com"
    Case "Hushmail" : url = "www.hushmail.com"
    Case "Inbox.com" : url = "www.inbox.com"
    Case "Lycos Mail" : url = "http://mail.lycos.com/lycos/Index.lycos"
    Case "Mail2World" : url = "http://www.mail2world.com/"
    Case "Mailpuppy" : url = "www.mailpuppy.com"
    Case "MyRealBox" : url = "www.myrealbox.com"
    Case "MyWay" : url = "www.myway.com"
    Case "SoftHome" : url = "www.softhome.net"
    Case "Yahoo Mail" : url = "https://login.yahoo.com/config/login_verify2?&.src=ym"
End Select
CType(internetbrowser.TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate(url)
 
or using a variable, even if all lines are copy-paste it is also about readability:

Good point. If a Select Case statement is more readable than a Dictionary(Of String, String) then by all means use that.

Personally I like the Dictionary in this situation since I know that when new features are requested I already have the collection so I don't need to change existing functionality if I need to know how many mail sites I'm keeping track of or iterate through them for display/reporting/etc.

I daveofgv does choose the Select Case I would suggest extracting the statement to a method returning the url for maintainability and reuse.

VB.NET:
Dim url As String = GetUrlFromSiteName(ComboBox2.Text)
CType(internetbrowser.TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate(url)
 
Thank you both for the great code ideas. I used the select case since I don't know about Dictionary(Of String, String). Since I was just introduced to it I will be learning it as much as I can.

It's funny how (even though it's all VB.NET) every programmer has different ways to do it and there are so many variations of coding. lol

Thank you to the both of you and it looks like I have to do a lot more googling to learn about Dictionary(Of String, String)......


Once again thank you

daveofgv
 
Good point. If a Select Case statement is more readable than a Dictionary(Of String, String) then by all means use that.
I would perhaps also use a Dictionary, the combo could also be filled from same list like this:
VB.NET:
theCombo.DataSource = mailSites.Keys.ToArray
The variable suggestion was more of an alternative to the repetetive code to lookup the browser control within the Select Cases.
 
I would perhaps also use a Dictionary, the combo could also be filled from same list like this:
VB.NET:
theCombo.DataSource = mailSites.Keys.ToArray
The variable suggestion was more of an alternative to the repetetive code to lookup the browser control within the Select Cases.

Naturally, you'd have to add an item of:

myDict("--Select Site--") = Nothing


dave, note that this combo might be really annoying to a user who uses the keyboard to tab to the control, then press the DOWN cursor key on the keyboard to scan through the box, or the first letter of the entry. Don't assume that everyone uses a combo like "Click Down Arrow, Scroll List, Find Item, Click Item".

I'd delete your program, because combos that do disruptive things when you simply change the setting are just too annoying
 
So is combobox's just no good whatsoever?

Combos are supposed to be used for:

Up to ~40 items (prefer 20)
Preferably items that start with a different letter
Do not cause a disruptive change of state when an option is chosen


You have to step outside of the view you have on how you use a computer; if you always use a mouse for everything you *must* consider that there are people out there who predominantly use a keyboard. Forcing them to use a computer in a certain way will make them resent using it. As noted, I like to navigate combos using the keyboard; your program would annoy me so much, i'd delete it
 
Last edited:
Thanks for all the info.

this program only has 6 - 8 items that the user can select from. The only reason why I chose to make the --- Select Item --- visible after the user clicks is that the user will only be on the computer for less then 10 minutes and I don't want the last item selected in the box after the user leaves.

I do appreciate all the info and I wrote it down so I know for next time.

Sometimes I wish I had 1/10 the knowledge of some people on this forum lol.

daveofgv
 
Back
Top