Combobox SelectedItem

  • Thread starter Thread starter Shmuel
  • Start date Start date
S

Shmuel

Hi,

I have a tabbed panel that has a combobox.
Now, if I want to get the selected item I do something like this:

String fromlang = ((Language)cbList_FromLang.SelectedItem).abbrev;

Which works as expected. But if I browse to another tab and then
come back, the combobox has lost the selected item.
I still see the text in the combobox, but if I try same as code above
I get an error stating: 'Object reference not set to an instance of an
object'.

My question is that is this kind of behavior a bug or is it just a feature?

Any ideas how to bypass this annoyance?

I have come up with one (to manually set it again):
try {
fromlang = ((Language)cbList_FromLang.SelectedItem).abbrev;
}
catch(Exception e) {
if (cbList_FromLang.Text.Length > 0)
{
string text = cbList_FromLang.Text;
int i = 9999;
int count = 0;
foreach (Language l in cbList_FromLang.Items)
{
if (l.name == text)
{
i = count;
}
count++;
}

if (i != 9999)
{
cbList_FromLang.SelectedIndex = i;
try
{
fromlang =
((Language)cbList_FromLang.SelectedItem).abbrev;
}
catch (Exception ex) { }
}
}
}

But it is really cumbersome do do this for every element on every tab.
So if somebody has any better idea, I would be very glad to hear about it.


Thanks,

Shmuel
 
Try setting the DropDownList property to DropDownList

---------
- G Himangi, LogicNP Software http://www.ssware.com
Shell MegaPack: GUI Controls For Drop-In Windows Explorer like File/Folder
Browsing Functionality (.Net & ActiveX Editions).
EZNamespaceExtensions: Develop namespace extensions rapidly in .Net and
MFC/ATL/C++
EZShellExtensions: Develop all shell extensions,explorer bars and BHOs
rapidly in .Net & MFC/ATL/C++
 
It has the same problem. After visiting another tab and coming back
there is no selected item, it is blank. I want them to be unchanged
even if I decide to visit another tab :)

The DropDown style doesn't loose the text, but the DropDownList does.
 
But if I browse to another tab and then
come back, the combobox has lost the selected item.

That is NOT normal behavior. I would feel safe betting that there is code
somewhere in your project that is resetting the selections on these combo
boxes when you change tabs. That's where you need to look. Step through,
step through.
 
Back
Top