2

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");
              }
            }
        }
    }
}

1 Answer 1

9

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Or use an actual grid.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.