Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

restrict keychars on textbox

Posted on 2013-01-07
8
Medium Priority
?
592 Views
Last Modified: 2013-01-07
Hi EE,

I need to restrict keyPress on a textBox. I only want the following :

Any digits before the dot, then after the dot only one of those:   25, 50 75

so for examples this is good:

4
34
12.25
9.50
66.75


this is wrong:

2.4
11.57
34.52
3.02

can you help ?
0
Comment
Question by:PhilippeRenaud
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 38751183
here:

Private Sub textBox1_KeyDown(sender As Object, e As KeyEventArgs)
	e.Handled = EvaluateText(e.KeyChar)
End Sub

Private Function EvaluateText(key As String) As Boolean
        If key = "." And textBox1.Text.Contains(".") Then
            Return False
        End If

        Dim data As String = textBox1.Text + key
       Dim d As Decimal = Decimal.Parse(data)
        Dim dec As Decimal = d - Math.Floor(d)
        Dim arr As Decimal() = {0.25D, 0.5D, 0.75D, 0}

        If arr.Contains(dec) Then
            Return True
        End If
        Return False

    End Function

Open in new window

0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 38751218
what is d ?
0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 38751222
nevermind ..
0
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
LVL 42

Accepted Solution

by:
Meir Rivkin earned 2000 total points
ID: 38751231
i;ve made a mistake in the function, here's working:

Private Sub textBox1_KeyDown(sender As Object, e As KeyEventArgs)
	e.Handled = EvaluateText(e.KeyChar)
End Sub

Private Function EvaluateText(key As String) As Boolean
  If key = "." And textBox1.Text.Contains(".") Then
            Return True
        End If

        If Not key = "." And Not TextBox1.Text.Contains(".") Then
            Return False
        End If

        Dim data As String = textBox1.Text + key
        Dim d As Decimal = Decimal.Parse(data)
        Dim dec As Decimal = d - Math.Floor(d)
        Dim arr As Decimal() = {0.25D, 0.5D, 0.75D, 0}

        If arr.Contains(dec) Then
            Return False
        End If
        Return True

    End Function

Open in new window

0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 38751241
if I add Cchar(Backspace) i would put it where?
0
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 38751252
it would be easier for you to have numeric control and combobox control with {.25,.50,.75,0} values.
screenshot
cause the validation code is overkill to what you want to achieve.
using 2 controls and your validation is complete, no errors.
0
 
LVL 1

Author Closing Comment

by:PhilippeRenaud
ID: 38751261
thanks, let me think about that combox Box yes.
thanks
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38751453
I'd suggest deriving from the existing TextBox so that you have a resusable component.

e.g.

Public Class QuarterlyTextbox
    Inherits TextBox

    Private _validKeys As New List(Of Keys) From
    {
            Keys.D0, Keys.NumPad0,
            Keys.D1, Keys.NumPad1,
            Keys.D2, Keys.NumPad2,
            Keys.D3, Keys.NumPad3,
            Keys.D4, Keys.NumPad4,
            Keys.D5, Keys.NumPad5,
            Keys.D6, Keys.NumPad6,
            Keys.D7, Keys.NumPad7,
            Keys.D8, Keys.NumPad8,
            Keys.D9, Keys.NumPad9,
            Keys.Decimal, Keys.OemPeriod
    }

    Private _controlKeys As New List(Of Keys) From
    {
            Keys.Back, Keys.Delete,
            Keys.Left, Keys.Right,
            Keys.Home, Keys.End
    }

    Protected Overrides Sub OnKeyDown(e As KeyEventArgs)
        If _controlKeys.Contains(e.KeyData) Then
            MyBase.OnKeyDown(e)
            Return
        End If

        If (Not _validKeys.Contains(e.KeyData)) OrElse
           (Me.Text.EndsWith(".25") OrElse Me.Text.EndsWith(".50") OrElse Me.Text.EndsWith(".75")) OrElse
           (Me.Text.Contains(".") AndAlso (e.KeyData = Keys.Decimal OrElse e.KeyData = Keys.OemPeriod)) OrElse
           (Me.Text.EndsWith(".") AndAlso e.KeyData <> Keys.D2 AndAlso e.KeyData <> Keys.NumPad2 _
                                  AndAlso e.KeyData <> Keys.D5 AndAlso e.KeyData <> Keys.NumPad5 _
                                  AndAlso e.KeyData <> Keys.D7 AndAlso e.KeyData <> Keys.NumPad7) OrElse
           ((Me.Text.EndsWith(".2") OrElse Me.Text.EndsWith(".7")) AndAlso e.KeyData <> Keys.D5 AndAlso e.KeyData <> Keys.NumPad5) OrElse
           (Me.Text.EndsWith(".5") AndAlso e.KeyData <> Keys.D0 AndAlso e.KeyData <> Keys.NumPad0) Then
            e.SuppressKeyPress = True
            e.Handled = True
            Return
        End If

        MyBase.OnKeyDown(e)
    End Sub

End Class

Open in new window


If you added this code to your project, then built it, you would then have a component you can drag from the ToolBox onto your form.

Screenshot
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question