Home > Ask the Microsoft .Net Development Experts > Visual Studio .NET Questions & Answers > Using an array, how can I create a combo box that lists a dropdown menu for units of length?
Ask The Win Development Expert: Questions & Answers
EMAIL THIS

Using an array, how can I create a combo box that lists a dropdown menu for units of length?

Chris Sells EXPERT RESPONSE FROM: Chris Sells

Pose a Question
Other Win Development Categories
Meet all Win Development Experts
Become an Expert for this site


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


>
QUESTION POSED ON: 12 December 2002
I'm trying to create a combo box that lists a dropdown menu. The dropdown menu is going to contain units of length -- such as mm., cm., in., ft., mi., km., etc. How would I do this using an array? Thanks! Here's what I have thus far:

Private Sub Form_Load()
    Dim Length(6) As Integer
    Dim J As Integer
        Length(1) = Millimeter
        Length(2) = Centimeter
        Length(3) = M
        Length(4) = dm
        Length(5) = dm
        Length(6) = mm
            For J = Length(1) To Length(6)
                cboStart.AddItem J
            Next J
End Sub

>
You were very close. A couple of tweaks to your existing code yields something that does what you want, I think:

Private Sub Form1_Load(...) Handles MyBase.Loade
  Dim Length(6) As String
  Dim J As Integer
  Length(1) = "Millimeter"
  Length(2) = "Centimeter"
  Length(3) = "M"
  Length(4) = "cm"
  Length(5) = "dm"
  Length(6) = "mm"
  For J = 1 To 6
    cboStart.Items.Add(Length(J))
  Next J
  cboStart.SelectedIndex = 0
End Sub

If you wanted to get fancy, you could use an ArrayList and data binding, saving yourself the for-loop:

Private Sub Form1_Load(...) Handles MyBase.Load
  Dim Length As New ArrayList()
  Length.Add("Millimeter")
  Length.Add("Centimeter")
  Length.Add("M")
  Length.Add("cm")
  Length.Add("dm")
  Length.Add("mm")
  cboStart.DataSource = Length
End Sub

If you didn't already have the array of values lying around, you could also add them to the combo box directly:

Private Sub Form1_Load(...) Handles MyBase.Load
  cboStart.Items.Add("Millimeter")
  cboStart.Items.Add("Centimeter")
  cboStart.Items.Add("M")
  cboStart.Items.Add("cm")
  cboStart.Items.Add("dm")
  cboStart.Items.Add("mm")
  cboStart.SelectedIndex = 0
End Sub


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
Ask the Expert
Read data from parallel port
Escape from mailto:
In C#, how can I change the properties of controls on another form?
How do I add images in listview by using VB.NET?
What is the best technique for connecting to a database from VB.NET?
How do you use control collection in VB.NET?
Is ADO.NET record locking no longer an option?
How do I print a form in VB.NET?
How do I get my start page back?
How do I determine the type of a control on a Windows Form in VB.NET?

Visual Studio .NET
Load a DSL domain model instance file programmatically
Calling via command prompt from ASP.NET
On line-by-line analysis in VS .NET
Best approach for merging text files using arrays
Descriptions of symbols in the class view of a .NET solution
What's the best approach for enumerating network resources?
How to enable a line-by-line code walkthrough
Changing a C++ Makefile project to a Utility project
How can I configure Visual Studio.NET 2002 to use the .NET Framework 1.1?
Changing a datagrid column to read-only=false based on variable

Visual Studio .NET (Archive)
What could be causing this problem with my add New and delete methods?
Is there a .NET equivalent to netsend that I can use in VB.NET?
I'm getting a lot of flickering on my application
How can I make a VB6 network app available on the Internet using .NET?
How can I implement an FTP upload using VB.NET?
VB.NET app works on my local drive but not on the network drive
Is developing add-ons for Windows Explorer supported using managed code in .NET?
In C#, how can I change the properties of controls on another form?
How do I add images in listview by using VB.NET?
What is the best technique for connecting to a database from VB.NET?

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice



Windows Development - White Papers, News and Expert Advice
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2000 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts