Visual Studio Development Bookmark and Share   
 index > Visual Basic Express Edition > displaying contents of a Listbox in a msgbox
 

displaying contents of a Listbox in a msgbox

Hi all,
a very basic question. How can I display the contents of a listbox on a msgbox? I could only find out answers with selected indexes not with all indexes.

BR
Aj
ajrk  Friday, October 16, 2009 7:35 AM

        Dim SB As New System.Text.StringBuilder

        For Each Item As String In ListBox1.Items

            SB.Append(Item & vbCrLf)

        Next

        MessageBox.Show(SB.ToString)

 

Dave299  Friday, October 16, 2009 9:49 AM

I see from your other thread that you have only just started with VB a couple of days ago but hopefully this won't be too deep for you.

First of all if you haven't already done so turn Option Strict On.  Best way is via Tools | Options | Projects and Solutions | VB Defaults.  That can prevent you getting some very hard to trace errors.  Learn about what it does in Help.

The listbox actually holds objects.  Usually those objects are strings - but they don't have to be.  So yes - it is displaying the same memory location, not a copy.

The code I posted only works if you have put strings into the listbox, any other type will generate an exception.  If you added this for example:

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim Obj As New Object
        ListBox1.Items.Add(Obj)
    End Sub

You will now find the code generates an exception because the listbox now contains an object as well as a list of strings.

Have a look at this as an illustration of a more general case.  Just start a new project, add a listbox and a button and copy and paste the code.  If it just goes over your head feel free to ignore it and come back to it later.  When you get more into it you will find the ability to add objects to a listbox is a very useful technique.

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim SB As New System.Text.StringBuilder

        For Each Item As Persons In ListBox1.Items

            Dim Person As Persons = CType(Item, Persons)

            SB.Append(Person.Name & " who lives " & Person.Address & vbCrLf)

        Next

        MessageBox.Show(SB.ToString)

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim Person1 As New Persons("Fred", "Somewhere")

        Dim Person2 As New Persons("Joe", "Somewhere else")

        ListBox1.Items.Add(Person1)

        ListBox1.Items.Add(Person2)

    End Sub

End Class

 

Public Class Persons

    Public Name As String

    Public Address As String

    Public Sub New(ByVal Name As String, ByVal Address As String)

        Me.Name = Name

        Me.Address = Address

    End Sub

    Public Overrides Function ToString() As String

        Return Name

    End Function

End Class

 

Dave299  Friday, October 16, 2009 11:33 AM

        Dim SB As New System.Text.StringBuilder

        For Each Item As String In ListBox1.Items

            SB.Append(Item & vbCrLf)

        Next

        MessageBox.Show(SB.ToString)

 

Dave299  Friday, October 16, 2009 9:49 AM
hi Dave,
that worked. thnx a lot. 
this is the first time I trying a super high level lang. so a stupid doubt. when we do these steps, are we displaying the same item (are we referring to same mem location)? It looks to me as if we are copying it as a string here. 
BR
Aj


ajrk  Friday, October 16, 2009 10:49 AM

I see from your other thread that you have only just started with VB a couple of days ago but hopefully this won't be too deep for you.

First of all if you haven't already done so turn Option Strict On.  Best way is via Tools | Options | Projects and Solutions | VB Defaults.  That can prevent you getting some very hard to trace errors.  Learn about what it does in Help.

The listbox actually holds objects.  Usually those objects are strings - but they don't have to be.  So yes - it is displaying the same memory location, not a copy.

The code I posted only works if you have put strings into the listbox, any other type will generate an exception.  If you added this for example:

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim Obj As New Object
        ListBox1.Items.Add(Obj)
    End Sub

You will now find the code generates an exception because the listbox now contains an object as well as a list of strings.

Have a look at this as an illustration of a more general case.  Just start a new project, add a listbox and a button and copy and paste the code.  If it just goes over your head feel free to ignore it and come back to it later.  When you get more into it you will find the ability to add objects to a listbox is a very useful technique.

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim SB As New System.Text.StringBuilder

        For Each Item As Persons In ListBox1.Items

            Dim Person As Persons = CType(Item, Persons)

            SB.Append(Person.Name & " who lives " & Person.Address & vbCrLf)

        Next

        MessageBox.Show(SB.ToString)

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim Person1 As New Persons("Fred", "Somewhere")

        Dim Person2 As New Persons("Joe", "Somewhere else")

        ListBox1.Items.Add(Person1)

        ListBox1.Items.Add(Person2)

    End Sub

End Class

 

Public Class Persons

    Public Name As String

    Public Address As String

    Public Sub New(ByVal Name As String, ByVal Address As String)

        Me.Name = Name

        Me.Address = Address

    End Sub

    Public Overrides Function ToString() As String

        Return Name

    End Function

End Class

 

Dave299  Friday, October 16, 2009 11:33 AM

You can use google to search for other answers

Custom Search

More Threads

• MSFlex Grid
• Using multiple data bound items
• help splitting text
• Could Anyone help me in VBE 2005,I need a way to make a button change to...
• Restore from System Tray - Two EXE's Interacting
• Skip a If Then statement?
• Repeat adding
• How to use MySQL VB.NET
• winsock help!
• Whats with SQL Sever currently?