编辑代码

using System;
using System.Windows.Forms;

public class Form1 : Form
{
    private TextBox textBox1;
    private Button button1;

    public Form1()
    {
        textBox1 = new TextBox();
        button1 = new Button();

        button1.Text = "Submit";
        button1.Click += new EventHandler(button1_Click);

        textBox1.TextChanged += new EventHandler(textBox1_TextChanged);

        this.Controls.Add(textBox1);
        this.Controls.Add(button1);
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        if (textBox1.Text.Length > 10)
        {
            textBox1.BackColor = Color.Red;
        }
        else
        {
            textBox1.BackColor = Color.White;
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (textBox1.Text.Length > 10)
        {
            MessageBox.Show("Text is too long!");
        }
        else
        {
            MessageBox.Show("Text is valid!");
        }
    }

    [STAThread]
    public static void Main()
    {
        Application.Run(new Form1());
    }
}