c# - 字符串格式不对

标签 c# string-formatting

字符串格式在命令行中很好,但在带有标签的 gui 中它全部关闭。我认为我的格式是正确的

例子:

enter image description here

代码:

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

最佳答案

解决此问题的最简单方法是使用固定宽度的字体。除非您将每个数字放在自己的位置,否则您永远无法使它与比例字体正确对齐标签或文本框,或使用 DataGridView .

关于c# - 字符串格式不对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21716924/

相关文章:

c# - 列字符串格式

c# - Silverlight 的几何绘图和操作选项

c# - 如何测试表达式是否短路

c# - 将任何数字格式化为三位数?

python将字符串更改为日期

c# - 将 DateTime 格式化为字符串

c# - 比较 XML 文件是否相等的最佳方法是什么?

c# - EntityFramework 未使用默认值更新列

c# - Entity Framework 6 + MySQL : Model tables are not created automatically

WPF 图像工具提示