1

$txtValue = New-Object System.Windows.Forms.MaskedTextBox

Using the mask $txtValue.Mask = "999,999,999.9999" doesn't allow me to enter, for example, 1.333 without typing 000,000,001.333 (or clicking directly at the correct position, but that's impractical). The mask $txtValue.Mask = "000,000,000.00" has almost the same issue.

I tried adding event functions, but it only edits the first number and fails:

$txtValue.add_TextChanged({ 
    $mytxt = $txtValue.Text 
    $txtValue.remove_TextChanged($thisEvent) 
    $myValue.Text = [double]::Parse($mytxt)
    $txtShareValue.add_TextChanged($thisEvent)
})

I also tried using the regex [^0-9.] and slicing the range if the text exceeds the length limit during an event, but this doesn't prevent entering multiple consecutive periods.

6
  • It looks like you're looking for string formatting on your double value like: '{0:0##,###,###.####}' -f 1.333. Unclear how 1.333 becomes ...001.3332 (where is that 2 coming from) ? Commented Nov 14, 2024 at 14:00
  • @SantiagoSquarzon The MaskedTextBox.Mask property uses a different syntax than .NET Numeric Formatting Strings which it says is in-fact derived from VB6, wow. Commented Nov 14, 2024 at 14:02
  • 3
    I'm unsure why you're using MaskedTextBox at all - it's intended for fixed-length, fixed-format text strings, not numeric values - for that you'd use NumericUpDown (which allows for direct input with formatting, and you can hide the up/down spinner buttons too, if you like). Commented Nov 14, 2024 at 14:08
  • @Dai What I was suggesting in my prev. comment, instead of using a Mask just parse and string formatting: $myValue.Text = '{0:0##,###,###.####}' -f [double]::Parse($mytxt) Commented Nov 14, 2024 at 14:20
  • The notes here can be useful. The mask definition in the question has ~the opposite functionality, though, but the properties described are the same. As mentioned, you can most probably make use of the NumericUpDown Control instead, if the masked text has no specific meaning, i.e., it's just numeric input Commented Nov 14, 2024 at 20:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.