我是VB的新手,我正在尝试扩展我们在课堂上创建的BASIC256程序。我成功创建了应用程序的控制台版本(下面的代码)和Windows窗体版本。我需要帮助创建文本框的错误检查。对于TextBox1,我希望防止出现空白字段。对于TextBox3,我想要一个正数,不超过2位小数。此外,我知道代码可能过于复杂,任何清洁建议将不胜感激。
控制台代码
Module Module1
Sub Main()
Dim check_num(10) As String
Dim description(10) As String
Dim amount(10) As Decimal
Dim total_bal(10) As Decimal
Dim pointer As Integer
Dim x As Integer
pointer = 0
start:
Console.Clear()
Console.WriteLine("Enter check number for a check")
Console.WriteLine("Enter B for a balance inquiry")
Console.WriteLine("Enter C for a bank charge")
Console.WriteLine("Enter D for a deposit")
Console.WriteLine("Enter F if you are finished")
Console.WriteLine("Enter choice: ")
check_num(pointer) = Console.ReadLine()
Select Case check_num(pointer)
Case "B", "b"
x = 0
For x = 0 To 10
If total_bal(x) = 0 Then
x = x - 1
If x < 0 Then
x = 0
End If
Else
If total_bal(x) <> 0 Then
GoTo cont
End If
End If
Console.WriteLine("Balance is: {0}", total_bal(x))
Console.WriteLine("Press any key to continue...")
Console.ReadKey(True)
GoTo start
cont:
Next x
Case "D", "d"
x = 0
For x = 0 To 10
If total_bal(x) = 0 Then
Console.WriteLine("Amount of deposit: ")
amount(x) = Console.ReadLine()
Console.WriteLine("Descripiton of transaction: ")
description(x) = Console.ReadLine()
If x = 0 Then
total_bal(x) = total_bal(x) + amount(x)
Else
total_bal(x) = total_bal(x - 1) + amount(x)
End If
GoTo start
End If
Next x
Case "F", "f"
End
Case Else
x = 0
For x = 0 To 10
If total_bal(x) = 0 Then
Console.WriteLine("Amount of debit: ")
amount([x]) = Console.ReadLine()
Console.WriteLine("Descripiton of transaction: ")
description$([x]) = Console.ReadLine()
If x = 0 Then
total_bal(x) = total_bal(x) - amount(x)
Else
total_bal(x) = total_bal(x - 1) - amount(x)
End If
GoTo start
End If
Next x
End Select
GoTo start
End Sub
End Module
Windows窗体代码
Public Class Form1
Dim check_num(10) As String
Dim description(10) As String
Dim amount(10) As Decimal
Dim total_bal(10) As Decimal
Dim pointer As Integer = 0
Dim x As Integer
Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
If RadioButton1.Checked = True Then
Label3.Visible = True
TextBox3.Visible = True
Label2.Visible = True
TextBox1.Visible = True
Button1.Visible = True
End If
End Sub
Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
If RadioButton2.Checked = True Then
Label3.Visible = True
TextBox3.Visible = True
Label2.Visible = True
TextBox1.Visible = True
Button1.Visible = True
End If
End Sub
Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged
If RadioButton3.Checked = True Then
Label3.Visible = True
TextBox3.Visible = True
Label2.Visible = True
TextBox1.Visible = True
Button1.Visible = True
End If
End Sub
Private Sub RadioButton4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton4.CheckedChanged
If RadioButton4.Checked = True Then
Label3.Visible = False
TextBox3.Visible = False
Label2.Visible = False
TextBox1.Visible = False
Button1.Visible = False
End If
x = 0
For x = 0 To 10
If total_bal(x) = 0 Then
x = x - 1
If x < 0 Then
x = 0
End If
GoTo Line1
End If
Next x
Line1:
Label4.Visible = True
TextBox2.Visible = True
TextBox2.Text = total_bal(x).ToString("n2")
MessageBox.Show("Press OK to clear the screen.")
Label4.Visible = False
TextBox2.Visible = False
RadioButton4.Checked = False
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If RadioButton1.Checked = True Then
x = 0
For x = 0 To 10
If total_bal(x) = 0 Then
If TextBox3.Text <> String.Empty Then
If Not Decimal.TryParse(TextBox3.Text, amount(x)) Then
MessageBox.Show("Please use 12345.67 format only.")
TextBox3.Focus()
Else
amount(x) = TextBox3.Text
description(x) = TextBox1.Text
GoTo Line1
End If
End If
End If
Next x
Line1:
If x = 0 Then
total_bal(x) = total_bal(x) - amount(x)
Else
total_bal(x) = total_bal(x - 1) - amount(x)
End If
MessageBox.Show("Check Number " & description(x) & " was withdrawn" & vbCrLf & "in the amount of $" & amount(x))
End If
If RadioButton2.Checked = True Then
x = 0
For x = 0 To 10
If total_bal(x) = 0 Then
If TextBox3.Text <> String.Empty Then
If Not Decimal.TryParse(TextBox3.Text, amount(x)) Then
MessageBox.Show("Please use 12345.67 format only.")
TextBox3.Focus()
Else
amount(x) = TextBox3.Text
description(x) = TextBox1.Text
GoTo Line2
End If
End If
End If
Next x
Line2:
If x = 0 Then
total_bal(x) = total_bal(x) - amount(x)
Else
total_bal(x) = total_bal(x - 1) - amount(x)
End If
MessageBox.Show("A fee for " & description(x) & " was withdrawn" & vbCrLf & "in the amount of $" & amount(x))
End If
If RadioButton3.Checked = True Then
x = 0
For x = 0 To 10
If total_bal(x) = 0 Then
If TextBox3.Text <> String.Empty Then
If Not Decimal.TryParse(TextBox3.Text, amount(x)) Then
MessageBox.Show("Please use 12345.67 format only.")
TextBox3.Focus()
Else
amount(x) = TextBox3.Text
description(x) = TextBox1.Text
GoTo Line3
End If
End If
End If
Next x
Line3:
If x = 0 Then
total_bal(x) = total_bal(x) + amount(x)
Else
total_bal(x) = total_bal(x - 1) + amount(x)
End If
MessageBox.Show("A deposit for " & description(x) & " was made" & vbCrLf & "in the amount of $" & amount(x))
End If
RadioButton1.Checked = False
RadioButton2.Checked = False
RadioButton3.Checked = False
RadioButton4.Checked = False
TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
Label3.Visible = False
TextBox3.Visible = False
Label2.Visible = False
TextBox1.Visible = False
Button1.Visible = False
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim Response As Integer
Response = MessageBox.Show("Do you really want to exit?", "", _
MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If Response = vbYes Then
Me.Close()
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
RadioButton1.Checked = False
RadioButton2.Checked = False
RadioButton3.Checked = False
RadioButton4.Checked = False
TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
Array.Clear(check_num, 0, check_num.Length)
Array.Clear(description, 0, description.Length)
Array.Clear(amount, 0, amount.Length)
Array.Clear(total_bal, 0, total_bal.Length)
End Sub
End Class
答案 0 :(得分:0)
对于textbox1,请检查您在该特定文本框中使用的任何按钮。检查将是这样的
If textbox1.text = String.Empty (or you can use "") Then
'Throw an error of some sort
End If
对于textbox2,为该文本框添加一个KeyPress事件处理程序。它看起来像这样
Private Sub textbox1_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtbox1.KeyPress
Dim iKeyPress as Integer = Asc(e.KeyChar)
Dim hasDecimal as Boolean = False
IF iKeyPress < 48 Or iKeyPress > 57 Then ' Only Allow Numbers
e.Handled = True
End If
Select Case iKeyPress
Case 46 'Decimal
If hasDecimal = False Then
e.Handled = True
hasDecimal = True
End If
End Select
End Sub