Home > Software engineering >  VB to calculate the sum of any two integers and product
VB to calculate the sum of any two integers and product

Time:09-15

How in the input of time limit only integer
Public Class Form1

Private Sub Button1_Click (sender As Object, As EventArgs e) Handles for. Click
Dim a, b As Double
A=TextBox1. Text
B=TextBox2. Text
TextBox3. Text=a + b
End Sub

Private Sub Button2_Click (sender As Object, As EventArgs e) Handles Button2. Click
Dim a, b As Double
A=TextBox1. Text
B=TextBox2. Text
TextBox3. Text=a * b
End Sub
The End of the Class
I tried to make a double into integer but just calculate calculate integer, can still lose the decimal input,

CodePudding user response:

You can determine if the input is the decimal, use MsgBox error.
Judgment can be used:
 IsNumeric (TextBox1. Text) 
InStr (TextBox1. Text, ". ") & gt; 0

CodePudding user response:

1. Interface design:
TextBox, limit input, can only input numeric characters 0 to 9 and +, - 12 characters,
When the input other characters will be input "to eat", and limit +, - can only enter in the location of the first character,

2. The logic design:
In front of the "executive" operation, check the input of a TextBox, first is "legal value"; The second contains no decimal point, (reference 1/f)
Check through, then the values, perform calculations; Or direct messages prompt for illegal content (specific details you), don't perform,

CodePudding user response:

Private Sub Command1_Click ()

Dim a, b As Double

A=Text1. Text

B=Text2. Text

Text3. Text=a + b

End Sub

Private Sub Command2_Click ()

Dim a, b As Double

A=Text1. Text

B=Text2. Text

Text3. Text=a * b

End Sub

Private Sub Text1_KeyPress (KeyAscii As Integer)

If KeyAscii & lt; 48 the Or KeyAscii & gt; 57 Then KeyAscii=0

End Sub

Private Sub Text2_KeyPress (KeyAscii As Integer)

If KeyAscii & lt; 48 the Or KeyAscii & gt; 57 Then KeyAscii=0
  • Related