×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Array as ComboBox's DataSource

Array as ComboBox's DataSource

Array as ComboBox's DataSource

(OP)
Colleagues,
I have a Public array of type String as data source for a combo box on a Form:
Array's being populated at the start of the program (from INI file), and then assigned as the data source to that combo box:

CODE

cboSoughtStrings.DataSource = gaSoughtStrings 

The problem is: when array is changed (programmatically), e.g. an element is added, the combo box's content remains the same.
In other programming language, I could

CODE

cboMyComboBox.DataSource = "" ' 

and then re-connect it to the updated array, but in VB .NET I got the following error:

Quote (VS 2019)

$exception {"Complex DataBinding accepts as a data source either an IList or an IListSource."} System.ArgumentException

I tried just

CODE

cboSoughtStrings.Refresh() 
and it didn't help.

What am I doing wrong?

TIA!

RE: Array as ComboBox's DataSource

Arrays don't support the interface necessaryu to automatically update when used as a datasource.

The trick is to insert a BindingSource. You can add one to the form in the IDE, and then something like the following (minimalist example):

CODE -->

Public Class Form1
    Dim myArray(4) As String
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        myArray(0) = "Mike"
        BindingSource1.DataSource = myArray
        ComboBox1.DataSource = BindingSource1
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        myArray(1) = "spoon"
        BindingSource1.ResetBindings(False)
    End Sub
End Class 

RE: Array as ComboBox's DataSource

(OP)
Hmm...
Actually, this array is "dynamic": if a sought string is found in the searched files, it is added into that array.
If the number of the elements in that array is at top limit (say, 10 elements), then elements are "pushed down", bottom element is out, and this found string becomes top element.
IOW, I, the Program, cannot know how many elements are there in that array, and I have to declare it as a placeholder, Dim gaMyArray() As String at the start.

Nevertheless, I discovered the solution: all it takes to update the ComboBox having bound to an array is just to repeat the assignment command:

CODE --> .NET

InsertItem2StringArray1Dim(gaSoughtStrings, Trim(txtSought.Text), giMaxStrItems) ' Adds a string to the array
cboSoughtStrings.DataSource = gaSoughtStrings ' Reconnects the ComboBox to that array
cboSoughtStrings.Refresh()
cboSoughtStrings.SelectedIndex = 0
cboSoughtStrings.Refresh() 

(I hope that the name of this lil' function on the 1st line of code is self-explanatory? smile)

Regards,

Ilya

RE: Array as ComboBox's DataSource

> elements are "pushed down", bottom element is out

In that case I'd be tempted to use a Queue or an ArrayList instead of an array, and still use BindingSource as an intelligent broker, e.g

CODE -->

Public Class Form2
    Public myArrayList As ArrayList

    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        myArrayList = New ArrayList({"just", "a", "test", "with", "more", "items", "than", "we", "will", "eventually", "display"}) ' initialise, but bigger than we eventually want
        BindingSource1.DataSource = myArrayList
        ComboBox1.DataSource = BindingSource1
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Static itemname As Long
        itemname = itemname + 1
        InsertItem2StringArray1Dims(itemname, 10)
    End Sub

    Private Sub InsertItem2StringArray1Dims(strText As String, Maxitems As Integer) ' deliberately uses your name, even though it is doing things differently
        If myArrayList.Count >= Maxitems Then myArrayList.RemoveRange(Maxitems - 1, myArrayList.Count - Maxitems + 1) ' Can handle a resize
        myArrayList.Insert(0, strText)
        BindingSource1.ResetBindings(False)
    End Sub

End Class 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close