我是VB的新手,我正在尝试制作一个计算器来计算售出的门票数量。我需要在dblAdultTicketsSold和dblChildTicketsSold中输入小数。我现在有限制,不允许负数,只允许数字。我感谢任何帮助。
Private Sub Button1_Click(sender As Object,e As EventArgs)处理btnCalculate.Click
''Declare local variables
Dim decAdultPricePerTicket As Decimal
Dim dblAdultTicketsSold As Double
Dim decGrossAdultSalesTotal As Decimal
Dim decChildPricePerTicket As Decimal
Dim dblChildTicketsSold As Double
Dim decGrossChildSalesTotal As Decimal
Dim decGrossSalesTotal As Decimal
Dim decNetAdultSalesTotal As Decimal
Dim decNetChildSalesTotal As Decimal
Dim decNetTotalSales As Decimal
'Put backcolor back to white after it turns yellow
txtAdultPricePerTicket.BackColor = Color.White
txtAdultTicketsSold.BackColor = Color.White
txtChildPricePerTicket.BackColor = Color.White
txtChildTicketsSold.BackColor = Color.White
' Validate Inputs
Try
'Validate Adult Price Per Ticket is positive numeric
If IsNumeric(txtAdultPricePerTicket.Text) Then
decAdultPricePerTicket = CDec(txtAdultPricePerTicket.Text)
Else
MessageBox.Show("Please enter a positive numeric value for Price per Adult Ticket.")
txtAdultPricePerTicket.Focus()
txtAdultPricePerTicket.BackColor = Color.Yellow
Exit Sub
End If
If decAdultPricePerTicket < 1 Then
MessageBox.Show("Please enter a positive numeric value for Price per Adult Ticket.")
txtAdultPricePerTicket.Focus()
txtAdultPricePerTicket.BackColor = Color.Yellow
Exit Sub
End If
'Validate amount of Adult tickets with positive numeric
If IsNumeric(txtAdultTicketsSold.Text) Then
dblAdultTicketsSold = CDbl(txtAdultTicketsSold.Text)
Else
MessageBox.Show("Please enter a positive numeric value for amount for Adult Tickets Sold.")
txtAdultTicketsSold.Focus()
txtAdultTicketsSold.BackColor = Color.Yellow
Exit Sub
End If
If dblAdultTicketsSold < 1 Then
MessageBox.Show("Please enter a positive numeric value for amount for Adult Tickets Sold.")
txtAdultTicketsSold.Focus()
txtAdultTicketsSold.BackColor = Color.Yellow
Exit Sub
End If
'Validate Child Price per Tickets is a positive numeric
If IsNumeric(txtChildPricePerTicket.Text) Then
decChildPricePerTicket = CDec(txtChildPricePerTicket.Text)
Else
MessageBox.Show("Please enter a positive numeric value for Price per Child Ticket.")
txtChildPricePerTicket.Focus()
txtChildPricePerTicket.BackColor = Color.Yellow
Exit Sub
End If
If decChildPricePerTicket < 1 Then
MessageBox.Show("Please enter a positive numeric value for Price per Child Ticket.")
txtChildPricePerTicket.Focus()
txtChildPricePerTicket.BackColor = Color.Yellow
Exit Sub
End If
'Validate amount of child tickets with positive numeric
If IsNumeric(txtChildTicketsSold.Text) Then
dblChildTicketsSold = CDbl(txtChildTicketsSold.Text)
Else
MessageBox.Show("Please enter a positive numeric value for amount of Child Tickets.")
txtChildTicketsSold.Focus()
txtChildTicketsSold.BackColor = Color.Yellow
Exit Sub
End If
If dblChildTicketsSold < 0 Then
MessageBox.Show("Please enter a positive numeric value for amount of Child Tickets.")
txtChildTicketsSold.Focus()
txtChildTicketsSold.BackColor = Color.Yellow
Exit Sub
End If
'Calculate and display GrossAdultSalesTotal
decGrossAdultSalesTotal = CDec(CDec(txtAdultPricePerTicket.Text) * CDbl(txtAdultTicketsSold.Text))
lblGrossAdultSalesTotal.Text = decGrossAdultSalesTotal.ToString("c")
'Calculate and display GrossChildSalesTotal
decGrossChildSalesTotal = CDec(CDec(txtChildPricePerTicket.Text) * CDbl(txtChildTicketsSold.Text))
lblGrossChildSalesTotal.Text = decGrossChildSalesTotal.ToString("c")
'Calculate total gross sales
decGrossSalesTotal = decGrossAdultSalesTotal + decGrossChildSalesTotal
lblGrossTotalSales.Text = decGrossSalesTotal.ToString("c")
'Calculate Net adult ticket sales 20%
decNetAdultSalesTotal = CDec(0.2 * decGrossAdultSalesTotal)
lblNetAdultSalesTotal.Text = decNetAdultSalesTotal.ToString("c")
'Calculate Net child ticket sales 20%
decNetChildSalesTotal = CDec(0.2 * decGrossChildSalesTotal)
lblNetChildSalesTotal.Text = decNetChildSalesTotal.ToString("c")
'Calculate Net total of adult and child tickets
decNetTotalSales = decNetChildSalesTotal + decNetAdultSalesTotal
lblNetTotalSales.Text = decNetTotalSales.ToString("c")
Catch
'Error Message
MessageBox.Show("All inputs must be valid positive numeric values")
End Try
End Sub
答案 0 :(得分:1)
如果您使用NumericUpDown,您可以将所有这些规则添加到控件...默认情况下它已经禁止字母字符,如果最小值设置为零并且增量设置为1,则它将禁止使用负数和十进制数字。如果您决定坚持使用文本框,我总是倾向于限制用户输入的内容,而不是在用户点击“提交”时将其显示错误消息。按钮或其他什么,你可以模仿NumericUpDown的文本过滤器,如下所示:
'Imports System.Text.RegularExpressions
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
TextBox1.Text = Regex.Replace(TextBox1.Text, "[^0-9]", "")
TextBox1.Select(TextBox1.Text.Length, 0)
End Sub
编辑:如果您需要用户在尝试输入十进制数时收到提醒,那么您可以尝试这样做:
Private Sub txtAdultPricePerTicket_TextChanged(sender As Object, e As EventArgs) Handles txtAdultPricePerTicket.TextChanged
If txtAdultPricePerTicket.Text.Contains(".") Then
MessageBox.Show("You can't buy a piece of a ticket dummy") '(may want to replace message text)
txtAdultPricePerTicket.BackColor = Color.Yellow
txtAdultPricePerTicket.Focus()
txtAdultPricePerTicket.Select(txtAdultPricePerTicket.Text.IndexOf("."), 1)
End If
End Sub
它将显示错误消息并突出显示小数,以便他们可以更改
答案 1 :(得分:0)
当使用键盘按下时,这将覆盖.
字符。
Private Sub tbKeyPress(sender As Object, e As KeyPressEventArgs) Handles dblAdultTicketsSold.KeyPress, dblChildTicketsSold.KeyPress
If e.KeyChar = "." Then
e.Handled = True
End If
End Sub