Question Combobox.SelectedIndex issue

gchq

Well-known member
Joined
Dec 14, 2007
Messages
168
Programming Experience
10+
Hi there

In a combobox populated by a DataTable I am having two issues!

The SelectedIndex property will not set
1. Either by integer
2. Or by FindString

VB.NET:
        Dim SupplyTypeList As New ComboBox
        SupplyTypeList.Name = "SupplyTypeList"
        SupplyTypeList.Location = New Point(220, 10)

        Dim vList As New DataTable
        vList.Columns.Add("ID", GetType(Integer))
        vList.Columns.Add("String", GetType(String))
        Dim vRow As DataRow = vList.NewRow
        vRow("ID") = 0
        vRow("string") = "Select Type"
        vList.Rows.Add(vRow)
        vRow = vList.NewRow
        vRow("ID") = 1
        vRow("String") = "Stock Item"
        vList.Rows.Add(vRow)
        vRow = vList.NewRow
        vRow("ID") = 2
        vRow("String") = "Reserve Item"
        vList.Rows.Add(vRow)
        SupplyTypeList.DataSource = vList
        SupplyTypeList.DisplayMember = "String"
        SupplyTypeList.ValueMember = "ID"

If I attempt to set the index (SupplyTypeList.SelectedIndex = 1) it blows out with InvalidArgument

If I attempt set the index via the FindString method it always returns a value of -1

VB.NET:
Dim vListRows() As DataRow = vList.Select("ID = " & row("Supply_Type"))
SupplyTypeList.SelectedIndex = SupplyTypeList.FindString(vListRows(0)("String"))

.. even though the string is valid and is in the ComboBox!

Any ideas?

Thanks
 
You need to add the ComboBox to your controls collection.

VB.NET:
Me.Controls.Add(SupplyTypeList)

Here's the sample code I used for testing. I would advise against having a column named "String".

VB.NET:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim supTypList As New ComboBox
        supTypList.Name = "SupplyTypeList"
        supTypList.Location = New Point(100, 100)

        Me.Controls.Add(supTypList)

        Dim dt As New DataTable
        dt.Columns.Add("ID", GetType(Integer))
        dt.Columns.Add("String", GetType(String))

        With dt.Rows
            .Add(0, "Select Type")
            .Add(1, "Stock Item")
            .Add(2, "Reserve Item")
        End With

        supTypList.DataSource = dt
        supTypList.DisplayMember = "String"
        supTypList.ValueMember = "ID"

        supTypList.SelectedIndex = 2
        'supTypList.SelectedIndex = supTypList.FindString(dt.Rows(1)("String"))

    End Sub
 
Add Control

Thanks for your reply

The code was only a snippet

VB.NET:
        MaintFormMain = New Form
        MaintFormMain.Size = New Point(500, 600)
        MaintFormMain.Controls.Clear()
        Dim vPanel As New Panel
        vPanel.Dock = DockStyle.Fill

VB.NET:
       vPanel.Controls.Add(SupplyTypeList)

VB.NET:
        MaintFormMain.Controls.Add(vPanel)
        MaintFormMain.Controls.Add(vToolStrip)
        MaintFormMain.Controls.Add(MStatusStrip)

All the controls are loading without any issue, and saving a selected value is not a problem - and this works fine

VB.NET:
Private Sub OrderTypeIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim vCombo As ComboBox = sender
        Dim vID As Integer = vCombo.SelectedValue
        Dim vObj() As Object = MaintFormMain.Controls.Find("ReserveLabel", True)
        Dim vLabel As Label = vObj(0)
        vObj = MaintFormMain.Controls.Find("CompIDTB", True)
        Dim vCompIDTB As TextBox = vObj(0)
        vObj = MaintFormMain.Controls.Find("CompNameTB", True)
        Dim vCompNameTB As TextBox = vObj(0)
        vObj = MaintFormMain.Controls.Find("MaintOrderToolstrip", True)
        Dim vToolstrip As ToolStrip = vObj(0)
        Dim vButton As ToolStripButton = vToolstrip.Items("ComponentButton")
        If vID = 2 Then
            vLabel.Visible = True
            vCompIDTB.Visible = True
            vCompNameTB.Visible = True
            vButton.Visible = True
        Else
            vLabel.Visible = False
            vCompIDTB.Visible = False
            vCompNameTB.Visible = False
            vButton.Visible = False
        End If
    End Sub

Usually I would have named the column "vString" not "String" - but other than that I still can't see a reason why it won't work! Sigh
 
Solved it

This extra line sorted everything!

VB.NET:
SupplyTypeList.BindingContext = MaintFormMain.BindingContext

Going to have to put THAT somewhere that I can remember!
 
Back
Top