如何覆盖TextBox文本属性以获得正确的绑定? [英] How do I override TextBox Text Property for correct Binding?

查看:73
本文介绍了如何覆盖TextBox文本属性以获得正确的绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



在我用来将数据绑定到控件的表单中,即使读取数据也行!我的意思是当来自bindingSource的数据绑定到表单时,一切正常。但是当我试图更新tableAdapter时,null值进入数据库!



这是 MoneyTextBox class:



  class  MoneyTextBox:TextBox 
{
public 覆盖 string 文字
{
设置
{
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;
}

受保护 覆盖 void OnTextChanged(EventArgs e)
{
if base .Text ==
{
.Text = 0;
}
else
{
if (< span class =code-keyword> 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);
}
}
.Select( this .TextLength, 0 );
}

受保护 覆盖 void OnKeyPress(KeyPressEventArgs e)
{
if ((e.KeyChar < 48 || e.KeyChar > 57 )&& e.KeyChar!= 8
{
e.Handled = ;
}
base .OnKeyPress(e);
}

受保护 覆盖 void OnKeyDown(KeyEventArgs e)
{
if (e.Control& e.KeyCode == Keys。 A)
this .SelectAll();
base .OnKeyDown(e);
}
}





有什么想法吗?如果有必要进一步解释,请告诉我们。在此先感谢...

解决方案

解决这个问题写这样的代码。



< pre lang =c#> public 覆盖 string 文字
{
设置
{
base .Text = value ;
this .OnTextChanged(EventArgs.Empty);
}
get
{
return skipComma( base .Text);
}
}


I written a class named MoneyTextBox that inherits TextBox.

Everything is OK but where I'm trying to bind data to the Text property of my MoneyTextBox.

In the form which I used to bind data to its controls, even reading data is OK! I mean when the data from bindingSource bound to the form, everything working correctly. But when I'm trying to "Update" the tableAdapter, null value went to the database!

Here is MoneyTextBox class:

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);
    }
}



Any idea? If it's necessary to more explain, tell me guys. Thanks in advance...

解决方案

to solve this problem write your code like this.

public override string Text
{
   set
   {
      base.Text = value;
      this.OnTextChanged(EventArgs.Empty);
   }
   get
   {
      return skipComma(base.Text);
   }
}


这篇关于如何覆盖TextBox文本属性以获得正确的绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆