how to get data in a label to be organized into colums

70 Views Asked by At

I am attempting to write a program that will use a post conditional loop. The loop is calculating Celsius to Fahrenheit conversions. I have the equation down but i cannot get the output that way i need it to be. I need to output both temperatures to the same label while keeping the C and F temps in their own organized column. here is a link showing exactly what i am attempting to do:

enter image description here

After that, i need to right justify the table while keeping the C temp data in the same place. I am not sure how to do either of these and apparently after 2 and a half hours of searching on google, it seems google doesnt know either. I am a beginner in C# and i cannot find anything that says how to do this specifically so i can learn to do it myself. Any help will greatly be appreciated. Happy holidays!!!

This is my current code i have, Im still working on it but the data keeps getting bunched together, and i need only to have the words Fahrenheit and Celceius displayed only once at the top

double Celceius=0;
double Fahrenheit;
lblOUT.BackColor=Color.Red;

while (Celceius <= 100)
{
    Fahrenheit = 32 + (Celceius * 1.8);

    lblOUT.Text += "Celceius"+ "Fahrenheit"+Convert.ToString(Celceius) + Environment.NewLine + Convert.ToString(Fahrenheit);

    Celceius += 5;
}
1

There are 1 best solutions below

4
On BEST ANSWER

You can use String.PadLeft function to pad spaces before your Fahrenheit values. You can give maximum length, 3 in your case, to pad necessary spaces. It will only pad spaces if length is smaller. You can check the details on below MSDN link.

http://msdn.microsoft.com/en-us/library/system.string.padleft%28v=vs.110%29.aspx

Below code will generate the list, but remember to use font which has equal size for each character such as "Courier New"

    private void button1_Click(object sender, EventArgs e)
    {
        double Celceius = 0;
        double Fahrenheit;
        lblOUT.BackColor = Color.Red;

        lblOUT.Text = "Celceius" + "".PadLeft(20) + "Fahrenheit";

        while (Celceius <= 100)
        {

            Fahrenheit = 32 + (Celceius * 1.8);

            lblOUT.Text += Environment.NewLine + Celceius.ToString().PadLeft(8,' ') + "".PadLeft(20) + Fahrenheit.ToString().PadLeft(10,' ');

            Celceius += 5;
        } 
    }