# [.net] weird problem with ComboBox

## Recommended Posts

This is the second strange problem I've got since I began to use C# :-( I'm using two ToolStripComboBox to list fonts and sizes available in a text editor I'm writing. The text is handled by a RichTextBox. I want to update the selected items of the two Combos when the use clicks on the RTB client area, so that they show the current font and its size at cursor position. I catch the click event of the RTB and call the following method:
        public void TextClick(object sender, EventArgs args)
{
fontslist.SelectedItem = text.SelectionFont.Name;
fontsizes.SelectedItem = text.SelectionFont.Size.ToString();
}


The strange thing is that when I click on the text only the first Combo is updated. I need to click one more time to update the second. Someone knows what's happening? (I think to be able to provide a more precise description of the problem, if required). Thank you

##### Share on other sites
Did you define the items in the 2nd combobox as strings or ints? A string vs int comparrison will probably fail ("1" != 1), so the first thing you might want to check is if the values in fonsizes are indeed strings. If they're ints instead, try removing the ToString() call from your code to see if that works better... As for why it does work the 2nd time, I haven't got a clue...

Hope this helps somewhat though :)

##### Share on other sites
Thank you, but I've already checked it and these are strings. I thought that for some reasons an exception was thrown by the first method and so the method was interrupted before reaching the second call, but if I put other instructions these are executed...

##### Share on other sites
I cannot belive it! Just as the first strange behaviour I've encountered, even the second belongs to RichTextBox. This is how I made that thing work:
            Font f = text.SelectionFont;            fontslist.SelectedItem = f.Name;            fontsizes.SelectedItem = f.Size.ToString();

I don't know why, but trying to access to the font in two subsequent calls doesn't work properly: I first have to get the whole Font object, and only then I can read the fields.
Very strange...

Thank you anyway.