Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array as ComboBox's DataSource

Status
Not open for further replies.

IlyaRabyy

Programmer
Nov 9, 2010
568
US
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:

VS 2019 said:
$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!
 
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:
[COLOR=blue]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[/color]
 
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:
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? :))

Regards,

Ilya
 
> 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:
[COLOR=blue]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"}) [COLOR=green]' initialise, but bigger than we eventually want[/color]
        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) [COLOR=green]' deliberately uses your name, even though it is doing things differently[/color]
        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[/color]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top