Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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:

C#
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...
Posted
Comments
BillWoodruff 31-May-15 4:20am    
Is this WPF ? You do not show the code where you are "trying to bind data to the Text property of my MoneyTextBox."
Ali Bagheri Shakib 31-May-15 4:54am    
No, it's Windows Form app.

In designer.cs :

private MoneyTextBox txtPrice;

in InitializeComponent():

this.txtPrice = new MoneyTextBox();
this.txtPrice.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.ghabzpardakhtBindingSource, "price", true));
Abhipal Singh 31-May-15 8:40am    
Can you please include "database update" code in your question as well?
Ali Bagheri Shakib 31-May-15 9:30am    
I used TableAapter.Update method

1 solution

to solve this problem write your code like this.

C#
public override string Text
{
   set
   {
      base.Text = value;
      this.OnTextChanged(EventArgs.Empty);
   }
   get
   {
      return skipComma(base.Text);
   }
}
 
Share this answer
 
Comments
Ali Bagheri Shakib 31-May-15 11:07am    
Why this change will solve the problem?! O.o

I think it occurs a loop! Since even the Design view not loading!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900