Brad Wethington - 1 year ago 63
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 decChildPricePerTicket As Decimal
Dim dblChildTicketsSold As Double
Dim decGrossChildSalesTotal As Decimal
Dim decGrossSalesTotal As Decimal
Dim decNetChildSalesTotal As Decimal
Dim decNetTotalSales As Decimal

'Put backcolor back to white after it turns yellow
txtChildPricePerTicket.BackColor = Color.White
txtChildTicketsSold.BackColor = Color.White

' Validate Inputs

Try

'Validate Adult Price Per Ticket is positive numeric
Else
Exit Sub
End If

Exit Sub
End If

'Validate amount of Adult tickets with positive numeric
Else
MessageBox.Show("Please enter a positive numeric value for amount for Adult Tickets Sold.")
Exit Sub
End If

MessageBox.Show("Please enter a positive numeric value for amount for Adult Tickets Sold.")
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 GrossChildSalesTotal
decGrossChildSalesTotal = CDec(CDec(txtChildPricePerTicket.Text) * CDbl(txtChildTicketsSold.Text))
lblGrossChildSalesTotal.Text = decGrossChildSalesTotal.ToString("c")

'Calculate total gross sales
lblGrossTotalSales.Text = decGrossSalesTotal.ToString("c")

'Calculate Net adult ticket sales 20%

'Calculate Net child ticket sales 20%
decNetChildSalesTotal = CDec(0.2 * decGrossChildSalesTotal)
lblNetChildSalesTotal.Text = decNetChildSalesTotal.ToString("c")

'Calculate Net total of adult and child tickets
lblNetTotalSales.Text = decNetTotalSales.ToString("c")

Catch
'Error Message
MessageBox.Show("All inputs must be valid positive numeric values")
End Try

End Sub

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: