Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need to have the num1textbox_KeyPress and num2textbox_KeyPress controls to onl

ID: 3798449 • Letter: I

Question

I need to have the num1textbox_KeyPress and num2textbox_KeyPress controls to only accept numbers, the period "." and the backspace key. I'm using Visual Studio 2015 as my program. Here is what I have so far:

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

Option Explicit On
Option Strict On
Option Infer Off

Public Class MainForm
Private Sub exitButton_Click(sender As Object, e As EventArgs) Handles exitButton.Click

' exit the program

Me.Close()

End Sub

Private Sub calcButton_Click(sender As Object, e As EventArgs) Handles calcButton.Click
' calculates either the sum of or the difference between two numbers

Dim num1 As Double
Dim num2 As Double
Dim answer As Double

' convert number input to Double

Double.TryParse(num1TextBox.Text, num1)
Double.TryParse(num2TextBox.Text, num2)

' calculate and display the sum or the difference

If additionRadioButton.Checked = True Then
answer = num1 + num2
ElseIf subtractionRadioButton.Checked = True Then
answer = num1 - num2
End If

answerLabel.Text = answer.ToString

End Sub

Private Sub num1TextBox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles num1TextBox.KeyPress

' only accept numbers, period, backspace keys

If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso
e.KeyChar <> ControlChars.Back Then
e.Handled = True
End If

End Sub

Private Sub num2TextBox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles num2TextBox.KeyPress

' only accept numbers, period, backspace keys

If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso
e.KeyChar <> ControlChars.Back Then
e.Handled = True
End If

End Sub
End Class

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

As you can see I have the num1textbox_KeyPress and num2textbox_KeyPress events almost finished I just need to add in the acceptance of the period "." key to them.

Thank you again!

Explanation / Answer

private num1TextBox_KeyPress(Object sender, KeyPressEventArgs e) Handles num1TextBox_KeyPress
{
   If( (e.KeyChar < "0" OR e.KeyChar > "9") And e.KeyChar != "." And e.KeyChar <> ControlChars.Back Then
       e.Handled = True
   End if
}

private num2TextBox_KeyPress(Object sender, KeyPressEventArgs e) Handles num2TextBox_KeyPress
{
   If( (e.KeyChar >= "0" OR e.KeyChar <= "9") And e.KeyChar != "." And e.KeyChar <> ControlChars.Back Then
       e.Handled = True
   End if
}

Above code will help you.

To check only "." you can simply put a condition in your code for e.KeyChar != "." / e.KeyChar <> "."

As you did not specify your programming language I am not able to compile I just did i as a Algorithm.