Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

automatic calculation and numeric validation

$
0
0
I am trying to validate the input to be numeric only and to be between 0 and 400. I am also trying to get the text box to automatically generate the calcultaion instead of using the calculate button. I have tried several different placings of the code but nothing seems to work completely correct. I have validated that the entries be numeric but not sure how to limit the response amount. Any tips on any of the code would be greatly appreciated.

Public Class Form1
    


    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        txtCostPerHour.Text = "0"
        txtEstimated.Text = "0"
        txtHours.Text = "0"
        txtPower.Text = "0"
        cbxAppliances.SelectedIndex = -1
        cbxAppliances.Focus()
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()

    End Sub

    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
        Dim Power As Integer = CInt(txtPower.Text)
        Dim Hours As Integer = CInt(txtHours.Text)
        Dim Price As Integer = CInt(txtCostPerHour.Text)
        Dim Estimated As Integer = (Power * Hours) * Price
        txtEstimated.Text = Estimated.ToString("C")


    End Sub

    Private Sub txtPower_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtPower.TextChanged
        If txtPower.TextLength = 0 Then
            txtPower.Text = "0"
        End If
        If Not IsNumeric(txtPower.Text) Then
            MessageBox.Show("Please enter numeric values only")
            Me.txtPower.Text = ""
        End If
    End Sub

    Private Sub txtHours_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtHours.TextChanged
        If txtHours.TextLength = 0 Then
            txtHours.Text = "0"
        End If
        If Not IsNumeric(txtHours.Text) Then
            MessageBox.Show("Please enter numeric values only")
            Me.txtHours.Text = ""
        End If
    End Sub

    Private Sub txtCostPerHour_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCostPerHour.TextChanged
        If txtCostPerHour.TextLength = 0 Then
            txtCostPerHour.Text = "0"
        End If
        If Not IsNumeric(txtCostPerHour.Text) Then
            MessageBox.Show("Please enter numeric values only")
            Me.txtCostPerHour.Text = ""
        End If
    End Sub
End Class


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>