c#で、MaxLengthが文字単位ではなく、バイト単位の入力制限のかかるテキストボックス

StrLib.LenB, StrLib.LeftB はバイト単位での処理ができる前提。

public partial class TextBoxEx : TextBox
{
    public TextBoxEx()
    {
        InitializeComponent();

        this.TextChanged += BaseEdit_TextChanged;
    }

    public TextBoxEx(IContainer container)
    {
        container.Add(this);

        InitializeComponent();

        this.TextChanged += BaseEdit_TextChanged;
    }

    void BaseEdit_TextChanged(object sender, EventArgs e)
    {
        if (this.MaxLength != 0 && StrLib.LenB(this.Text) > this.MaxLength)
        {
            int n = this.SelectionStart;
            this.Text = StrLib.LeftB(this.Text, this.MaxLength);
            this.SelectionStart = n;
        }
    }
}