MaskedTextBox and the Decimal Type

Hello All,

I'm hoping someone can help me with this one. I have a model object that represents a stock part. This part has a price. The price is stored on the database as type money. The price on the object is stored as type decimal.

I have a binding source that has this object as it's datasource and it is bound to serveral controls on the stock part view. The bound control for the price is a MaskedTextBox. When I input a price, say 2.35, and tab out of the control it is stored properly in the object but the bound control displays 235.__ I have tried the following masks, none of which work...

999.00
###.00
000.00

Can someone clue me in as to how I get decimal values to display correctly in the MaskedTextBox

Thanks,
Gary Short




Answer this question

MaskedTextBox and the Decimal Type

  • Megahalo

    Hello Mike,

    No, I'm afraid it didn't. I can get it to work with unbound controls, but as soon as I bind the control I get the symptoms I described above.

    Cheers,
    Gary
    http://www.garyshort.org



  • AlexanderPovalyaev

    Hello Mike,

    Yes I tried setting the promptChar = 0 (not demo'd in the screencast) but I get the same symptoms, only I get 0s instead of underscores, but the underlying problem of all numbers appearing at the left of the decimal point still persists.

    Thanks,
    Gary



  • Octurus

    Hello,

    Fair point, I don't get the underscore character if I set the prompt char = 0. However, the main point is that I if I input 2.35 what gets displayed is all the numbers to the left of the decimal point e.i. 235 - although the correct numeric value is stored in the object.

    I note you tried it with a DataTable and it worked okay, have you tried with your own model object as in my situation

    Cheers,
    Gary
    http://www.garyshort.org



  • hbaradi

    moved to windows forms forum

  • Stan H

    So did my recommendation work

  • peetfkl

    Hello Mike,

    Thanks for you perseverance with this matter. I'm afraid that didn't work either. I've created a screencast that shows the problem. you can view it at

    http://www.garyshort.org/screencasts/BoundMaskedTextBoxProblem.wmv

    if you wish.

    Thanks again for your help,
    Gary
    http://www.garyshort.org



  • LambrosKaralis

    I can tell from your video that the PromptChar was not set. I still see underscore characters. Have you ensured that PromptChar is set to 0

  • Taikonaut

    RE: However, the main point is that I if I input 2.35 what gets displayed is all the numbers to the left of the decimal point e.i. 235 - although the correct numeric value is stored in the object

    I see, that's because of where the input focus (cursor) is placed on the text box. Try implementing an OnEnter event handler for the text box and select all of the text, that way when a user starts typing number the entire string in the text box is replaced by what they type.

    Some controls automatically select all text when focus is placed on them. That's a good option to have so that all of the text gets replaced and not just some of it.



  • Charles Trindade

    That's strange since I tried it with a VS.NET project with a bound DataTable. I bound the MaskedTextBox to a DataTable. Some rows had DBNull values and some had decimal values. When I modified the data in the MaskedTextBox, it displayed all digits properly - I never get a "235.__" if the PromptChar is set to "0". I tried variations and each time I never got to see the underscore character. Note that I set the Mask to "###.##".

  • Adam Gregrory

    That functionality is not built in the MaskedTextBox, this control has no notion of what type of data it is dealing with. You may want to check the DecimalTextBox sample control at : http://msdn.microsoft.com/smartclient/codesamples/windowsforms/default.aspx



  • Slimjim100

    Set the PromptChar property of the MaskedTextBox control to 0 (the number zero) - that should solve your problem.

    E.g., this.maskedTextBox1.PromptChar = 0;

     



  • MaskedTextBox and the Decimal Type