Marcus
Marcus

Reputation: 75

How do you right align output in a label?

Here's my code, (GUI app):

const double TIPSTEP = 0.05;
double dinnerPrice = 10.00;
double tipRate;
double tip;
double lowRate, maxRate, minDinner, maxDinner;

lowRate = Convert.ToDouble(txtLowTip.Text);         
maxRate = Convert.ToDouble(txtHighTip.Text);          
minDinner = Convert.ToDouble(txtLowDinner.Text);         
maxDinner = Convert.ToDouble(txtHighDinner.Text);

lblOutput.Text = "   Price";           
for (tipRate = lowRate; tipRate <= maxRate; tipRate += TIPSTEP)
    lblOutput.Text += String.Format("{0, 8}", tipRate.ToString("F"));

lblOutput.Text += "\n-----------------------------------------------------------------\n";
tipRate = lowRate;
while (dinnerPrice <= maxDinner)
{
    lblOutput.Text += String.Format("{0, 8}", dinnerPrice.ToString("C"));
    while (tipRate <= maxRate)
    {
        tip = dinnerPrice * tipRate;
        lblOutput.Text += String.Format("{0, 8}", tip.ToString("F"));
        tipRate += 0.05;
    }
    dinnerPrice += minDinner;
    tipRate = lowRate;
    lblOutput.Text += "\n";
}

I think I'm doing it right. I wrote this first as a console app and it lined up perfectly. This time the numbers aren't right aligned like they should be, any suggestions?

Upvotes: 0

Views: 96

Answers (1)

Steve Wellens
Steve Wellens

Reputation: 20620

I wrote this first as a console app and it lined up perfectly.

Console windows use fixed-width fonts.

When you switched to a Windows GUI app, you now have variable-width fonts. 'M' is much wider than 'i'.

You can set the font of the label to be Courier or some other fixed-width font.

Upvotes: 1

Related Questions