-1

In windows form application, C# using Visual Studio 2012

My mask is 0.00 but when I enter 3.00 it consider just 3 not 3.00 and also when enter 3.30 it consider 3.3 but if I enter 3.01 then this value being considered.

problem is I want that it consider last zero's also.

in form designer code:

this.maskedTextBox1.Location = new System.Drawing.Point(402, 121);
this.maskedTextBox1.Mask = "0.00";
this.maskedTextBox1.Name = "maskedTextBox1";
this.maskedTextBox1.PromptChar = '-';
this.maskedTextBox1.Size = new System.Drawing.Size(31, 22);
this.maskedTextBox1.TabIndex = 23;
this.maskedTextBox1.ValidatingType = typeof(System.DateTime);
this.maskedTextBox1.MaskInputRejected += new 

System.Windows.Forms.MaskInputRejectedEventHandler(this.maskedTextBox1_MaskInputRejected);
reVerse
  • 35,075
  • 22
  • 89
  • 84
REHAN
  • 1
  • Show the code inside `maskedTextBox1_MaskInputRejected` – James Nov 09 '14 at 09:32
  • maskedTextBox1_MaskInputRejected is empty.. because 0 allow 0 to 9 digits only by default as Microsoft's websit http://msdn.microsoft.com/en-us/library/system.windows.forms.maskedtextbox.mask%28v=vs.110%29.aspx – REHAN Nov 09 '14 at 09:38
  • Could you explain why you use as ValidatingType a DateTime instead of a decimal (or double...) – Steve Nov 09 '14 at 09:47
  • @steve actually i wanna validate double...becz users enter their gpa/marks in this field.. including the decimal point like 3.44 3.00 etc. but restrict to 0-9 digits only ..this works correctly but not work for special cases like i have mentioned above in my query. – REHAN Nov 09 '14 at 12:04

1 Answers1

0

The Mask property applies to the format displayed in the Maskedit and to the characters that can be typed. It doesn't automatically format the Text for you.

If you want to display a string with the format required you need to convert the user typed text to a valid decimal and then use composite formatting to prepare a string that display the double as you wish

double d = Convert.ToDouble(maskedTextBox1.Text)
Console.WriteLine(string.Format("{0:N2}", d));
Steve
  • 213,761
  • 22
  • 232
  • 286