ComboBox exception caused in winforms FormClosing event

Posted by Spooky2010 on Stack Overflow See other posts from Stack Overflow or by Spooky2010
Published on 2010-02-22T09:02:53Z Indexed on 2010/04/10 14:13 UTC
Read the original article Hit count: 460

Filed under:
|

Using c# vs2008 winforms

I have an application with a bunch of child winforms. Each time a form is closed I need to store the current selected value of a combobox that is on each form to a Global application variable so that I can track it and use it in new forms. The combobox is populated on form start via a method to set its datasource to an ArrayList of items

What I have found is if the combobox is populated with items in the designer, and you try to get the combobox value in the form closing event, I always get a NullReferenceException.

However if the comboBox has an datasource like I DO have in my application and I try to get the combobox value in the form closing event then I would say 95% of the time I DO NOT get the NullReferenceException, but I do get it 5% of the time. That ratio can even vary depending on what computer I run the application on. for example I have 1 computer where the exception always occurs.

My question is therefore what is (the best) way to get the value of the combobox, last thing before the form closes without causing the exception. I would def prefer to do it last thing before the form closes rather than track it with every selected index change event.

Any advice appreciated.

© Stack Overflow or respective owner

Related posts about c#

Related posts about winforms