Home
Username:
Password:
Microsoft Visual Basic 2005 Tutorials

Lists / Combo Box Example




Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.


Learn More

Subtitles of the Movie

In this movie I will demonstrate an example of using a Combo Box to allow the user to select the name and then remove that name. However, sometimes the user will perform an action that will create a problem unless you specify a certain action is followed first. For example, to remove a name, you first need to select the name. So if we tried to ask Visual Basic to remove a name without first selecting a name, problems might result. The key to this operation is the selected index property. When a project is running and the user selects an item from the list, the index number of that item is stored in the selected index property of the List Box. You will recall that the index of the first item in the list is always zero. If no list item has been selected, then the selected index property is automatically set to negative one. You can use the selected index property to select an item in the list or to deselect all items in code. In this example, looking at our Combo Box and our items collection property, we recall that we have six names listed in the Combo Box through the string collection editor. As I run the program and then if I were to select an item such as Pam and click Remove Names, the name Pam is indeed removed. If, on the other hand, I do not make a selection and click Remove Names, I receive a message box that says you must first select a name to be removed. Now, this does not happen automatically and it has to be built into your code. So let's take a look at the code of the Remove Names Button. In here you see a simple if statement that is being used, but the trick here is to make use of the selected index property. You'll notice that the Names Combo Box selected index property is being compared to negative one. By comparing to see if the selected index is not equal to negative one, then what we're saying is if it's not equal to negative one, then that means that the user has indeed made a selection. That is, if they choose the third item, the selected index is two. Remember; zero, one, two. So therefore it's not equal to negative one. If it's not equal to negative one, then we want to go ahead and remove the item that they have selected, which in turn will be in the names combo.text property. If, on the other hand, the IF statement is false, which means it is equal to negative one, then the else of the IF statement takes over, which says to display a message box to display you must first, you must first select a name to be removed. Again, this else is only going to be in effect if the selected index is equal to negative one. And the only time the selected index is equal to negative one is if while the project is running, the user has not made a selection. So once again, if I don't make a selection and click Remove, the message box is indeed displayed because the else portion of the IF statement is now in effect.

Tutorial Information

Course: Microsoft Visual Basic 2005
Author: Arthur Lee
SKU: 33940
ISBN: 1-935320-10-6
Release Date: 2008-11-19
Duration: 7.5 hrs / 97 lessons
Work Files: Yes
Captions: Available on CD and Online University
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 98,729 Video Tutorials (23,265 free)
  • Video Available as Flash or QuickTime
  • Over 1026 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available