Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Beginning VB 6 For coders who are new to Visual Basic, working in VB version 6 (not .NET).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 26th, 2006, 06:38 AM
Registered User
 
Join Date: Sep 2006
Location: Karachi, , Pakistan.
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default List Box VB

Okie... i am trying to polish my skills before i shall go ahead wid the project... here is the imaginery scenario...

Objective:
To build a user interface capable of adding and deleting Country Names and their respective City Names.

Description:
A little program is being Built to store customer's Data. Since the company is having worldwide network of customers we have placed two Combo boxes one for each Country and City Name so as the user can select instead of typing it everytime. However a provision to edit the countries and Cities list is necessary so there is a dedicated space allocated for this task where I have putted following controls:


Controls:
2 Text Boxes...1 Each for typing Country and City Names
2 List Boxes... 1 for showing Countries and 1 for showing the respective cities for the selected Country
4 Command Buttons ... 2 set of Add & remove

Problem:
Since i am a newbie, i am totally confused as how this sub-category thingy has to be done... I mean for instance if i have to add U.S.A. its pretty easy to type following block of code

Private Sub commandbutton1_Click()
Listbox1.additem textbox1.text
End Sub


But what if i want to add washigton cuz that has to be added as a sub iem of U.S.A i.e. only to be shown when U.S.A is selected... though i can simply add a huge block of If-Then but that will only work for preset items and if the user want to add another city then I will have to alter the If-Then block again


Learn from the mistakes of others. You can't live long enough to make them all yourself
Reply With Quote
  #2 (permalink)  
Old September 26th, 2006, 10:06 AM
Friend of Wrox
 
Join Date: May 2006
Location: San Diego, CA, USA.
Posts: 643
Thanks: 0
Thanked 0 Times in 0 Posts
Default

There are numerous ways to do this.

One way is with collections (the Collection object in VB).

In this scenario you could create a class for Country, and a class for City, as well as a collection class of Countries and a collection class of Cities. The class for Country would have 2 members - one for the name of the country, and the other which is the Cities collection class.The items in the collections would be keyed on the name (either Country or City depending on the collection) so you could easily access them when a user clicks on an item in the list box.

Confused? Well, this is actually very simple, and VB provides a mechanism for creating collection classes that works extremely well.
There are numerous examples of creating collection classes to do this sort of thing. A good book on the subject is Daniel Applemans Dan Appleman's Developing Com/Activex Components With Visual Basic 6 which you can get via Half.com or Amazon for about 3 bucks US.

However... at the newbie level, you might also want to try to figure out a way to implement this using an array.

Well. Welcome to programming. You are going to find that there are 1000 right ways to do anything.


Woody Z http://www.learntoprogramnow.com
Reply With Quote
  #3 (permalink)  
Old September 27th, 2006, 02:37 PM
Friend of Wrox
 
Join Date: Nov 2004
Location: Port Orchard, WA, USA.
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Since you almost certainly are working with a database, why not create a 1:many table set with those being Country:Cities?

For the list boxes, you could set the 2nd one to the right list by changing its rowsource to SELECT CITY FROM CITIES WHERE CTRY_ID = <fill in val here>.

Having recieved an entry in textbox1 that cannot be found in the Countries table, use database code to add the name to the table. With the listbox using SELECT CTRY FROM COUNTRIES, adding the new country to the table will add it to the listbox.

The same goes for the cities part: if that which is typed has no match, add it to the table.

This way, for the customer's data, rather than adding the spelled out country and city, you add the IDs pointing to the appropriate rows. If a country has been misspelled you change that entry in the Countries table, and every customer form that country now has been "upgraded" to have the country name spelled properly.
Reply With Quote
  #4 (permalink)  
Old September 27th, 2006, 04:41 PM
Friend of Wrox
 
Join Date: May 2006
Location: San Diego, CA, USA.
Posts: 643
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I would suggest that this looks more like a student project that is not likely to involve using a database. It seems to me that the main purpose of this sort of project is to teach the student how to think about solutions and the kinds of things that you can do with code to manage this type of relationship.

Woody Z http://www.learntoprogramnow.com
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Grab Values From List Box into Text Box phungleon VB How-To 2 June 19th, 2008 10:33 PM
multi-column list box values moved to 2nd list box sbmvr Access VBA 1 May 14th, 2007 01:58 PM
vb.net list box --- need asap!! Sandi77 BOOK: Beginning Access VBA 0 February 26th, 2006 09:01 AM
Populate List Box by Combo Box Selection mmcdonal Access 2 June 15th, 2004 12:08 PM
Search using drop down list box and a text box tcasp Classic ASP Basics 1 July 31st, 2003 02:58 PM



All times are GMT -4. The time now is 02:50 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.