Search code examples
c#compact-framework

Setting fonts on controls that do not support a font


Using the CF 2.0 here. In order to support multiple languages, I set the font on each control (using a recursive function) when the form is created. I've found that some controls simply do not support a font property, so code like this:

cntrl.Font = new Font("Tahoma", 12.0f, FontStyle.Regular);

throws the NotSupportedException exception. Even calling:

if (cntrl.Font != null)

throws the same exception. To get around this, I've written a helper function as follows:

private static bool DoesControlSupportFont(Control cntrl)
{
    // Some control's do not support the Font property
    bool bSupported = true;
    if ((cntrl is HScrollBar) ||
        (cntrl is Panel) ||
        (cntrl is PictureBox) ||
        (cntrl is ProgressBar) ||
        (cntrl is TrackBar) ||
        (cntrl is VScrollBar))
        bSupported = false;

    return bSupported;
}

This works but seems inefficient and a little inelegant. Is this the recommended way to deal with controls that do not support a font? Perhaps there's a more efficient way (like using a try-catch block and dealing with the exception)?


Solution

  • I don't think there are other viable options to solve this issue than the ones you have already mentioned.

    In the full framework, you can test for the [Browsable(false)] attribute, to get an indication of what you are intended to use. This information has been stripped from the Compact Framework assemblies.

    If I were you I'd at least put your existing code in a try/catch block, specifically catching NotSupportedException, so future controls are automatically covered (anyone can subclass Control).

    I would leave in the hardcoded checks too as these cases aren't exactly exceptional.

    PS. Ran into this link a while ago, explaining how to "hide" properties from the CF designer.