Results 1 to 8 of 8

Thread: [RESOLVED] [2005] Problem with ComboBox_SelectedIndexChanged event

  1. #1

    Thread Starter
    Member
    Join Date
    May 2006
    Posts
    47

    Resolved [RESOLVED] [2005] Problem with ComboBox_SelectedIndexChanged event

    I have ComboBox bound to DataTable (Lookup table) and I have some code in ComboBox_SelectedIndexChanged event. I want that code to be executed when user select value from ComboBox.
    Problem is that ComboBox_SelectedIndexChanged event fired on form load which I dont want, I expect it to be fired only when user pick item in combobox.
    How to avoid ComboBox_SelectedIndexChanged event to be fired on form load?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Problem with ComboBox_SelectedIndexChanged event

    Quote Originally Posted by dranko
    How to avoid ComboBox_SelectedIndexChanged event to be fired on form load?
    You can't. The event is raised when the SelectedIndex property changes. Think outside the square a bit. In the event handler, test whether the Load event handler has already been executed. You would do that by setting a boolean flag at the end of the Load event handler.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    May 2006
    Posts
    47

    Resolved Re: [2005] Problem with ComboBox_SelectedIndexChanged event

    Yes, that works.
    My problem is in my thinking that SelectedIndex changed only when I click on ComboBox and pick item, but obviously I was wrong.

    Thanks

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Problem with ComboBox_SelectedIndexChanged event

    When you set the DataSource property of the ComboBox it automatically selects the first item. If you don't want the first item selected you have to explicitly set the SelectedIndex to -1, but that means two SelectedIndexChanged events rather than none.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Member
    Join Date
    May 2006
    Posts
    47

    Re: [2005] Problem with ComboBox_SelectedIndexChanged event

    I like your answers because you give explanation how something works (not only solution), and I learned a lot from you.

    Thanks.

  6. #6
    Member
    Join Date
    Oct 2005
    Posts
    39

    Re: [RESOLVED] [2005] Problem with ComboBox_SelectedIndexChanged event

    I was having this same issue. You want to use the SelectionChangeCommitted event as that will only respond to the user change on the control, not a program change of a index.

  7. #7

    Thread Starter
    Member
    Join Date
    May 2006
    Posts
    47

    Thumbs up Re: [RESOLVED] [2005] Problem with ComboBox_SelectedIndexChanged event

    Eh, why didn't I read help more carefully.

    "SelectionChangeCommitted is raised only when the user changes the combo box selection. Do not use SelectedIndexChanged or SelectedValueChanged to capture user changes, because those events are also raised when the selection changes programmatically."

    Thanks.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] [2005] Problem with ComboBox_SelectedIndexChanged event

    New information for me too.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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