Results 1 to 5 of 5

Thread: Combobox SelectedIndexChanged

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Combobox SelectedIndexChanged

    I have a combobox that I am filling with data at form load. I also have code that adds data to another field when the SelectedIndex gets changed. Right now I have a SelectedIndex = -1 after the combobox is filled so that there is no value listed in the combobox when the form is finished loading.

    The problem is that that SelectedIndex = -1 is tripping my SelectedIndexChange code block. This seems like I am attempting such a common task that there must be an easy workaround or I am really missing something obvious.

    Any ideas?

  2. #2
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840

    Re: Combobox SelectedIndexChanged

    You should do a check to see if SelectedIndex=-1 in the top of the SelectedIndexChanged method end exit if it is.
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Combobox SelectedIndexChanged

    Thanks Pax. I tried that but since the records in the combobox get loaded and ordered alphabeticaly the IndexNumber starts out at 23 (The 23rd Record is (Abe) and then gets set to -1 as the form finishes loading, which is when the IndexXhanged gets fired.

    Doing it this way the 23 is what gets tested not the -1. And I can't test for a 23 all the time either because it is possible that a record will get added that will be Aarron and will move ahead of record 23.

    Anything else you can think of that I can try?

  4. #4
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Combobox SelectedIndexChanged

    I use this same basic type of code every where I use a combobox:

    VB Code:
    1. If Me.cboLevel.SelectedIndex < 0 Then
    2.             MessageBox.Show("You must select a User Level for this new user." & System.Environment.NewLine & _
    3.                             "Please select a User Level from the provided list.", "No User Level Selected", MessageBoxButtons.OK, MessageBoxIcon.Warning)
    4.             Me.cboLevel.Focus()
    5.             Exit Sub
    6.         Else
    7.             intLevel = Me.intUserLevels(Me.cboLevel.SelectedIndex)
    8.         End If

    if the selectedindex is < 0 then the procedure exits. I also fill the combo box alphabetically, But if nothing is selected in the box the selectedindex is equal to -1
    if the first item is selected then the selectedindex = 0 and so on...
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Combobox SelectedIndexChanged

    Here is my entire codeblock
    VB Code:
    1. Private Sub cboMarketPiece_SelectedindexChanged( _
    2.         ByVal sender As Object, ByVal e As System.EventArgs) _
    3.         Handles cboMarketPiece.SelectedValueChanged
    4.  
    5.         If cboMarketPiece.SelectedIndex < 0 Then
    6.             cboMarketPiece.Focus()
    7.             Exit Sub
    8.         Else
    9.             Dim cnn As New OleDbConnection
    10.             Dim cmd As New OleDbCommand
    11.             Dim dr As OleDbDataReader
    12.             cnn.ConnectionString = conBCA
    13.             cmd = cnn.CreateCommand
    14.             cmd.CommandText = "SELECT Number From " & _
    15.                 "queMarketingPiece Where MarketingPiece = '" & _
    16.                 cboMarketPiece.Text & "'"
    17.             Try
    18.                 cnn.Open()
    19.                 dr = cmd.ExecuteReader
    20.                 While dr.Read()
    21.                     txtMpCode.Text = dr("Number").ToString
    22.                 End While
    23.                 dr.Close()
    24.             Catch ex As Exception
    25.                 Exit Sub
    26.             Finally
    27.                 cnn.Close()
    28.             End Try
    29.         End If
    30.     End Sub

    The txtMpCode.Text = dr("Number").ToString is filling that box when it should not be because I set the Index to -1 at the form load. I did come up with a quick and dirty work around and that is to add this to the form load.
    VB Code:
    1. Private Sub frmCreateBarcode_Load( _
    2.         ByVal sender As Object, ByVal e As System.EventArgs) _
    3.         Handles Me.Load
    4.  
    5.         FillMarketingPiece()
    6.         FillCouponOffer()
    7.         txtMpCode.Clear()
    8.         txtCoCode.Clear()
    9.  
    10.     End Sub
    11. ' The last line in FillCouponOffer and FillMarketingPieceOffer is cboXXX.SelectedIndex = -1
    That seems to be doing the trick.

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