Brad Wethington Brad Wethington - 2 months ago 11
Vb.net Question

How to keep decimals out of text box inputs in VB

I'm new to VB and I am trying to make a calculator to calculate the number of tickets sold. I need to make it where I can't put a decimal in dblAdultTicketsSold and dblChildTicketsSold. I have restrictions on now that wont allow negative numbers and will only allow numerics. I appreciate any help.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles 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

Answer

If you use a NumericUpDown you can add all these rules to the control...it already disallows alpha characters by default, and if the minimum is set to zero and increment set to 1, then it will disallow negative and decimal numbers. If you decide to stick with your textbox, I always prefer to limit the user's input as they're entering it rather than blow up their screen with error messages when they click the "submit" button or whatever, and you can mimic the NumericUpDown's text filters like so:

'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

Edit: If you need the user to be alerted when they try to enter a decimal number, then you could try this:

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

It will display an error message and highlight the decimal so they can change it