5. TextBox

namespace Q5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        int er = 0;
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "***")
            {
                er = 0;
                return;
            }
            if (er == 3)
            {
                err.Text = "輸入超過3次";
                textBox1.Text = "???";
                textBox1.ForeColor = Color.Red;
                return;
            }
            int n = int.Parse(textBox1.Text);
            if (n < 1 || n > 10)
            {
                err.Text = "輸入錯誤";
                er++;
                return;
            }
            textBox1.ForeColor = Color.Black;

            err.Text = "";

            string t = "";

            for (int i = n; i > 0; i--)
            {
                t += i;
                for (int j = n - i; j > 0; j--)
                {
                    t += " 1";
                }
                t += "\r\n";
            }

            textBox2.Text = t;
        }
    }
}

Last updated