Sign in to follow this  
cignox1

[.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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.
Don't ask me why.
Very strange...

Thank you anyway.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this