MaskedEditTextbox mask numeric

I am using an MaskedEditTextbox but i am having trouble with the mask.
I want the user to be able to input the following mask

Min 1.0
max 999999.9
but when the user wants to input 12.4 it has to input first "12" then a lot of spaces then the ".4"
Is there a mask that allows 1.0 as wel as 999999.9
Who is Participating?
abelConnect With a Mentor Commented:
Hmm, the question is, should you really use the masked edit for this? It is meant for input that requires a certain defined mask, i.e., for a fixed length.

Anyway, here's a solution (better, a workaround), using the keypress event and using "######.#" as a mask. It will react on the decimal (assuming a dot) and will change the value to match the digits typed so far and it will place the cursor after the dot.

Private Sub MaskedTextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MaskedTextBox1.KeyPress
If e.KeyChar = "."c Then
    Dim len As Integer = MaskedTextBox1.Text.Length
    Dim s As String = MaskedTextBox1.Text.Replace(" ", "")
    MaskedTextBox1.Text = s.PadLeft(len)
    MaskedTextBox1.SelectionStart = MaskedTextBox1.Mask.Length - 1
End If
End Sub

Open in new window

dgbAuthor Commented:
Hi Vee_Mod,

Still getting used to the new way of asking questions.

This question is reagarding Microsoft visual studio 2005 (.NET)
Is this in Windows Forms application or in a ASP.NET application? Both have MaskedTextBox...

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Either way, the mask you are after is "#####0.0"
dgbAuthor Commented:
I've already tryed the "#" instead of the "0" but it doesn't work.
you still need to type spaces
(typing the "." or "," doesn't result in skipping to decimal side)
Can you answer my other question too, please? Then I can try to mimic your situation without being on the wrong track. If you use ASP.NET, please post the declarative part (aspx) code you have so far.

> (typing the "." or "," doesn't result in skipping to decimal side)
I don't understand what you mean here.

dgbAuthor Commented:
it is 2005 on vista

>> (typing the "." or "," doesn't result in skipping to decimal side)

>I don't understand what you mean here.

What i mean is that if i type "12.1" it stops with "12"
I can only type spaces until i reach the decimal sign than i can type the ".1"
> it is 2005 on vista

that was not the other question I meant. Do you use ASP.NET with VB.NET or do you use WinForms with VB.NET?

but from your description I understand winforms, so I will continue with that for a moment.
dgbAuthor Commented:
Correct i am using winforms
dgbAuthor Commented:
I have tested the code and it works.
Thanks for the workaround
As you pointed out maskedtextbox is not that handy for numeric input but on the other hand dates
are a different story.

Anyways thanks for the help

ps if you have an better solution wich works for numeric input as well dates please let me know.

glad I could help :)

If you need both in one control, I'd create my own based on a text control, but that's just me. For your current situation it only takes a few lines to workaround, so you're easily set. I'd stick to that unless you need more special behavior.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.