我刚从大学开始使用Visual Basic进行编码,但是我仍在学习如何进行编码。
我的问题是:当为表单中的重置按钮处理了Click
事件时,将执行所有TextBox
,即MsgBox("Please insert a value.")
。还有其他方法可以使用这些功能而不执行那些MsgBox
吗?单击重置按钮时,我不想举起MsgBox
。
这是我的代码:
Public Class MainWindowApp
'--------------------------------------------------------------------------------------------------------------------------------
'Program Info
'--------------------------------------------------------------------------------------------------------------------------------
'Program: Calculate the Average - Windows Forms
'Date: 10/10/2018
'Author: Edino de Souza
'Operation: Create a VB Form application that will prompt the user for 3 different numbers.
' It should Then calculate the average of those 3 numbers And display the Average To the user On the Console.
' The output message should read As follows
' The Average Of Number 1 And Number 2 And Number 3 Is Answer.
' Ex. The Average of 90 And 80 And 70 Is 80
' 2) Using if statements, prevent user input errors from occurring when clicking the Calculate button.
' The following should be checked:
' a) That a Value was provided for each edit box.
' b) That the value input is a number (Note the isNumeric function can be used for this).
' c) That the value entered in to the number boxes are not Less than 0 and not greater than 100.
' If the value falls under one of the conditions above, use the messagebox function to display a relevant message
' to the user (Note, if you want to place the cursor in the textbox being checked automatically, you can call the
' focus function of the textbox you are checking)
' 3) If you are not using a label control to report the result of the calculation, add one to your form. Once the
' average has been calculated, set the text of your label control to the average. Using if statements, based on
' the resulting grade, change the text as follows:
' 60% and above, change the text to green
' Below 60%, set the color to red
' The forecolor property of the label can be used to change the color.
'
'--------------------------------------------------------------------------------------------------------------------------------
'Change Log
'--------------------------------------------------------------------------------------------------------------------------------
'Date Programmer Change
'--------------------------------------------------------------------------------------------------------------------------------
'10/10/2018 Edino de Souza Initial/Final Version
'10/15/2018 Edino de Souza Adjusted reset button to reset variables too.
'10/17/2018 Edino de Souza First part of the addition of the IF statements
'10/17/2018 Edino de Souza Adjusted the code to perform what is required.
'--------------------------------------------------------------------------------------------------------------------------------
'Begin of Variables Session
Public num1 As Decimal
Public num2 As Decimal
Public num3 As Decimal
Public CalcAverage As Decimal
'End of Variables Session
'Begin of Input Session 1
Private Sub text_num1_TextChanged(sender As Object, e As EventArgs) Handles text_num1.TextChanged
'TextBox and Clear button controlling.
text_num1.Enabled = True 'Enable textbox 1
Bt_Clear1.Enabled = True 'Enable clear button 1
text_num2.Enabled = False 'Disable textbox 2
Bt_Clear2.Enabled = False 'Enable clear button 2
text_num3.Enabled = False 'Disable textbox 3
Bt_Clear3.Enabled = False 'Disable button 3
LabelCalcAverage.Enabled = False 'Disable the calculator button
Tx_Average.Enabled = False 'Disable the average textbox
'End - TextBox and Clear button controlling.
'Section to verify if all the conditions were satisfied.
If text_num1.Text = Nothing Then
MsgBox("Please insert a value.")
text_num2.Enabled = False
ElseIf IsNumeric(text_num1.Text) = False Then
MsgBox("Please insert a number")
text_num2.Enabled = False
ElseIf (text_num1.Text) < 0 Then
MsgBox("Please insert a number greater than zero")
text_num2.Enabled = False
ElseIf (text_num1.Text) > 100 Then
MsgBox("Please insert a number less than one hundred")
text_num2.Enabled = False
Else
Decimal.TryParse(text_num1.Text, num1)
text_num2.Enabled = True 'Enable textbox 2 after all validations
Bt_Clear2.Enabled = True 'Enable textbox 2 clear button after all validations
End If
'End - Section to verify if all the conditions were satisfied.
End Sub
'End of Input Session 1
'Begin of Input Session 2
Private Sub text_num2_TextChanged(sender As Object, e As EventArgs) Handles text_num2.TextChanged
If text_num2.Text = Nothing Then
MsgBox("Please insert a value.")
text_num3.Enabled = False
ElseIf IsNumeric(text_num2.Text) = False Then
MsgBox("Please insert a number")
text_num3.Enabled = False
ElseIf (text_num2.Text) < 0 Then
MsgBox("Please insert a number greater than zero")
text_num3.Enabled = False
ElseIf (text_num2.Text) > 100 Then
MsgBox("Please insert a number less than one hundred")
text_num3.Enabled = False
Else
Decimal.TryParse(text_num2.Text, num2)
text_num3.Enabled = True 'Enable textbox 3 after all validations
Bt_Clear3.Enabled = True 'Enable textbox 3 clear button after all validations
End If
End Sub
'End of Input Session 2
'Begin of Input Session 3
Private Sub text_num3_TextChanged(sender As Object, e As EventArgs) Handles text_num3.TextChanged
If text_num3.Text = Nothing Then
MsgBox("Please insert a value.")
ElseIf IsNumeric(text_num3.Text) = False Then
MsgBox("Please insert a number")
ElseIf (text_num3.Text) < 0 Then
MsgBox("Please insert a number greater than zero")
ElseIf (text_num3.Text) > 100 Then
MsgBox("Please insert a number less than one hundred")
Else
Decimal.TryParse(text_num3.Text, num3)
LabelCalcAverage.Enabled = True 'Enable the calculator button only after all fields were filled
End If
End Sub
'End of Input Session 3
'Caculation session
Private Sub LabelCalcAverage_Click(sender As Object, e As EventArgs) Handles LabelCalcAverage.Click
CalcAverage = (num1 + num2 + num3) / 3
Tx_Average.Enabled = True
'Function to show different colors on TextBox and Label
If CalcAverage < 60 Then
Tx_Average.ForeColor = Color.Yellow
Tx_Average.BackColor = Color.Red
Label_Average.BackColor = Color.Red
Tx_Average.Text = CalcAverage.ToString("###.##")
ElseIf CalcAverage >= 60 Then
Tx_Average.ForeColor = Color.Yellow
Tx_Average.BackColor = Color.Green
Label_Average.BackColor = Color.Green
Tx_Average.Text = CalcAverage.ToString("###.##")
End If
'End of function to show different colors on TextBox and Label
'Disable all textbox and clear buttons after Calculate button was pressed
text_num1.Enabled = False
Bt_Clear1.Enabled = False
text_num2.Enabled = False
Bt_Clear2.Enabled = False
text_num3.Enabled = False
Bt_Clear3.Enabled = False
'End of disable all textbox and clear buttons after Calculate button was pressed
End Sub
'End of caculation session
'Function to clear individuals textbox
Private Sub Bt_Clear1_Click(sender As Object, e As EventArgs) Handles Bt_Clear1.Click
text_num1.Clear()
num1 = Nothing
End Sub
Private Sub Bt_Clear2_Click(sender As Object, e As EventArgs) Handles Bt_Clear2.Click
text_num2.Clear()
num2 = Nothing
End Sub
Private Sub Bt_Clear3_Click(sender As Object, e As EventArgs) Handles Bt_Clear3.Click
text_num3.Clear()
num3 = Nothing
End Sub
'End of function to clear individuals textbox
'Messagebox button
Private Sub bt_about_Click(sender As Object, e As EventArgs) Handles bt_about.Click
MsgBox("Program design by Edino - W0430397")
End Sub
'End of messagebox button
'Function to clear all textbox
Private Sub B_Reset_Click_1(sender As Object, e As EventArgs) Handles B_Reset.Click
text_num1.Text = ""
text_num2.Text = ""
text_num3.Text = ""
Tx_Average.Clear()
CalcAverage = Nothing
Tx_Average.BackColor = Color.White
Label_Average.BackColor = Color.FromKnownColor(KnownColor.Control)
Tx_Average.Enabled = False
num1 = Nothing
num2 = Nothing
num3 = Nothing
text_num1.Enabled = True
Bt_Clear1.Enabled = True
text_num2.Enabled = False
Bt_Clear2.Enabled = False
text_num3.Enabled = False
Bt_Clear3.Enabled = False
LabelCalcAverage.Enabled = False
'Application.Restart() 'Another way to reset all TextBoxes but this one restart the application
End Sub
'End of function to clear all TextBoxes
End Class
答案 0 :(得分:1)
感谢您的帮助。我已经调整了代码,现在可以按照我想要的方式工作。这是我的代码:
Public Class MainWindowApp
'------------------------------------------------------------------------------------------------------------------------------------------
'Program Info
'------------------------------------------------------------------------------------------------------------------------------------------
'Program: Calculate the Average - Windows Forms
'Date: 10/10/2018
'Author: Edino de Souza
'Operation: This application will prompt the user for 3 numbers and calculate the average following somes rules and display to the user,
'also when show the average it will show In red If the average Is less than 60 and green if is equal or greater than 60.
'Rules to follow:
' a) The user must provide a value each edit box.
' b) The user must provide a number.
' c) The user must provide a value not Less than 0 and not greater than 100.
'If the value won't falls under one of the conditions above, the user will get messageboxes with each error.
' For example:
' MsgBox("Please insert a value on TextBox1")
' MsgBox("Please insert a number on TextBox1")
' MsgBox("Please insert a number greater than zero on TextBox1")
' MsgBox("Please insert a number less than one hundred on TextBox1")
'
'------------------------------------------------------------------------------------------------------------------------------------------
'Change Log
'------------------------------------------------------------------------------------------------------------------------------------------
'Date Programmer Change
'------------------------------------------------------------------------------------------------------------------------------------------
'10/10/2018 Edino de Souza Initial/Final Version
'10/15/2018 Edino de Souza Adjusted reset button to reset variables too.
'10/17/2018 Edino de Souza First part of the addition of the IF statements
'10/17/2018 Edino de Souza Adjusted the code to perform what is required.
'10/21/2018 Edino de Sozua Correction of the MSGBOX showing on reset button and added a better description of the operation.
'------------------------------------------------------------------------------------------------------------------------------------------
'Begin of Variables Session
Public num1 As Decimal
Public num2 As Decimal
Public num3 As Decimal
Public CalcAverage As Decimal
Public doNotShowMsgBox As Boolean 'This variable is used to enable or disable the validations part of the code to avoid MSGBOX.
'End of Variables Session
'Begin of Input Session 1 with validations
Private Sub text_num1_TextChanged(sender As Object, e As EventArgs) Handles text_num1.TextChanged
'TextBox and Clear button controlling.
text_num1.Enabled = True 'Enable textbox 1
Bt_Clear1.Enabled = True 'Enable clear button 1
text_num2.Enabled = False 'Disable textbox 2
Bt_Clear2.Enabled = False 'Enable clear button 2
text_num3.Enabled = False 'Disable textbox 3
Bt_Clear3.Enabled = False 'Disable button 3
LabelCalcAverage.Enabled = False 'Disable the calculator button
Tx_Average.Enabled = False 'Disable the average textbox
'End - TextBox and Clear button controlling.
'Section to verify if all the conditions were satisfied.
If doNotShowMsgBox = False Then
If text_num1.Text = Nothing Then
MsgBox("Please insert a value on TextBox1")
text_num2.Enabled = False
doNotShowMsgBox = True
text_num1.Clear()
doNotShowMsgBox = False
ElseIf IsNumeric(text_num1.Text) = False Then
MsgBox("Please insert a number on TextBox1")
text_num2.Enabled = False
doNotShowMsgBox = True
text_num1.Clear()
doNotShowMsgBox = False
ElseIf (text_num1.Text) < 0 Then
MsgBox("Please insert a number greater than zero on TextBox1")
text_num2.Enabled = False
doNotShowMsgBox = True
text_num1.Clear()
doNotShowMsgBox = False
ElseIf (text_num1.Text) > 100 Then
MsgBox("Please insert a number less than one hundred on TextBox1")
text_num2.Enabled = False
doNotShowMsgBox = True
text_num1.Clear()
doNotShowMsgBox = False
Else
Decimal.TryParse(text_num1.Text, num1)
text_num2.Enabled = True 'Enable textbox 2 after all validations
Bt_Clear2.Enabled = True 'Enable textbox 2 clear button after all validations
End If
ElseIf doNotShowMsgBox = True Then 'Won't execute validations part when active.
End If
'End - Section to verify if all the conditions were satisfied.
End Sub
'End of Input Session 1 with validations
'Begin of Input Session 2 with validations
Private Sub text_num2_TextChanged(sender As Object, e As EventArgs) Handles text_num2.TextChanged
If doNotShowMsgBox = False Then
If text_num2.Text = Nothing Then
MsgBox("Please insert a value on TextBox2")
text_num3.Enabled = False
doNotShowMsgBox = True
text_num2.Clear()
doNotShowMsgBox = False
ElseIf IsNumeric(text_num2.Text) = False Then
MsgBox("Please insert a number on TextBox2")
text_num3.Enabled = False
doNotShowMsgBox = True
text_num2.Clear()
doNotShowMsgBox = False
ElseIf (text_num2.Text) < 0 Then
MsgBox("Please insert a number greater than zero on TextBox2")
text_num3.Enabled = False
doNotShowMsgBox = True
text_num2.Clear()
doNotShowMsgBox = False
ElseIf (text_num2.Text) > 100 Then
MsgBox("Please insert a number less than one hundred on TextBox2")
text_num3.Enabled = False
doNotShowMsgBox = True
text_num2.Clear()
doNotShowMsgBox = False
Else
Decimal.TryParse(text_num2.Text, num2)
text_num3.Enabled = True 'Enable textbox 3 after all validations
Bt_Clear3.Enabled = True 'Enable textbox 3 clear button after all validations
End If
ElseIf doNotShowMsgBox = True Then 'Won't execute validations part when active.
End If
End Sub
'End of Input Session 2
'Begin of Input Session 3 with validations
Private Sub text_num3_TextChanged(sender As Object, e As EventArgs) Handles text_num3.TextChanged
If doNotShowMsgBox = False Then
If text_num3.Text = Nothing Then
MsgBox("Please insert a value on TextBox3.")
doNotShowMsgBox = True
text_num3.Clear()
doNotShowMsgBox = False
ElseIf IsNumeric(text_num3.Text) = False Then
MsgBox("Please insert a number")
doNotShowMsgBox = True
text_num3.Clear()
doNotShowMsgBox = False
ElseIf (text_num3.Text) < 0 Then
MsgBox("Please insert a number greater than zero on TextBox3")
doNotShowMsgBox = True
text_num3.Clear()
doNotShowMsgBox = False
ElseIf (text_num3.Text) > 100 Then
MsgBox("Please insert a number less than one hundred on TextBox3")
doNotShowMsgBox = True
text_num3.Clear()
doNotShowMsgBox = False
Else
Decimal.TryParse(text_num3.Text, num3)
LabelCalcAverage.Enabled = True 'Enable the calculator button only after all fields were filled
End If
ElseIf doNotShowMsgBox = True Then 'Won't execute validations part when active.
End If
End Sub
'End of Input Session 3
'Caculation session
Private Sub LabelCalcAverage_Click(sender As Object, e As EventArgs) Handles LabelCalcAverage.Click
CalcAverage = (num1 + num2 + num3) / 3
Tx_Average.Enabled = True
'Function to show different colors on TextBox and Label
If CalcAverage < 60 Then
Tx_Average.ForeColor = Color.Yellow
Tx_Average.BackColor = Color.Red
Label_Average.BackColor = Color.Red
Tx_Average.Text = CalcAverage.ToString("###.##")
ElseIf CalcAverage >= 60 Then
Tx_Average.ForeColor = Color.Yellow
Tx_Average.BackColor = Color.Green
Label_Average.BackColor = Color.Green
Tx_Average.Text = CalcAverage.ToString("###.##")
End If
'End of function to show different colors on TextBox and Label
'Disable all textbox and clear buttons after Calculate button was pressed
text_num1.Enabled = False
Bt_Clear1.Enabled = False
text_num2.Enabled = False
Bt_Clear2.Enabled = False
text_num3.Enabled = False
Bt_Clear3.Enabled = False
'End of disable all textbox and clear buttons after Calculate button was pressed
End Sub
'End of caculation session
'Function to clear individuals textbox
Private Sub Bt_Clear1_Click(sender As Object, e As EventArgs) Handles Bt_Clear1.Click
doNotShowMsgBox = True
text_num1.Clear()
num1 = Nothing
doNotShowMsgBox = False
End Sub
Private Sub Bt_Clear2_Click(sender As Object, e As EventArgs) Handles Bt_Clear2.Click
doNotShowMsgBox = True
text_num2.Clear()
num2 = Nothing
doNotShowMsgBox = False
End Sub
Private Sub Bt_Clear3_Click(sender As Object, e As EventArgs) Handles Bt_Clear3.Click
doNotShowMsgBox = True
text_num3.Clear()
num3 = Nothing
doNotShowMsgBox = False
End Sub
'End of function to clear individuals textbox
'Messagebox button
Private Sub bt_about_Click(sender As Object, e As EventArgs) Handles bt_about.Click
MsgBox("Program design by Edino - W0430397")
End Sub
'End of messagebox button
'Function to clear all textbox
Private Sub B_Reset_Click_1(sender As Object, e As EventArgs) Handles B_Reset.Click
doNotShowMsgBox = True
text_num1.Clear()
num1 = Nothing
text_num2.Clear()
num2 = Nothing
text_num3.Clear()
num3 = Nothing
doNotShowMsgBox = False
Tx_Average.Clear()
CalcAverage = Nothing
Tx_Average.BackColor = Color.White
Label_Average.BackColor = Color.FromKnownColor(KnownColor.Control)
Tx_Average.Enabled = False
text_num1.Enabled = True
Bt_Clear1.Enabled = True
text_num2.Enabled = False
Bt_Clear2.Enabled = False
text_num3.Enabled = False
Bt_Clear3.Enabled = False
LabelCalcAverage.Enabled = False
'Application.Restart() 'Another way to reset all textbox but this one restart the application
End Sub
'End of function to clear all textbox
End Class
答案 1 :(得分:0)
将验证代码移动到TextBox的Validating事件。
sizeof s