Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Visual Basic 2010 Programmer's Reference
This is the forum to discuss the Wrox book Visual Basic 2010 Programmer's Reference by Rod Stephens; ISBN: 9780470499832
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Visual Basic 2010 Programmer's Reference 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 Display Modes
  #1 (permalink)  
Old March 29th, 2011, 08:41 PM
Registered User
 
Join Date: Mar 2011
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Help with List Boxes

Hi - I am new to VB and I'm working on a project. I have a list box where users can select more than one name from a list. I can't seem to get my code to work. Any suggestions? I think I'm missing a piece of the code but for the life of me, I can't figure it out. Here is my code.

Private Sub MainForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles _
Me.Load

' fills the list box with values

namesListBox.Items.Add(Convert.ToString("Debbie"))
namesListBox.Items.Add(Convert.ToString("Bill"))
namesListBox.Items.Add(Convert.ToString("Jim"))
namesListBox.Items.Add(Convert.ToString("Ahmad"))
namesListBox.Items.Add(Convert.ToString("Carol"))
namesListBox.SelectedIndex = 0

End Sub

Private Sub multiButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles _
multiButton.Click

Dim SELECTED_NAMES As String

For INDEX As Integer = 0 To namesListBox.SelectedItems.Count - 1
SELECTED_NAMES = namesListBox.SelectedItems.ToString & " " &
resultLabel.Text.ToString & " " &
ControlChars.NewLine
INDEX += 1
Next INDEX

End Sub

Any suggestions would be greatly appreciated. Thanks!

Jewel
Reply With Quote
  #2 (permalink)  
Old March 29th, 2011, 09:22 PM
Rod Stephens's Avatar
Wrox Author
Points: 2,992, Level: 22
Points: 2,992, Level: 22 Points: 2,992, Level: 22 Points: 2,992, Level: 22
Activity: 37%
Activity: 37% Activity: 37% Activity: 37%
 
Join Date: Jan 2006
Location: , , .
Posts: 599
Thanks: 1
Thanked 84 Times in 83 Posts
Default

First, you don't need to convert a string into a string to add items to the list box. For example, this should work:

Code:
namesListBox.Items.Add("Debbie")
Using Convert won't hurt, it just makes the code less clear.

I think this is the step giving you problems:

Code:
SELECTED_NAMES = namesListBox.SelectedItems.ToString
Here you're using SelectedItems when you probably want something like SelectedItems(INDEX). Note also that you should not increment the looping variable in a For loop because the loop does it for you.

So try something like this:

Code:
Dim SELECTED_NAME As String
For INDEX As Integer = 0 To namesListBox.SelectedItems.Count - 1
    SELECTED_NAMES &= namesListBox.SelectedItems(INDEX) & _
        ControlChars.NewLine
Next INDEX

resultLabel.Text = SELECTED_NAMES
Let me know if that works.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
Reply With Quote
  #3 (permalink)  
Old March 30th, 2011, 11:01 AM
Registered User
 
Join Date: Mar 2011
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your help. I'm getting an error on the section of code below

name.displayLabel.Text = SELECTED_NAMES

saying that my variable is used before it is assigned a value

This little snippet of code is really giving me a hard time! Thanks for your help!

Jewel
Reply With Quote
  #4 (permalink)  
Old March 30th, 2011, 12:30 PM
Rod Stephens's Avatar
Wrox Author
Points: 2,992, Level: 22
Points: 2,992, Level: 22 Points: 2,992, Level: 22 Points: 2,992, Level: 22
Activity: 37%
Activity: 37% Activity: 37% Activity: 37%
 
Join Date: Jan 2006
Location: , , .
Posts: 599
Thanks: 1
Thanked 84 Times in 83 Posts
Default

Sorry, I spelled it SELECTED_NAME in one place and SELECTED_NAMES in another. Try changing them all to SELECTED_NAMES.

I'm also not sure why you have name.displayLabel. I think you can just say displayLabel.

If that doesn't work, post your current code again and I'll take another look.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
Reply With Quote
  #5 (permalink)  
Old April 5th, 2011, 08:43 AM
Registered User
 
Join Date: Mar 2011
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your help!
Reply With Quote
Reply


Thread Tools
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
LIST BOXES Vision G Access 10 May 27th, 2006 01:45 AM
List Boxes, Help! JeffGirard Access 17 August 22nd, 2005 01:31 PM
Dynamic List Boxes swhite Access VBA 10 September 11th, 2003 01:27 PM
SQL , List Boxes/Menu Boxes, DB's Ginzu3 Classic ASP Databases 1 June 30th, 2003 04:07 AM



All times are GMT -4. The time now is 11:22 PM.


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