majortom84
majortom84

Reputation: 651

string format is not right

The string formats just fine in command line but in gui with a label it is all off. I think my formatting is correct

Example:

enter image description here

Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DisplayMultiplicationTableGUI
{
    public partial class Form1 : Form
    {
        int i, j;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label1.Text = String.Format("{0,3}", " ");
            for (i = 1; i <= 10; i++)
                label1.Text += String.Format("  {0,3}", (i).ToString());


            for (i = 1; i <= 10; i++)
            label2.Text += String.Format("\n{0,3} ", (i).ToString());

            for (i = 1; i <= 10; i++)
            {
                for (j = 1; j <= 10; j++)
                    label3.Text += String.Format("{0,3}  ", (i*j).ToString());
                label3.Text += String.Format("\n");
              }
            }
        }
    }
}

Upvotes: 2

Views: 312

Answers (1)

Robert Harvey
Robert Harvey

Reputation: 180787

The easiest way to solve this is to use a fixed-width font. You're never going to get this to line up correctly with a proportional font, unless you put each number in its own label or text box, or use a DataGridView.

Upvotes: 9

Related Questions