Thursday, November 20, 2008

Fri Nov 21: Using combox boxes

Greetings, fellow code monkeez,

Today being Friday and all, I thought that we should keep things as simple yet as informative as possible. Plus, I wanted to introduce you all to a couple of extremely useful controls:

A) the PANEL

B) the combobox aka, "pulldown menu"

A panel is good for holding other controls together as if they functioned as one unit. Combo boxes are good for holding lists of items in a visible fashion, so you can see them and use them. We are going to make use of both today. I will show you how the first time around, then you will find some other uses for BOTH these controls

OK, lets launch into this, shall we?

Step A) start up a new project and call it PullDowns

Step B) Add a button to your new form, and set its text to say "Shaazaam!"

Step C) Next, add a Panel to your form. Make it as big as possible without overlapping the Button you just made. Set its background color to red or blue or green or something bright. Set its visibility to "False"

Step D) Add a webbrowser control onto the Panel, and resize it so that it takes up most of the room inside the panel.

Step E) drag a combobox onto the Panel, and set it just above the webbrowser control

Now to set some properties and add some simple code. Here are the next steps:

Step F) your ComboBox has a property called "ITEMS"; next to it is the word "Collections" followed by a little button with dots on it. Click on that button

Step G) This pops up a place where you can add items to your combobox, similar to the way you added items to your menus the other day. Our collections list is going to consist of webpage addresses, something like this:

http://www.thinkgeek.com/
http://www.bittorrent.com/
http://www.limewire.com/

By all means, set your website addresses to whatever you want, as long as they are school appropriate

Step H) Doubleclick on the combobox to get into its code. We are going to use the items in the combobox to direct the webbrowser control to display a webpage. The code for it is pretty simple:

MsgBox(ComboBox1.SelectedItem)
WebBrowser1.Navigate(New Uri(ComboBox1.SelectedItem))

Step I) Now, add this code to that button you made:
Panel1.Visible = True

RUN your program to verify that it works; I will be observing you through LanSchool. Once you have mastered this, I want you to be able to prove what you know by using both a combobox and a panel in some other way. PLEASE POST A DISCUSSION ON HOW YOU DID THIS.

Please upload this new mini project, then you may return to work on your project, which you will also upload before the class is done.

Cheers,
Mr. L

1 comment:

Ryan Norby said...

This code lets you use your own titles for the site in the control. For example, in my ComboBox control the list of data are as follows:
Google
Bittorrent
YouTube
Iron Maiden Offical Website

Public Class Form1
Dim websites() As String = {"http://www.google.com/", "http://www.bittorrent.com/", "http://www.youtube.com/", "http://www.ironmaiden.com/"}
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
WebBrowser1.Navigate(New Uri(websites(ComboBox1.SelectedIndex())))
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ComboBox1.SelectedIndex() = 0
End Sub
End Class