Journeyman1234
Journeyman1234

Reputation: 557

Event to fire after a System Windows Form element has been updated

I have a Windows form with a series of list boxes

The contents of subsequent lists change based on choices in earlier ones

All the boxes are bound to a BindingList with OnListChanged enabled

I use this to retain user-specified 'checked' items when elements are added or removed

However, I note that if I add an item to the list, it appears to update the UI only after all the other events have been fired

I've looked at the events I would expect to fire on the CheckedListBox, and in the related ViewModel, in order to catch the one which adds an item to the list, but so far without success

Can someone please advise me which event would allow me to call my 'CheckBoxes' method after the UI has been updated, otherwise they all get set to blank again until the form is closed and opened

    private void OnPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        // I've removed the debug statements, but this event when the bound list updates is fired before the UI updates

    }

    private void teams_checked_list_box_SelectedIndexChanged(object sender, EventArgs e)
    {
        // same with this event, and the other events like SizeChanged
    }

EDITED: On reflection, I realise that the problem occurs because I am not setting the true / false checked flag in the binding, because I couldn't figure out how to do it. If someone could point me in the right direction? Code currently looks like this:

            teams_checked_list_box.DataSource = Globals.ThisAddIn.TFSTeamsViewModel.ListOfTeamsFromVM.value;
            teams_checked_list_box.DisplayMember = "name";

So basically I am only updating the item name, and the check flag is handled on a later pass

Upvotes: 0

Views: 34

Answers (1)

veevan
veevan

Reputation: 204

How are you adding items to your list? Are you re-binding the listbox? There are basically two ways you can do this depending on your method of adding items:

  1. If you are re-binding, capture the event BEFORE the item is added. Loop through the CheckedItems property of your list box. Save the values. Add your item. Loop through the NEW values and recheck.

OR

  1. When the user checks the item in the listbox, capture that using the ItemChecked event and then store the value of the item somewhere. (Variable, hidden textbox -- ugly, but it works -- etc.) If you are only adding items to the BOTTOM of your list, you can store the index of the item. If you are re-sorting your list, you'll need to store a unique id to reference the item. (Note: you will also need to REMOVE the checked items from your stored value if the user UN-checks an item in your listbox.) Then, after you add your items to the listbox, loop back through and set the checked value on the appropriate listbox items.

Upvotes: 0

Related Questions