将数据绑定到自定义文本框C#

将数据绑定到自定义文本框C#,c#,data-binding,binding,overriding,2-way-object-databinding,C#,Data Binding,Binding,Overriding,2 Way Object Databinding,我编写了一个名为MoneyTextBox的类,它继承了TextBox 一切正常,但我正在尝试将数据绑定到我的MoneyTextBox的Text属性 在我用来将数据绑定到控件的表单中,即使读取数据也可以!我的意思是当bindingSource中的数据绑定到表单时,一切都正常工作。但是当我试图更新tableAdapter时,空值进入了数据库 这里是MoneyTextBox类: class MoneyTextBox : TextBox { public override string Text

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

一切正常,但我正在尝试将数据绑定到我的
MoneyTextBox
Text
属性

在我用来将数据绑定到控件的表单中,即使读取数据也可以!我的意思是当bindingSource中的数据绑定到表单时,一切都正常工作。但是当我试图
更新
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);
    }
}
class MoneyTextBox:TextBox
{
公共重写字符串文本
{
设置
{
base.Text=值;
}
得到
{
返回skipComma(base.Text);
}
}
公共字符串skipComma(字符串str)
{
字符串strnew=“”;
如果(str==“”)
{
strnew=“0”;
}
其他的
{
strnew=str.Replace(“,”,String.Empty);
}
返回strnew;
}
受保护的覆盖void OnTextChanged(事件参数e)
{
如果(base.Text==“”)
{
此.Text=“0”;
}
其他的
{
如果(this.Text!=“”)
{
双d;
如果(!Double.TryParse(this.Text,out d))
{
this.Text=null;
返回;
}
如果(d==0)
{
此.Text=“0”;
}
其他的
this.Text=d.ToString(“#,#”,System.Globalization.CultureInfo.InvariantCulture);
}
}
this.Select(this.TextLength,0);
}
按键时受保护的覆盖无效(按键事件参数e)
{
如果((e.KeyChar<48 | | e.KeyChar>57)和&e.KeyChar!=8)
{
e、 已处理=正确;
}
按键(e);
}
受保护的覆盖无效OnKeyDown(KeyEventArgs e)
{
if(e.Control&e.KeyCode==Keys.A)
这是SelectAll();
base.OnKeyDown(e);
}
}
有什么想法吗?如果需要更多解释,告诉我伙计们。先谢谢你