?
Solved

How do I bind an array to a listbox?

Posted on 2004-10-01
3
Medium Priority
?
1,287 Views
Last Modified: 2008-03-24
Greetings Experts...

Disclaimer: Newbie

I've been playing around with writing my own classes and have actually been fairly successful. That being said. I'm trying to use an array as a property in a class. I don't know how to bind the array to a listbox, I don't even know if I declared the array correctly.

Here's the code in the class

    Dim lweekstr As Array = Array.CreateInstance(GetType(Object), 53)
    Public Property weekstr() As Array
        Get
            Return lweekstr
        End Get
        Set(ByVal Value As Array)
            lweekstr = Value
        End Set
    End Property

Here's the code in the class that instantiates the above:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim f As New ClassLibrary1.CalendarClass
        f.Year = 2004
        f.test() 'this method populates the array
        Me.ListBox1.DataSource = f.weekstr
        '---- this code tests to see if there is something in the array(there is)
        Dim c As Integer
        For c = 1 To 10
            MsgBox(f.weekstr(c))
        Next
    End Sub

Thanks in advance for your help
 
0
Comment
Question by:KenDickinson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 12202846
If you are trying to store an Integer into an Object array, there is a performance hit from converting a value type (Integer) into an object type to store in the array (this is called boxing).  Use a type-specific declaration instead:

Dim lWeekStr As Integer()

Bob
0
 
LVL 8

Accepted Solution

by:
bramsquad earned 2000 total points
ID: 12202942
       this works

        Dim arr As Integer() = {1, 2, 3, 4}
        For i As Integer = 0 To arr.GetUpperBound(0)
            Me.ListBox1.Items.Add(arr(i))
        Next

~b
0
 
LVL 28

Expert Comment

by:iboutchkine
ID: 12203497
To bind lb to array
  ListBox1.DataSource = (arr)


To ppulate array from lb

ListBox1.Items.CopyTo(arr, 0)



or all together - populate array from lb and back to array

 Dim arr(ListBox1.Items.Count - 1)

        ListBox1.Items.CopyTo(arr, 0)
        ListBox1.Items.Clear()
        MessageBox.Show(Join(arr, ";"))
        ListBox1.Items.AddRange(arr)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question