Data binding with custom TextBox c #

I wrote a class named MoneyTextBox

that inherits TextBox

.

Everything is fine, but where am I trying to bind data to a property of Text

mine MoneyTextBox

.

In the form I used to bind data to my controls, even reading the data is fine! I mean, when the bindingSource data is bound to the form, everything works correctly. But when I try Update

tableAdapter the null value hit the database!

Here is the 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 you need more explanation, tell me guys. Thanks in advance...

+3
override c # data-binding binding 2-way-object-databinding


source to share


No one has answered this question yet

Check out similar questions:

6155
What is the difference between string and string in C #?
3575
How to list a transfer?
2964
How to cast int to enum?
2397
What are the correct version numbers for C #?
2058
How do I get a consistent byte representation of strings in C # without manually specifying the encoding?
1743
What's the best way to give a C # auto property an initial value?
1742
How to calculate a person's age in C #?
1690
How does data binding work in AngularJS?
1476
Hidden features of C #?
1169
Calling the basic constructor in C #



All Articles
Loading...
X
Show
Funny
Dev
Pics