I’m working on an athletic workout journal application that will include an Interval / Countdown Timer.

I plan to build several versions of this application in different technologies (WPF, Web, Phone) but I’m building the first version in Windows Forms.

I want the user to be able to dynamically set the size of the “Stopwatch” display font, then resize the form and have the text re-center in the form, taking into consideration the new font size.

Stuff like this is easy in HTML, but not so much in Windows Forms.

Though I’m still in the early stages I thought I’d share the bit of math that I’ve come up with and invite any suggestions you might have.

Code Snippet
  1. using System.Text;
  2. using System.Windows.Forms;
  3.  
  4. namespace WorkOutTimer
  5. {
  6.     public partial class TimerMainForm : Form
  7.     {
  8.  
  9.         public TimerMainForm()
  10.         {
  11.             InitializeComponent();
  12.         }
  13.  
  14.         private void TimerMainForm_Resize(object sender, EventArgs e)
  15.         {
  16.             Int32 fs = System.Convert.ToInt32(textBoxFontSize.Text);
  17.             TimerClock.Font = new Font(FontFamily.GenericSansSerif, fs, FontStyle.Bold, GraphicsUnit.Pixel);
  18.             Point mp = new Point(((this.Size.Width – TimerClock.Width) / 2), 20);
  19.             TimerClock.Location =  mp;
  20.         }
  21.     }
  22. }