tags:

views:

125

answers:

2

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.

A: 

I'm guessing you're current implementation then is actually using the event handler. I'm not sure where you're trying to grab the value in your code, whether it's the form code itself or somewhere else.

What I'd probably do is the following:

protected override void OnClosing(CancelEventArgs e)
{
   // Save Value

   base.OnClosing(e);
}
Ian
I will try this, however because the exception is intermittent it will take a while to see if it resolves the problem. Is there any info to know if the combobox/datasource/valuemember is supposed to be disposed before/during or after the formclosing event
Spooky2010
I mean if the datasource link to the combobox can be disposed during any time during the form closing event then that would explain the intermitant nature of the problem and your solution should fix it. yes no ??
Spooky2010
@Spooky2010 : What's probably happening is that the controls are free'd up allowing the garbage collector to consume them. This might explain the problem, because the garbage collector runs at regular intervals, but you don't know when of those might be... You might be able to test the old code by adding a GC.Collect() just before the code where you are trying to get the value, if it fails everytime then that's most likely your problem.
Ian
A: 

Are you using the SelectedValue property of the combobox to access the selected item? This returns...

An object containing the value of the member of the data source specified by the ValueMember property. (MSDN)

If you haven't specified a ValueMember, this could be the problem. If you want to simply access the string value as displayed in the combobox, try using the SelectedText property instead.

Julian Martin
well you are correct in that this explains why i was getting the exception when i was testing different scenarios with a combobox that had items entered via the designer.There was no valuemember set However the application itself does have a value member set
Spooky2010
It might be that the property to which the ValueMember relates is not always set for all of the elements in your ArrayList - if this is null in any cases, then depending on what you try and do with the value, you may get a Null Exception. Can you post the code you're using to store the selected value?
Julian Martin