VB.NET School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 306,996 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,964 people online right now. Registration is fast and FREE... Join Now!




Visual Basic 2008 List Box

 

Visual Basic 2008 List Box

TimBDesigns

5 Nov, 2009 - 08:16 PM
Post #1

New D.I.C Head
*

Joined: 25 Apr, 2009
Posts: 7


My Contributions
My name is Tim I'm having a problem getting my multiple selections from my list box to display in the results label and there is no examples in the text any how I'm thiniking I need to use a loop to get it done. is this evern in the ball park? I'm not looking for the solution but I would like to be pointed in the right direction. I allready have been stuck for hours and I wish to narrow my focus. Thanks for any help.
CODE
Option Explicit On
Option Strict On
Option Infer Off

Public Class MainForm

    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("Debbie")
        namesListBox.Items.Add("Bill")
        namesListBox.Items.Add("Jim")
        namesListBox.Items.Add("Ahmad")
        namesListBox.Items.Add("Carol")
    End Sub

    Private Sub exitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles exitButton.Click
        Me.Close()
    End Sub

    Private Sub singleButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles singleButton.Click
        Select Case Convert.ToString(namesListBox.SelectedItem)
            Case "Debbie"
                resultLabel.Text = "Debbie"
            Case "Bill"
                resultLabel.Text = "Bill"

            Case "Jim"
                resultLabel.Text = "Jim"

            Case "Ahmad"
                resultLabel.Text = "Ahmad"

            Case "Carol"
                resultLabel.Text = "Carol"
        End Select
    End Sub

    Private Sub multiButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles multiButton.Click
        Dim i As Integer = 0
        Dim numberOfNames As Integer
        numberOfNames = namesListBox.Items.Count
        Do While i >= numberOfNames
        Loop

    End Sub
End Class


User is offlineProfile CardPM
+Quote Post


PsychoCoder

RE: Visual Basic 2008 List Box

5 Nov, 2009 - 08:21 PM
Post #2

I Code, Therefore I am
Group Icon

Joined: 26 Jul, 2007
Posts: 14,919



Thanked: 517 times
Dream Kudos: 11525
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

My Contributions
VB 2008 is VB.NET, moved to VB.NET smile.gif
User is offlineProfile CardPM
+Quote Post

CharlieMay

RE: Visual Basic 2008 List Box

6 Nov, 2009 - 02:27 AM
Post #3

D.I.C Head
Group Icon

Joined: 25 Sep, 2009
Posts: 239



Thanked: 43 times
Dream Kudos: 25
My Contributions
Instead of namesListBox.Items.Count you can use

CODE

namesListBox.SelectedItems.Count


keep in mind though that selected indexes start a 0 so you will have to subtract 1 from the count.

so something like this should give you an acceptable result:

CODE

        resultLabel.Text = String.Empty
        For x As Integer = 0 To namesListBox.SelectedItems.Count - 1
            resultLabel.Text &= namesListBox.SelectedItems(x).ToString & " "
        Next


Which would also work in your singleButton.Click event... In other words, you only need a single button to handle whether a single selection is made or a multiple selection.

On more tip; in your SingleButton.Click you can achieve the same results just by simply using
CODE

resultLabel.text = Convert.ToString(namesListBox.SelectedItem)


But as I stated above, you can achieve both results with the other example.

This post has been edited by CharlieMay: 6 Nov, 2009 - 02:29 AM
User is offlineProfile CardPM
+Quote Post

Munkybones

RE: Visual Basic 2008 List Box

7 Nov, 2009 - 08:57 PM
Post #4

New D.I.C Head
*

Joined: 31 Oct, 2009
Posts: 18



Thanked: 1 times
My Contributions
You shouldnt need a do loop to get this reaction to work to fill a listbox..there is one thing that you have just misplaced it looks like

try putting the following code under the button command instead of the mainform page
CODE
namesListBox.Items.Add("Debbie")
        namesListBox.Items.Add("Bill")
        namesListBox.Items.Add("Jim")
        namesListBox.Items.Add("Ahmad")
        namesListBox.Items.Add("Carol")

right now its in the main form so its just sitting there inactive...if you set the listbox to run from the button this should work
User is offlineProfile CardPM
+Quote Post

Donnie1581

RE: Visual Basic 2008 List Box

7 Nov, 2009 - 11:39 PM
Post #5

D.I.C Head
**

Joined: 5 Nov, 2009
Posts: 56



Thanked: 2 times
My Contributions
Ok, so you are wanting to populate the listbox with names, then you want to click on a name in the listbox and the name to show in a textbox when selected?
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 06:02AM

Live VB.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month