Hey guys, I'm trying to get this form to display in the listbox the address from the info from the textboxes, I followed an example that uses a class to do it, but it seems I am going wrong somewhere and I don't know where. Here is the code for the class:

Public Class CLCustomer
    Private pName As String
    Private pAddress As String
    Private pState As String
    Private pCity As String
    Private pZip As String
    Public Property Name() As String
        Get
            Return pName
        End Get
        Set(ByVal value As String)
            pName = value
        End Set
    End Property

    Public Property Address() As String
        Get
            Return pAddress
        End Get
        Set(ByVal value As String)
            pAddress = value
        End Set
    End Property

    Public Property City() As String
        Get
            Return pCity
        End Get
        Set(ByVal value As String)
            pCity = value
        End Set
    End Property

    Public Property State() As String
        Get
            Return pState
        End Get
        Set(ByVal value As String)
            pState = value
        End Set
    End Property

    Public Property Zip() As String
        Get
            Return pZip
        End Get
        Set(ByVal value As String)
            pZip = value
        End Set
    End Property
    Public Function DisplayAddress(ByVal sep As String)
        Dim strTemp As String = pName.ToString() & sep
        strTemp &= pAddress.ToString() & sep
        strTemp &= pCity.ToString() & ", " & pState.ToString() & pZip.ToString()
        Return strTemp
    End Function
    Public Sub New()

    End Sub
    Public Sub New(ByVal vpName As String, ByVal vpAddress As String, ByVal vpState As String, ByVal vpCity As String, ByVal vpZip As String)
        pName = vpName
        pAddress = vpAddress
        pState = vpState
        pCity = vpCity
        pZip = vpZip
    End Sub
End Class

Here is the form code:

Public Class Form1

    Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow.Click
        Dim strName As String = txtName.Text
        Dim strAddress As String = txtAddress.Text
        Dim strState As String = cboState.Text
        Dim strCity As String = txtCity.Text
        Dim strZipCode As String = txtZip.Text
        Dim showAddress As New CLCustomer(strName, strAddress, strState, strCity, strZipCode)
        txtList.Text = showAddress.DisplayAddress(vbCrLf)
    End Sub


End Class

Recommended Answers

All 4 Replies

Try,

ListBox1.Items.Add(showAddress.DisplayAddress(vbCrLf))

It worked, but it is on one line, not on several lines like in an address

example:

John Doe
8745 West Way Dr
Anywhere, KS 45125

You should have to use OwnerDraw listbox. Set DrawMode and handle DrawItem and MeasureItem event.

Here is a code snippet.

Dim data() As String = {"One" & vbCrLf & "Two", "Hello" & vbCrLf & "Hi"}
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ListBox1.DrawMode = DrawMode.OwnerDrawVariable
        For Each p As String In data
            ListBox1.Items.Add(p)
        Next
    End Sub

    Private Sub ListBox1_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
        e.DrawBackground()
        e.DrawFocusRectangle()
        e.Graphics.DrawString(data(e.Index), New Font(FontFamily.GenericSansSerif, 16, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.Black, e.Bounds)
    End Sub

    Private Sub ListBox1_MeasureItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles ListBox1.MeasureItem
        e.ItemHeight = 50
    End Sub

Are you trying to plug multiple addresses into that listbox or just one? If you're just using it to show one address, why not use a multiline textbox? If you are using the listbox as a selector for the addresses, listbox still might not be the right answer. As is, listbox doesn't support multiline entries so maybe try something that does(like datagridview) Or you could owner draw the listbox like adatapost says.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.