Print a Celsius to Fahrenheit conversion chart in C#

[Celsius to Fahrenheit]

A friend of mine cooks a lot using Japanese cookbooks. Because ovens in the United States only display temperature in degrees Fahrenheit (someone should make that a switch on the oven), she needs to convert temperatures from Celsius to Fahrenheit. This example prints a list of conversions from Celsius to Fahrenheit and vice versa that she can print and post on her refrigerator.

At design time I added a PrintDocument and a PrintPreviewDialog to the form. I then set the dialog’s PrintDocument property to the document.

The following code executes when you click the Print Chart button.

private void btnCreateChart_Click(object sender, EventArgs e)
{
    ppdChart.ShowDialog();
}

This code calls the dialog’s ShowDialog method. The dialog then makes the PrintDocument generate the graphics that it should display in its preview.

The PrintDocument then raises its PrintPage event to let your code generate the graphics that should be printed. The following code shows how this example draws the Celsius to Fahrenheit chart.

private void pdocChart_PrintPage(object sender,
    PrintPageEventArgs e)
{
    const float font_size = 12;
    const float dy = font_size * 1.5f;
    float x0 = e.MarginBounds.Left + 0.5f * 100;
    float x1 = x0 + 0.75f * 100;
    float y = e.MarginBounds.Top;
    e.Graphics.TextRenderingHint =
        TextRenderingHint.AntiAliasGridFit;
    using (Font font = new Font("Times New Roman", font_size))
    {
        using (StringFormat sf = new StringFormat())
        {
            sf.Alignment = StringAlignment.Center;

            e.Graphics.DrawString("Celsius", font,
                Brushes.Blue, x0, y, sf);
            e.Graphics.DrawString("Fahrenheit", font,
                Brushes.Blue, x1, y, sf);
            y += dy;

            for (int celsius = 60; celsius <= 250; celsius += 5)
            {
                float fahrenheit = celsius * 9f / 5f + 32;
                e.Graphics.DrawString(celsius.ToString(),
                    font, Brushes.Black, x0, y, sf);
                e.Graphics.DrawString(fahrenheit.ToString("0"),
                    font, Brushes.Black, x1, y, sf);
                y += dy;
            }

            y = e.MarginBounds.Top;
            float x2 = x1 + 1.2f * 100;
            float x3 = x2 + 0.75f * 100;
            e.Graphics.DrawString("Fahrenheit", font,
                Brushes.Blue, x2, y, sf);
            e.Graphics.DrawString("Celsius", font,
                Brushes.Blue, x3, y, sf);
            y += dy;

            for (int fahrenheit = 140; fahrenheit <= 500;
                fahrenheit += 10)
            {
                float celsius = (fahrenheit - 32) * 5f / 9f;
                e.Graphics.DrawString(fahrenheit.ToString(),
                    font, Brushes.Black, x2, y, sf);
                e.Graphics.DrawString(celsius.ToString("0"),
                    font, Brushes.Black, x3, y, sf);
                y += dy;
            }
        }
    }
}

This event handler sets up some values, sets the Graphics object’s TextRenderingHint property, and then creates a font. It also creates a StringFormat object to center text horizontally.

The code draws the headings Celsius and Fahrenheit centered over the X coordinates x0 and x1 and then moves the value y down to the next line.

Next the code loops through Celsius values ranging from 60 to 250. For each value the code coverts the value in Celsius into a value in Fahrenheit and draws the values on the page, moving the value of y down for the next line each time.

After it has displayed the Celsius to Fahrenheit conversions, the program resets the Y coordinate to the top of the page a repeats the same steps, this time displaying Fahrenheit to Celsius conversions.

That’s all there is to it. When you run the program and press the button, the program displays a print preview of the conversions. You can click the preview dialog’s Print button to send the result to the printer.

You could make a lot of changes to this example. For instance, if you use a smaller font and move the columns close together, you could probably make a chart that would fit in your wallet, particularly if (like my friend) you only need to convert Celsius to Fahrenheit. You might also alternate the colors of the rows so it’s easier to follow across the rows.


Download Example   Follow me on Twitter   RSS feed   Donate




About RodStephens

Rod Stephens is a software consultant and author who has written more than 30 books and 250 magazine articles covering C#, Visual Basic, Visual Basic for Applications, Delphi, and Java.

This entry was posted in Uncategorized and tagged , , , , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *