combobox issue

smpolk

Member
Joined
Nov 22, 2006
Messages
10
Programming Experience
1-3
Has anyone seen this before? If i set the selectedindex of a combobox to 0, it doesnt select the first item in the list. Actually, it returns -1. If i set selectedindex to 1, it selects the second item in the list.

Either i have done something wrong, or the selectedindex property is messed up. I know for a fact that the code is setting the selectedindex to 0. I have checked it numerous amounts of times.

Any help? TIA
 
You have done something wrong. The SelectedIndex property is not messed up.
 
This is the code i use.
VB.NET:
[COLOR=#0000ff][COLOR=black]Private Sub frmSelectCompany_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load[/COLOR]
[COLOR=black] dtList = New DataTable[/COLOR]
[COLOR=black] Dim sqliteConStr As String = [/COLOR][COLOR=#800000]"DRIVER=SQLite3 ODBC Driver;Database="[/COLOR][COLOR=black] & Environment.CurrentDirectory &[/COLOR] [COLOR=#800000]"\QBOEData.db;Version=3;NoCreat=True;"[/COLOR]
[COLOR=black] Dim sqliteConn As New Odbc.OdbcConnection(sqliteConStr)[/COLOR]
[COLOR=black] Dim sql As String[/COLOR][COLOR=black] =[/COLOR] [COLOR=#800000]"SELECT ID, CompanyName, CompanyKey FROM Company"[/COLOR]
[COLOR=black] Dim da As New Odbc.OdbcDataAdapter(sql, sqliteConn)[/COLOR]
[COLOR=black] Try[/COLOR]
[COLOR=black]     da.Fill(dtList)[/COLOR]
 
[COLOR=black]     Me.cboCompany.DataSource = dtList[/COLOR]
[COLOR=black]     Me[/COLOR][COLOR=black].cboCompany.DisplayMember =[/COLOR] [COLOR=#800000]"CompanyName"[/COLOR]
[COLOR=black]     Me[/COLOR][COLOR=black].cboCompany.ValueMember =[/COLOR] [COLOR=#800000]"ID"[/COLOR]
[COLOR=black]If [/COLOR][COLOR=black]My[/COLOR][COLOR=black].Settings.Last_Company_Used =[/COLOR] [COLOR=#800000]"" [/COLOR][COLOR=black]Then[/COLOR]
[COLOR=black]         Me.cboCompany.Text = [/COLOR][COLOR=#800000]""[/COLOR]
[COLOR=black]Me[/COLOR][COLOR=black].cboCompany.SelectedIndex = -1[/COLOR]
[COLOR=black]     Else[/COLOR]
[COLOR=black]         Dim sFilter As String[/COLOR][COLOR=black] =[/COLOR] [COLOR=#800000]"CompanyName = "[/COLOR][COLOR=black] & PrepStr(My.Settings.Last_Company_Used)[/COLOR]
[COLOR=black]         Dim dr() As DataRow = dtList.Select(sFilter)[/COLOR]
[COLOR=black]         If dr.Length > 0 Then[/COLOR]
[COLOR=black]             Me[/COLOR][COLOR=black].cboCompany.Text =[/COLOR] [COLOR=#800000]""[/COLOR]
[COLOR=black]             Me.cboCompany.SelectedIndex = dtList.Rows.IndexOf(dr(0))[/COLOR]
[COLOR=black]         Else[/COLOR]
[COLOR=black]             Me[/COLOR][COLOR=black].cboCompany.Text =[/COLOR] [COLOR=#800000]""[/COLOR]
[COLOR=black]Me[/COLOR][COLOR=black].cboCompany.SelectedIndex = -1[/COLOR]
[COLOR=black]         EndIf[/COLOR]
[COLOR=black]     EndIf[/COLOR]
[COLOR=black] Catch ex As Exception[/COLOR]
[COLOR=black]     MsgBox(ex.Message)[/COLOR]
[COLOR=black] End Try[/COLOR]
[/COLOR][COLOR=black]End Sub[/COLOR]

Now, i have verified my value in this line to be 0:
VB.NET:
[COLOR=black]Me.cboCompany.SelectedIndex = dtList.Rows.IndexOf(dr(0))[/COLOR]

I have also manually set the selectedindex to 0 in the immediate window by doing:
VB.NET:
[COLOR=black]Me.cboCompany.SelectedIndex = 0[/COLOR]

It doesnt work with any of the above. Now, if i do:
VB.NET:
[COLOR=black]Me.cboCompany.SelectedIndex = 1[/COLOR]
It selects the second item in the list.

I have attached some screenshots to show what is happening. If you want more verifying screenshots, let me know. I will get them and post them.
 

Attachments

  • Breakpoint index value.jpg
    Breakpoint index value.jpg
    47.4 KB · Views: 27
  • Nothing selected.jpg
    Nothing selected.jpg
    16 KB · Views: 27
  • Nothing selected 2.jpg
    Nothing selected 2.jpg
    26.8 KB · Views: 26
Very weird situation.

So what you mean is that right after this line:
VB.NET:
Me.cboCompany.SelectedIndex = 0

the selectedindex equals -1?

Can you show the cboCompany_selectedindexchanged event?
Or maybe it has something to do with cboCompany being databound. Are you using any databinding_positionchanged events?
 
I found the problem in this part of the code:
VB.NET:
[COLOR=black]Me[/COLOR][COLOR=black].cboCompany.Text =[/COLOR][COLOR=#0000ff] [/COLOR][COLOR=#800000]""[/COLOR]
[COLOR=black]Me.cboCompany.SelectedIndex = dtList.Rows.IndexOf(dr(0))[/COLOR]

The problem is that i have Me.cboCompany.Text = "". The selectedindex is 0, but the text property of the combobox is "". I commented that out and it worked fine.

Strange that it only does that if i set the selectedindex to 0. If i set the selectedindex to 1, the text property has text in it.

Thank you to everyone that replied to this thread.
 
indexof searches through an array, looking for a value, if it cant find it it returns -1.

i think that indexof(dr(0)) is making the entire thing screw up, as it cant find (dr(0)) (whatever it is) within the dtList.

what do you think?
regards
adam
 
Last edited:
dr is a datarowarray. dr(0) is what was returned when i did a datatable.select. I know that only one record will be returned. dtList.Rows.IndexOf(dr(0)) returns the index of that specific datarow in the datatable, which will be the same index in the combobox. If anyone knows an easier, cleaner way to get that index, please share.

I think what happened was that since, by default, a combobox sets itself to index 0, when I tried setting the selectedindex to 0, it didnt consider that a change to update the combobox text. Since i did the .text = "" before i set the selectedindex, it cleared the .text property, then applied the selectedindex = 0, which caused no change to the selectedindex, therefore, the combobox does not update the .text property.

This is the only explanation i can come up with. This is why when i set the selectedindex = 1, the combobox considered that a change to the selectedindex and updated the .text property to the selectedtext, even though i was clearing the .text property before setting the selectedindex = 1.

Since i removed the .text = "" and the default selectedindex is 0, it shows the selectedtext for index 0 in the .text property, which, of course, is what i needed.

What led me to this conclusion is that i put in the combobox_selectedindexchanged event with a msgbox showing me the selectedindex value. When i set the selectedindex = 0, the msgbox popped up twice, both having a 0 value. When i set the selectedindex = 1 or -1, the msgbox popped up three times. Twice with a 0 value and once with either 1 or -1, which tells me that setting the selectedindex to 0 does not cause the selectedindexchanged event to fire to update the combobox when loading, but any other value does.

Maybe someone could give a better explanation of this if i am not making sense.
 
could you post your code on the forum for me? even if you do set a selectedindex, no matter what it still should register a selectedindexchanged event...

so could you just post your code?

cheers mate

regards
adam
 
Not sure what code you are requesting that i have not already posted other than the code i use to check the selectedindex and the text property, which is nothing more than an if statement. Here is the code as of now. You can scroll up to get the previous code i was using.

This is at module level
VB.NET:
Private dtList as DataTable
VB.NET:
[COLOR=#0000ff][COLOR=black]Private Sub frmSelectCompany_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load[/COLOR]
    [COLOR=black]dtList = New DataTable[/COLOR]
[/COLOR][COLOR=#0000ff][COLOR=black] Dim sqliteConStr As String = [/COLOR][COLOR=#800000]"DRIVER=SQLite3 ODBC Driver;Database="[/COLOR][COLOR=black] & Environment.CurrentDirectory &[/COLOR] [COLOR=#800000]"\QBOEData.db;Version=3;NoCreat=True;"[/COLOR]
[/COLOR][COLOR=#0000ff][COLOR=black] Dim sqliteConn As New Odbc.OdbcConnection(sqliteConStr)[/COLOR]
[/COLOR][COLOR=#0000ff][COLOR=black] Dim sql As String[/COLOR][COLOR=black] =[/COLOR] [COLOR=#800000]"SELECT ID, CompanyName, CompanyKey FROM Company"[/COLOR]
[/COLOR][COLOR=#0000ff][COLOR=black] Dim da As New Odbc.OdbcDataAdapter(sql, sqliteConn)[/COLOR]
[/COLOR][COLOR=#0000ff][COLOR=black] Try[/COLOR]
[/COLOR][COLOR=#0000ff][COLOR=black]da.Fill(dtList)[/COLOR]
 
[/COLOR][COLOR=#0000ff][COLOR=black]     Me.cboCompany.DataSource = dtList[/COLOR]
[/COLOR][COLOR=#0000ff][COLOR=black]Me[/COLOR][COLOR=black].cboCompany.DisplayMember =[/COLOR] [COLOR=#800000]"CompanyName"[/COLOR]
[/COLOR][COLOR=#0000ff][COLOR=black]     Me[/COLOR][COLOR=black].cboCompany.ValueMember =[/COLOR] [COLOR=#800000]"ID"[/COLOR]
[/COLOR][COLOR=#0000ff][COLOR=black]If [/COLOR][COLOR=black]My[/COLOR][COLOR=black].Settings.Last_Company_Used =[/COLOR] [COLOR=#800000]"" [/COLOR][COLOR=black]Then[/COLOR]
[/COLOR][COLOR=#0000ff][COLOR=black]'Me.cboCompany.Text = [/COLOR][COLOR=#800000]""[/COLOR]
[/COLOR][COLOR=#0000ff][COLOR=black]         Me[/COLOR][COLOR=black].cboCompany.SelectedIndex = -1[/COLOR]
[/COLOR][COLOR=#0000ff][COLOR=black]Else[/COLOR]
[/COLOR][COLOR=#0000ff][COLOR=black]Dim sFilter As String[/COLOR][COLOR=black] =[/COLOR] [COLOR=#800000]"CompanyName = "[/COLOR][COLOR=black] & PrepStr(My.Settings.Last_Company_Used)[/COLOR]
[/COLOR][COLOR=#0000ff][COLOR=black]         Dim dr() As DataRow = dtList.Select(sFilter)[/COLOR]
[/COLOR][COLOR=#0000ff][COLOR=black]    If dr.Length > 0 Then[/COLOR]
[/COLOR][COLOR=#0000ff][COLOR=black]    'Me[/COLOR][COLOR=black].cboCompany.Text =[/COLOR] [COLOR=#800000]""[/COLOR]
[/COLOR][COLOR=#0000ff][COLOR=black]Me.cboCompany.SelectedIndex = dtList.Rows.IndexOf(dr(0))[/COLOR]
[/COLOR][COLOR=#0000ff][COLOR=black]         Else[/COLOR]
[/COLOR][COLOR=#0000ff][COLOR=black]        'Me[/COLOR][COLOR=black].cboCompany.Text =[/COLOR] [COLOR=#800000]""[/COLOR]
[/COLOR][COLOR=#0000ff][COLOR=black]    Me[/COLOR][COLOR=black].cboCompany.SelectedIndex = -1[/COLOR]
[/COLOR][COLOR=#0000ff][COLOR=black]         End If[/COLOR]
[/COLOR][COLOR=#0000ff][COLOR=black]End If[/COLOR]
[/COLOR][COLOR=#0000ff][COLOR=black] Catch ex As Exception[/COLOR]
[/COLOR][COLOR=#0000ff][COLOR=black]     MsgBox(ex.Message)[/COLOR]
[/COLOR][COLOR=#0000ff][COLOR=black] End Try[/COLOR]
[/COLOR][COLOR=black]End Sub[/COLOR]
Here is the relevant part of the code that i am using for the OK button, just in case you are requesting that as well.
VB.NET:
Private Sub llOK_LinkClicked(...) Handles llOK.LinkClicked
    If Not Me.cboCompany.SelectedIndex = -1 And Not Me.cboCompany.Text = "" Then
        .... 'Do whatever here
    Else
        MsgBox("Please select a company.")
    End If
End Sub
You could even test this theory yourself with much less code to see what outcome you get. Build a form with a combobox and an OK button (just to close the form). In the form load event, build a datatable, put a couple of rows of data in it, then set it as the datasource to the combobox. Of course, set your displaymember to one of the columns. After that, put this code:

VB.NET:
<your combobox>.Text = ""
<your combobox>.SelectedIndex = 0
You will also need to put in a selectedindexchanged event for your combobox with a message box to display your selectedindex value to help see when the selectedindex changes and what it changes to.

Once you test that, change the code to:
VB.NET:
<your combobox>.Text = ""
<your combobox>.SelectedIndex = 1
and see what happens.

I think i am more curious to see if someone can recreate the problem i was having.
 
Back
Top