3

我写了一个名为MoneyTextBox继承的类TextBox

一切都很好,但是我试图将数据绑定到Text我的MoneyTextBox.

在我用来将数据绑定到它的控件的表单中,即使读取数据也可以!我的意思是当来自 bindingSource 的数据绑定到表单时,一切正常。但是当我尝试Update使用 tableAdapter 时,空值进入了数据库!

这里是MoneyTextBox类:

class MoneyTextBox : TextBox
{
    public override string Text
    {
        set
        {
            base.Text = value;
        }
        get
        {
            return skipComma(base.Text);
        }
    }

    public string skipComma(string str)
    {
        string strnew = "";
        if (str == "")
        {
            strnew = "0";
        }
        else
        {
            strnew = str.Replace(",", String.Empty);
        }
        return strnew;
    }

    protected override void OnTextChanged(EventArgs e)
    {
        if (base.Text == "")
        {
            this.Text = "0";
        }
        else
        {
            if (this.Text != "")
            {
                double d;
                if (!Double.TryParse(this.Text, out d))
                {
                    this.Text = null;
                    return;
                }
                if (d == 0)
                {
                    this.Text = "0";
                }
                else
                    this.Text = d.ToString("#,#", System.Globalization.CultureInfo.InvariantCulture);
            }
        }
        this.Select(this.TextLength, 0);
    }

    protected override void OnKeyPress(KeyPressEventArgs e)
    {
        if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8)
        {
            e.Handled = true;
        }
        base.OnKeyPress(e);
    }

    protected override void OnKeyDown(KeyEventArgs e)
    {
        if (e.Control & e.KeyCode == Keys.A)
            this.SelectAll();
        base.OnKeyDown(e);
    }
}

任何想法?如果需要更多解释,请告诉我。提前致谢...

4

0 回答 0