Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use of MaskedTextBox with currency? 1

Status
Not open for further replies.

Paco75

Programmer
Oct 11, 2001
239
0
0
US
Hi,

I'm trying to put a MaskedTextBox in which users can type a decimal number like a money amount i.e. 12345678.99 i put 99999999.00 in the "Mask" property but i dont like the way it works with the dot... they have to move the keyboard's cursor to the dot after typing the amount to write...

Examples of what i want:
type '123^ . ' then press the dot, the cursor moves to the dot ready to write decimals like this ' 123.^ '
type ' 123^ . ' then press the dot, the cursor moves to the dot ready to write decimals like this ' 123.^ '

the ^ represents the position of the keyboard cursor.

Any idea on how to achieve this?


thanks
 
hi , just happened on this , normally on the Foxpro site . In looking at conversions from Foxpro to dotNet ( but use C# , not VB ) , looked at all the commercial options , codeplex projects etc etc . The only good solution I've found is this little set of assemblies from . At 50 USD , it seems to plug a huge number of gaps in winforms etc . Let me know what u think
 
I think it could have done it but i prefered to use textBox keydown and keypress events.

Private variable in the form to act as a flag
Code:
' Boolean flag used to determine when a character other than a number is entered. 
Private nonNumberEntered As Boolean = False

Method to call in Keydown event of textBoxes
Code:
    ' Handle the KeyDown event to determine the type of character entered into the control.
    Private Sub DecimalValidate(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        Dim txtObject As MaskedTextBox = sender
        ' Initialize the flag to false.
        nonNumberEntered = False

        ' Determine whether the keystroke is a number from the top of the keyboard. 
        If e.KeyCode < Keys.D0 OrElse e.KeyCode > Keys.D9 Then
            ' Determine whether the keystroke is a number from the keypad. 
            If e.KeyCode < Keys.NumPad0 OrElse e.KeyCode > Keys.NumPad9 Then
                ' Determine whether the keystroke is a backspace. 
                If e.KeyCode <> Keys.Back Then
                    If e.KeyCode = Keys.Decimal Or e.KeyCode = Keys.OemPeriod Then
                        If txtObject.Text.Contains(".") Then
                            'A dot is already present in text so it is considered as a non numeric
                            'so it will dismissed.
                            nonNumberEntered = True
                        End If
                    Else
                        ' A non-numerical keystroke was pressed.  
                        ' Set the flag to true and evaluate in KeyPress event.
                        nonNumberEntered = True
                    End If
                End If
            End If
        End If
        'If shift key was pressed, it's not a number. 
        If Control.ModifierKeys = Keys.Shift Then
            nonNumberEntered = True
        End If
    End Sub

Code to put in the keypress event of textboxes, cancel keypress if the flag is true
Code:
        ' Check for the flag being set in the KeyDown event. 
        If nonNumberEntered = True Then
            ' Stop the character from being entered into the control since it is non-numerical.
            e.Handled = True
        End If
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top