Results 1 to 15 of 15

Thread: Adding Variables of Array to List Box

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    8

    Adding Variables of Array to List Box

    Ok so I have an array and wish to add the variables of the array to separate lines of the list box. I came across this which in some ways worked but also didn't. The second thing mentioned on the page was to us:

    Code:
    ListBox1.DataSource = Form2.Title
    This works the first time you run it but if you try to run it again nothing happens. So using the above or something else how can I get it to update the list box?

    Another thing I tried was to use the DataSource method but instead to clear the list box first, but that turned up an error

    Thanks for any light you can shine on this,
    Sparky13

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Adding Variables of Array to List Box

    Are you using a one-dimensional array? If so then you can simply assign the array to the ListBox's DataSource property. If the array doesn't contain simple objects like Strings or Integers then you will also need to set the DisplayMember to specify which property or column to display.

    Alternatively, you can call Items.AddRange and pass the array as an argument.

    For us to be more specific, you're going to have to be more specific, i.e. EXACTLY what is the data and how are you using it?

    Also, if your array is multi-dimensional then none of this applies. You'd have to use a loop or just not use an array, which is almost certainly the better option. People often use 2D arrays in situations where they are not appropriate and using a 1D array of custom objects would be better.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    8

    Re: Adding Variables of Array to List Box

    Thanks heaps for the reply. It's a 1-dimensional array containing strings (and integers where I'll use this again). I tried the data source thing but can't seem to find how to assign it to the array itself and not just the form

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Adding Variables of Array to List Box

    You're not assigning anything to the array or to the form. You are assigning the array to the DataSource. The variable or property you're assigning to goes on the left, the value you're assigning goes on the right and the assignment operator, i.e. '=', goes in the middle. It's just like simple mathematics, e.g. if you want to assign 10 to X you do it like this:
    Code:
    X = 10
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    8

    Re: Adding Variables of Array to List Box

    I'm a bit thick headed . I understand what you mean that X = 10 is how you write it not 10 = X. I'm just confused what I'm actually putting where. My question before was because I was looking in the DataSource thing within the properties of the list box.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Adding Variables of Array to List Box

    I don't really know what you're saying. I don't really know how to be clearer than to say that you assign the array to the DataSource. The page you linked to shows you what to do and doesn't mention anything about a Title, so I don't really know what the problem is. Please spell it out, fully and clearly.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    8

    Re: Adding Variables of Array to List Box

    I'll try to spell it out better. I use this:
    Code:
    lbEntries.DataSource = Form2.Title
    (lbEntries being my list box and Title being the array)
    It works the first time the code is run but every time after that it doesn't update the list box with what is now in the array

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Adding Variables of Array to List Box

    I didn't realise that 'Title' was an array because the name suggests otherwise. If you have an array of titles then it should be named 'Titles' or something else that suggests that there are multiple values. 'Title' would be appropriate for a String variable that contained a single title.

    Anyway, as for the problem, that code is going to do exactly what you think it is. It's going to display the contents of Form2.Title in lbEntries. If you're not seeing what you expect to see then it's not because that code isn't working. It's because you're not binding the same array that you made changes to. Either you're binding the wrong array or you made changes to the wrong array. You didn't by any chance use ReDim at some point maybe? You should show us the code where you think you're making changes to this array.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    8

    Re: Adding Variables of Array to List Box

    Ok, seeing as my code's fairly all over the place I made a demo form etc to show what's happening. I'll attach it so you can download it if you wish, but also Include the code:

    Code:
    Public Class Form1
        Dim arrTitle(50) As String
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            ListBox1.DataSource = arrTitle 'This button is to add the data to the list box for the first time
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            arrTitle(0) = "0"
            arrTitle(1) = "1"
            arrTitle(2) = "2"
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            arrTitle(3) = "3" 'This button is to add a new variable in the array and add that new variable to the list box
            ListBox1.DataSource = arrTitle
        End Sub
    End Class
    And again thanks heaps for taking the time to help me out
    Attached Files Attached Files

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Adding Variables of Array to List Box

    OK, the problem would be that you're not actually changing the DataSource. You're assigning the same object that is already assigned, so the control doesn't actually do anything. You have two choices:

    1. Make sure the DataSource actually changes. To do that, set it to Nothing first and then assign the array again. The problem with that is that you will lose any selection during the change, so you'll have to reinstate that.

    2. A better option is to not use an array as the DataSource. While I haven't tried it, I think that if you bind the array to a BindingSource and then bind that to the control, calling ResetBindings on the BindingSource after changing anything in the array should refresh the control. Alternatively, you could use just a BindingList in place of both, which also has a ResetBindings method.

    Personally, I would go with the BindingList. Your situation is more appropriate to a collection than an array in the first place, so that's what you should be using regardless.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    8

    Re: Adding Variables of Array to List Box

    Can you link me something that explains how I would use it. All I can find is ones using databases etc

  12. #12

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    8

    Re: Adding Variables of Array to List Box

    Bump

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Adding Variables of Array to List Box

    Quote Originally Posted by sparky13 View Post
    Can you link me something that explains how I would use it. All I can find is ones using databases etc
    Define "it". There is no single "it" in my post so I don't know what "it" you're referring to. We can't read minds so you must be clear.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  14. #14

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    8

    Re: Adding Variables of Array to List Box

    Using the binding source method

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Adding Variables of Array to List Box

    Whether you're using a database or not doesn't make a difference. Without a BindingSource, you assign your list directly to the DataSource of the grid. That list might a DataTable, an array, a List(Of T) or whatever. It can be any object that implements the IList or IListSource interface. Any example you've seen of using the BindingSource is relevant. Instead of binding your list to the control, you bind your list to the BindingSource and bind the BindingSource to the control.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width