none
SelectedIndexChanged out of control RRS feed

  • Question

  • I have a nice ComboBox in my application which I use to pull records from a database. That part works as expected. But anytime I "Add", "Modify" or "Deleted" items from the ComboBox, the corresponding event SelectedIndexChanged always fires? Is there a way to force the event only to respond to "my command"? Right now, I have to use Flags declared at the Form level to keep the event from firing uncontrollably because, I'm only interested in the ComboBox fetching some code to perform a database query on behalf of the user -- not in response to a ComboBox.Find() method?

    Any help is appreciated.

    Friday, June 26, 2020 3:55 PM

Answers

  • Roger,

    The Combobox is really an old beast from Win16  just converted to Win32 and then used almost unchanged in .Net and .WPF, I assume it is in the same in Net Core. 

    There is nothing wrong of using Flags, although I've always promoted the way Karen shows currently too, simply because it is more .Net.

    Because it is so old, it has issues which are not wrong, but which you won't expect. For instance is the Selected Index only fired as it has happened, but that means also new initialized when it is automatically set to zero. 

    Try with the combobox the way which fits yourself the most. 


    Success
    Cor

    • Marked as answer by roger.breton Tuesday, June 30, 2020 3:04 AM
    Saturday, June 27, 2020 5:43 PM

All replies

  • Use add and remove handlers

        Private Sub AddNewButton_Click(sender As Object, e As EventArgs) Handles AddCatagoryButton.Click
            Dim currentName = _categoryBindingList.Item(CategoryComboBox.SelectedIndex).CategoryName
    
            RemoveHandler CategoryComboBox.SelectedIndexChanged, AddressOf CategoryComboBox_SelectedIndexChanged
            _categoryBindingList.Add(New Category() With {.CategoryID = -1, .CategoryName = NewCatagoryTextBox.Text})
    
            CategoryComboBox.DataSource = _categoryBindingList.OrderBy(Function(item) item.CategoryName).ToList()
            CategoryComboBox.SelectedIndex = CategoryComboBox.FindString(currentName)
            AddHandler CategoryComboBox.SelectedIndexChanged, AddressOf CategoryComboBox_SelectedIndexChanged
        End Sub


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, June 26, 2020 4:22 PM
    Moderator
  • I long had Add and Remove Handlers for my ComboBox but I never thought I'd also need / could use them around ComboBox Add or Remove operations. I'll give it a try. It's short circuiting my hard-earned logic, otherwise :-)
    Friday, June 26, 2020 4:43 PM
  • I long had Add and Remove Handlers for my ComboBox but I never thought I'd also need / could use them around ComboBox Add or Remove operations. I'll give it a try. It's short circuiting my hard-earned logic, otherwise :-)
    I've never used add and remove handlers this way as whatever happens there in that event would be handled just fine and also I rarely ever used this event but instead relied on other events detached from the actual ComboBox or ListBox.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, June 26, 2020 5:08 PM
    Moderator
  • I had to back to fetching the database data for the ComboBox out of my dataset, keeping track of the ComboBox directly proved to be too complex of a task, given my current code. Thank you so much for your kind help and encouragements :-)
    Saturday, June 27, 2020 2:00 AM
  • Roger,

    The Combobox is really an old beast from Win16  just converted to Win32 and then used almost unchanged in .Net and .WPF, I assume it is in the same in Net Core. 

    There is nothing wrong of using Flags, although I've always promoted the way Karen shows currently too, simply because it is more .Net.

    Because it is so old, it has issues which are not wrong, but which you won't expect. For instance is the Selected Index only fired as it has happened, but that means also new initialized when it is automatically set to zero. 

    Try with the combobox the way which fits yourself the most. 


    Success
    Cor

    • Marked as answer by roger.breton Tuesday, June 30, 2020 3:04 AM
    Saturday, June 27, 2020 5:43 PM
  • Thank you Cor for your encouragements and suggestions.

    My database design is simple but I probably, because of my technical ignorance, approach it wrongly. It's really easy, though. The values shown in the ComboBox act as a "switchboard" to navigate directly to selected records. I guess I could implement it from scratch, from a TextBox, but I have to say I appreciate the "convenience" of the control.  

    Saturday, June 27, 2020 6:26 PM
  • For those who are interested in learning from my mistakes, below are some of the messages I received in the course of development, as a consequence of the various choices I made along the way...

    I have to say I learned the hard way and, as of now, I reverted to where I was last Sunday night, in other words, I scrapped a whole week of frustrating long hours trying to develop a more sophisticated approach to 'Updating" the database for a handful of users. The interaction of DataSets, TableAdapters and manual kinds of updates is beyond my current system understanding. Hope to learn better approaches in the future, but there is no much than can be learned from Lynda.com or Udemy :(  At one point, I had one error coming from SQL Server complaining that ColumnX (which holds the Primary Key) data was missing in the course of copying its value from Source Row to Destination Row?, therefore creating a Null value and, sure enough, the Destination Table did not allow for Nulls in that column, so, naturally, it threw an Exception: how can I one "trace" anything through SQL Management Studio, the way one can trace, line by line, in VisualStudio? I don't think it can't be done that way? It's a LOT of work. Better be prepared to roll one's sleeves high.

    Thank you Karen and everyone else for your help.


    Saturday, June 27, 2020 8:12 PM
  • Roger,

    The first one is normally thrown if 2 user update the same record at almost the same time (and something the other one cannot see). There is nothing you can do than to put your update in a try and catch block, and if an exception is thrown take the kind of solution you want.  It can be as well because you are updating the same record twice.

    The second one means that you're updating a column with nothing while that is expected. This can have many reasons, one of those that you use a select statement with a star and than use a commandbuilder

    The third one we can only see partially, but as well set them in a try and catch block

    https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/try-catch-finally-statement


    Success
    Cor


    Sunday, June 28, 2020 6:38 PM
  • Dear Cor,

    Your comments are well taken, believe me. I had to go through figuring what each one of those error messages meant, to the best of my humble knowledge but there was a (tipping) point where I had to draw the line. Tomorrow morning my client is going to call with a series of bugs he found and expect me to turnaround fixes for each one; but I won't be able to give him an updated version with "ghosts" roaming somewhere in the background ready to pop out with more exceptions I won't have explanations for and draw me into never ending hours to understand the underlying complexity of what I'm "playing with", unknowingly. I don't want to become a slave of that application :(

    Sunday, June 28, 2020 8:15 PM
  • Hi roger.breton,

    How is the question going? Did you solve your problem?

    If you need further assistance, please let me know.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, June 30, 2020 3:01 AM
    Moderator
  • I reverted to what I consider a "safe" place for me where I seem to have "control" over what's going on in my application. I'm awaiting my client further feedback but latest tests are encouraging. I should close this thread.
    Tuesday, June 30, 2020 3:03 AM
  • Thanks ...thats working fine for me....really appreciated work.
    Wednesday, July 8, 2020 6:53 AM