开发者

Prevent combobox from resizing on font change

I have a combo box that has a list of font families in it. As you can guess I'm making a toolstrip for editing fonts in a rich text box control. The problem is when I change fonts it's resizing my combobox.

scrolling through different fonts causes the combo box to become "jumpy" and some fonts have a huge height which is causing for some hilarious problems.

Exhibit A:

Prevent combobox from resizing on font change

Exhibit B:

Prevent combobox from resizing on font change

Yeh... I'll show the code that I hav开发者_Go百科e so far... by the way the combobox is just bound to the font families collection.

    void box_SelectedIndexChanged(object sender, EventArgs e)
    {
        String text = ((Font)box.SelectedItem).Name;
        Font font = (Font)box.SelectedItem;


        BeginInvoke(new Action(() => box.Text = text));
        BeginInvoke(new Action(() => box.Font = font));
    }

Anyone have any ideas, if I can't find a solution I can just stop the font from changing and just display the name in the default font.


Using a ToolStripComboBox is the problem here I think. The .NET 2.0 ToolItem classes have a lot of residual, erm, features that never got addressed. WPF sucked the resources away. The tool strip is obviously not handling the resize very well. Nor does it make the rest of the form move down when it gets bigger which is by design.

The canonical font combobox uses owner draw to display the fonts in the dropdown list in their regular style. Without changing the font of the box itself. You really don't want the toolstrip to resize, that's just not a great UI.


The only way I can think of doing it is by creating a custom combobox control and deriving from said control. This will give you access to the variable ownerdraw which gives us a little more flexibility without having to mess around with the ItemHeight property. Hooking into one of the events, which dictate that the value of the control has changed.

You could then have a function like the following to calculate the new layout size:

 using (Font font = new Font(this.Font.FontFamily, (float)this.PreviewFontSize))
  {
    Size textSize;

    textSize = TextRenderer.MeasureText("yY", font);
    _itemHeight = textSize.Height + 2;
  }


I tried all these approaches with little success sadly. However I didn't realize it until today when I looked at how microsoft office implements it. They actually use the same font in the combo box for the selected item no matter what font is selected. So as much as I want to make it more custom I'm just going to use a uniform font for whatever font is shown in the selected index.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜