0

I am trying to add regular expression to the code below to ensure that only numbers are accepted. The code is very basic it calculates the area of a square and put the result in a RichTextBox.Text I am using VB Visual Studio 2012. Any help will be greatly appreciated.

------------------------------------------------------
Public Class SquareArea
    Inherits ShapeArea
    Public Overrides Function Area() As Double
        Return (Me.Lengh ^ 2)
    End Function
End Class

------------------------------------------------------------
Public Class Square

    Private Sub Square_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub ResultButton_Click(sender As Object, e As EventArgs) Handles ResultButton.Click
        Dim area = New SquareArea
        area.Lengh = SideTextBox.Text
        ResultRichTextBox.Text = area.Area()
    End Sub

    Private Sub CloseSquareButton_Click(sender As Object, e As EventArgs) Handles CloseSquareButton.Click
        Me.Close()
    End Sub
End Class
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Akram Issa
  • 19
  • 1
  • 1
  • 4
  • Private Sub SideTextBox_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles WidthTextBox.Validating 'your code here If (System.Text.RegularExpressions.Regex.IsMatch(WidthTextBox.Text, "^[0-9]*$")) Then MessageBox.Show("Please enter only numbers.") WidthTextBox.Text.Remove(WidthTextBox.Text.Length - 1) End If End Sub – Akram Issa May 06 '14 at 02:57

3 Answers3

1

There are several ways of doing this. But the best would be to use the Validating Event of the SideTextBox textbox.

Private Sub SideTextBox_Validating  (ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtSideTextBox.Validating
'your code here
End Sub 

or You could also use its KeyPress Event so the user is prompted whenever they enter a non-numeric character.

Private Sub SideTextBox_KeyPress (ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSideTextBox.KeyPress
'your code here
End Sub 
Kwame
  • 45
  • 1
  • 1
  • 8
  • 1
    Don't use KeyPress, that won't handle the case where the user pastes in invalid input. Validating is fine, but even better would be to use a NumericUpDown control instead of a TextBox. – Cody Gray - on strike May 04 '14 at 06:04
  • @cody based on the qsn he asked, you could use keypress to test if the character entered is numeric...but thanks for your comment, I'll look into ur. – Kwame May 06 '14 at 13:16
0

Use this code...

Private Sub txtValue_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtValue.KeyPress

        If Char.IsDigit(e.KeyChar) = False And Char.IsControl(e.KeyChar) = False Then
            e.Handled = True
        End If
    End Sub 
  • This code worked but i have to use regular expression – Akram Issa May 06 '14 at 02:12
  • This code is not working, I am not sure why. Any Help? – Akram Issa May 06 '14 at 02:54
  • This code is not working, I am not sure why. Any Help Private Sub SideTextBox_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles WidthTextBox.Validating 'your code here If (System.Text.RegularExpressions.Regex.IsMatch(WidthTextBox.Text, "^[0-9]*$")) Then MessageBox.Show("Please enter only numbers.") WidthTextBox.Text.Remove(WidthTextBox.Text.Length - 1) End If End Sub – Akram Issa May 06 '14 at 02:57
0

I don't usually write in Visual Basic but you are looking to add RegularExpression to your class as a Class Member (System.Text.RegularExpressions namespace). Regular Expression Pattern shown below will allow only digits. Calling the Match method on regex returns a Match class which you can call Success on for a Boolean result (true/false)

You may have to make slight changes as I don't normally write in VB, but the expression and class are correct

'Declare Regular Expression Class, Compile once    
Dim RegularExpression regex As RegularExpression = New RegularExpression("^[0-9]*$", RegExOptions.Compile)
    Private Sub Square_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub ResultButton_Click(sender As Object, e As EventArgs) Handles ResultButton.Click
        Dim area = New SquareArea
    ' Insert Magic Here
    If regex.Match(SideTextBox.Text).Success=False Then
        MessageBox.Show("Invalid entry")
        Return
    End If
        area.Lengh = SideTextBox.Text
        ResultRichTe...

    Private Sub CloseSquareButton_Click(sender As Object, e As EventArgs) Handles CloseSquareButton.Click
        Me.Close()
    End Sub
    End Class
Pepto
  • 1,434
  • 10
  • 13