views:

247

answers:

4

I'd like to use Segoe UI 9 pt on Vista, and Tahoma 8 pt on Windows XP/etc. (Actually, I'd settle for Segoe UI on both, but my users probably don't have it installed.) But, these being quite different, they really screw up the layout of my forms. So... is there a good way to deal with this?

An example: I have a Label, with some blank space in the middle, into which I place a NumericUpDown control. If I use Segoe UI, the NumericUpDown is about 5 pixels or so to the left of the blank space, compared to when I use Tahoma. This is a pain; I'm not sure what to do here.

So most specifically, my question would be: how can I place controls in the middle of a blank space in my Labels (or CheckBoxes, etc.)? Most generally: is there a good way to handle varying fonts in Windows Forms?

Edit: I don't think people understood the question. I know how to vary my fonts based on OS. I just don't know how to deal with the layout problems that arise from doing so.

*Reply to ajryan, quick_dry*: OK, you guys understand the question. I guess MeasureString might work, although I'd be interested in further exploration of better ways to solve this problem.

The problem with splitting the control is most apparent with, say, a CheckBox. There, if the user clicks on the "second half" of the CheckBox (which would be a separate Label control, I guess), the CheckBox doesn't change state.

+1  A: 

First of all, you can find out which version of Windows you are using with the OperatingSystem.Platform property in the System library.

Second, it is possible that you may put your font settings in Resource files, and determine which resource file to use depending on certain conditions (e.g., your operating system version).

Personally though, I think it would be nice to let your user determine the fonts that they prefer as opposed to the font that you want for them to use.

Finally, you might want to take a look at WPF as this is one of the problem spaces that it was designed to solve.

Jon Limjap
+1  A: 

is the problem working out the placement of controls? i.e. you know font X and Y work on OS A and B, and give the layout you want with the text you're using on those systems?

MeasureString method might help in working out your layout in a way that you weren't tied to specific fonts.

float textWidth = graphics.MeasureString(someString, someFont).Width;

(would a change in text alignment work? I might be misunderstanding the problem too)

Steven Adams
+1  A: 

It's strange to need to layout one control within another. You might be solving an upstream problem wrong. Are you able to split the label into two labels with the updown between and maybe rely on a Windows Forms TableLayout panel?

If it's essential to try to position based on font sizes, you could use Graphics.MeasureString("String before updown", myLabel.Font)

If what you're after is font-dependent control positioning, you should probably retitle the question.


[edit] You can handle the click event of the "second half" part of the label and change the checkbox state on that event. The whole thing seems like a hack though. What is the problem being solved by this weird control layout? Why do you need an up-down in the middle of a label?

Aidan Ryan
+3  A: 

I second the usage of TableLayoutPanel for single-line inline controls.

I usually set each column and the first row to AutoSize and set each child control's Dock property to Fill in the designer. That gets the horizontal layout to display properly. To make the the text line up between labels/textboxes, set the TextAlign property to MiddleLeft.

If your text flows onto to the next line there's no easy solution. Using Graphics.MeasureString/TextRenderer.MeasureText and some fancy wrapping logic is your best bet :(

rpetrich