This is a migrated thread and some comments may be shown as answers.

RadMaskedEditBox and cost entry

7 Answers 196 Views
MaskedEditBox
This is a migrated thread and some comments may be shown as answers.
Scott
Top achievements
Rank 1
Scott asked on 25 Nov 2010, 01:04 AM
I am using the radMasked editBox for currency entry and am having a few issues.
Mask = c
MaskType = Numeric

1. Negative values are not handled nicely.
   The only way to stop a currency field being negative is to press the - sign.
   You should be able to  a. Most importantly select the - sign and press delete or backspace
                                        b. Select all the text including the - sign and type over it (currently the new value is still negative)
                                        c. press the + sign should make it positive

2. Is there a way to stop the user being able to enter negative values as we don't want them most time, and all i can think to do is try to stop the - keypress before it is filtered to the control.

3. Another important issue is typing over existing values
if you have a value of $800.12 entered and you select just the 8 and change it to another number lets say 7 the result is $7.12, it took out the 0's following the 8.
This is potentially hazardous, a user could easily rush through and not notice the 0's have been removed and then someone could be out quite a bit of money.

7 Answers, 1 is accepted

Sort by
0
Scott
Top achievements
Rank 1
answered on 25 Nov 2010, 01:42 AM
Another issue i just found is

4. if you copy a currencey from say notepad with a value like 80.00, then select all the text in the radMaskedEditbox
and press CTRL+V (paste) the result in the Masked edit box is as follows $080.00.00 note the two decimal points, this can not be achived by simple typing a second decimal so the paste should not allow it. 
0
Scott
Top achievements
Rank 1
answered on 25 Nov 2010, 04:27 AM
And one more.

5. The MaxLength does not function.
I have opened the control editor for the RadMaskedEditBox and under the RadMaskedEditBoxItem treenode there is a property for MaxLength, setting this has no effect what so ever.

We use the RadMaskedEditBox to enter cost fields which are saved to the database so it would be nice to limit these to only values that the SQL server money field will accept, Max length would do the trick quite nicely.

I know we could add a validation routine to our form to check this and report to the user about invalid values, but even better is them not being able to add them in the first place, and this would save us a lot of repeat code adding validation.

Is there another way to set the max length or limit the values able to be entered.
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 25 Nov 2010, 11:46 AM
Hello Scott,

There are quite a few questions here so I may need to answer them over a few replies.
Please see the code below. It is just a RadMaskedEditBox on a form.

This should address a couple of your issues:
1: Does not allow minus values
2: Corrects the copy and paste problem

I will try and get back to you with a workaround to your other issues.
Imports Telerik.WinControls
Imports Telerik.WinControls.UI
Imports System
  
  
Public Class Form1
  
    Private WithEvents m_TextBox As RadTextBoxItem
  
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.RadMaskedEditBox1.Mask = "c"
        Me.RadMaskedEditBox1.MaskType = MaskType.Numeric
        Me.RadMaskedEditBox1.MaskedEditBoxElement.TextBoxItem.MaxLength = 5
        m_TextBox = Me.RadMaskedEditBox1.MaskedEditBoxElement.TextBoxItem
    End Sub
  
    Private Sub TextBox_KeyPress(ByVal sender As System.Object, ByVal e As KeyPressEventArgs) Handles m_TextBox.KeyPress
        ' use this code to stop minus being entered
        If e.KeyChar = CChar("-") Then
            e.Handled = True
        End If
    End Sub
  
    Private Sub TextBox_KeyDown(ByVal sender As System.Object, ByVal e As KeyEventArgs) Handles m_TextBox.KeyDown
        ' use this to correct the copy paste issue
        Try
            If e.KeyCode = Keys.V AndAlso (e.Modifiers And Keys.Control) <> Keys.None Then
                Me.RadMaskedEditBox1.Value = Clipboard.GetText()
                e.Handled = True
                e.SuppressKeyPress = True
            End If
        Catch ex As FormatException
            e.Handled = False
            e.SuppressKeyPress = False
        End Try
    End Sub
  
End Class

hope that helps
Richard
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 25 Nov 2010, 12:11 PM
Hello Scott,

I tried the internal TextBoxItem for MaxLength as well, but this didn't function as I expected, so the following code has been revised to include a max length functionality.

I hope this all helps. Please mark as answer anything you found useful and if you need more information, just let me know.

Richard
Imports Telerik.WinControls
Imports Telerik.WinControls.UI
Imports System
  
  
Public Class Form1
  
    Private WithEvents m_TextBox As RadTextBoxItem
  
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.RadMaskedEditBox1.Mask = "c"
        Me.RadMaskedEditBox1.MaskType = MaskType.Numeric
        m_TextBox = Me.RadMaskedEditBox1.MaskedEditBoxElement.TextBoxItem
        ' specify the number of digits. In this case 6 = a max of 9,999.99 (6 integer characters)
        Me.RadMaskedEditBox1.MaxLength = 6
    End Sub
  
    Private Sub TextBox_TextChanging(ByVal sender As System.Object, ByVal e As TextChangingEventArgs) Handles m_TextBox.TextChanging
        ' use this to stop long input
        Dim chrArray() As Char
        Dim numbers As Integer
        Dim result As Integer
        chrArray = m_TextBox.Text.ToCharArray
        For intCounter = 0 To chrArray.Length - 1
            If Integer.TryParse(chrArray(intCounter), result) Then
                numbers = numbers + 1
            End If
            If numbers > Me.RadMaskedEditBox1.MaxLength Then
                e.Cancel = True
            End If
        Next
    End Sub
  
    Private Sub TextBox_KeyPress(ByVal sender As System.Object, ByVal e As KeyPressEventArgs) Handles m_TextBox.KeyPress
        ' use this code to stop minus being entered
        If e.KeyChar = CChar("-") Then
            e.Handled = True
        End If
    End Sub
  
    Private Sub TextBox_KeyDown(ByVal sender As System.Object, ByVal e As KeyEventArgs) Handles m_TextBox.KeyDown
        ' use this to correct the copy paste issue
        Try
            If e.KeyCode = Keys.V AndAlso (e.Modifiers And Keys.Control) <> Keys.None Then
                Me.RadMaskedEditBox1.Value = Clipboard.GetText()
                e.Handled = True
                e.SuppressKeyPress = True
            End If
        Catch ex As FormatException
            e.Handled = False
            e.SuppressKeyPress = False
        End Try
    End Sub
  
End Class
0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 25 Nov 2010, 01:06 PM
Hello Scott,

Ok, let's take them one by one,
First, for the Num + sign to make the value positive and for the Num - to make the value negative you can use the following:
public class CustomMaskedEditBox : RadMaskedEditBox
{
    public override string ThemeClassName
    {
        get
        {
            return typeof(RadMaskedEditBox).Name;
        }
    }
 
    public CustomMaskedEditBox()
    {
        this.textBoxItem.KeyDown += new KeyEventHandler(TextBoxItem_KeyDown);
         
    }
 
    void TextBoxItem_KeyDown(object sender, KeyEventArgs e)
    {
        double value;
        double.TryParse(this.Value.ToString(), out value);
 
        if (e.KeyValue == 109)
        {
             
            if (value <= 0)
            {
                value *= -1;
                this.Value = value;
            }
        }
 
        if (e.KeyValue == 107) // plus sign, the Keys.oemplus is not recognized as the num + sign
        {
            if (value <= 0)
            {
                value *= -1;
                this.Value = value;
            }
        }
    }
}

For the copy paste you can use Richards suggestion, it should do what you need.

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga
Telerik WinForms MVP

0
Scott
Top achievements
Rank 1
answered on 26 Nov 2010, 12:15 AM
Thanks for the quick responses guys,

these workarounds did fix my problems, I am adding them all to a class that inherits from the radMaskeditBox and making a new currencey text box to use within our program.

The only real issue we have left was the point 3. from my first post in this thread.

Following your samples I have also coded a fix to allow deleting the minus sign from the start to make a positive number
You just add this code to a class inheriting the RadMaskEditBox

    Private Const WM_KEYDOWN As Long = &H100
  

    Protected Overrides Function ProcessKeyPreview(ByRef m As System.Windows.Forms.Message) As Boolean

        ' If allowing neagative values this is a fix to allow deleting the minus sign
        If m.Msg = WM_KEYDOWN Then
            If m.WParam = Keys.Back Or m.WParam = Keys.Delete Then
                If Me.Value < 0 Then
                    If Me.SelectedText = "-" Then
                        Me.Value *= -1
                    End If
                End If

            End If
        End If

        Return MyBase.ProcessKeyPreview(m)
    End Function

0
Richard Slade
Top achievements
Rank 2
answered on 26 Nov 2010, 12:26 AM
Hello,

Glad that this is all sorted for you and we were able to help.
Regards,
Richard
Tags
MaskedEditBox
Asked by
Scott
Top achievements
Rank 1
Answers by
Scott
Top achievements
Rank 1
Richard Slade
Top achievements
Rank 2
Emanuel Varga
Top achievements
Rank 1
Share this question
or