Listbox sorting problem

I am having a problem after a listbox is sorted. I use the listbox for a variety of reports with radio buttons off to the side to let the user choose what report he wants to see. Everything works fine until the user chooses to see a different report after he has sorted the listbox by clicking on a column header. As soon as the method that generates the new report method calls listbox.HasHeading = True, the HeaderPressed code runs again and usually crashes the program. I have not found a way to disable the HeaderPressed event.

What’s causing the crash? Is is somehow just the event being invoked that crashes or is it your event code not handling that specific situation?

If it’s your event code, and since it’s triggered by other code, a simple fix is to add a flag. Set it true before listbox.HasHeading = True and then false after. In HeaderPressed you can filter out the scenario with “if skipHeaderPressed then return” at the top. This is sorta shaky though, it’d be better for the event code to be robust enough to handle any situation it’s invoked into.

Basically, rather than disable the event I think you’ll have to handle the situation.

I added if me.ListCount < 2 then exit at the start of the event code which prevents the crash. But the column the user last selected in the previous report is still highlighted.

yeah, I don’t use headers much and doing some experiments can’t find how to get rid of that sort indicator either. Should be simple, just set a constant, but which one? Hopefully this isn’t a cosmetic bug.

You can disable the highlighting of the row with

Listbox.listindex = -1

Since you re-configure the listbox for each result, I think the program is trying to sort a column that isn’t there anymore.
Try this before you re-enable Listbox.HasHeading

Listbox.SortedColumn = -1

This will set the listbox to un-sorted