7 Replies - 1370 Views - Last Post: 18 February 2013 - 09:03 PM Rate Topic: -----

#1 jab8288   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 25-June 12

automatic calculation and numeric validation

Posted 18 February 2013 - 06:48 PM

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



Is This A Good Question/Topic? 0
  • +

Replies To: automatic calculation and numeric validation

#2 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1515
  • View blog
  • Posts: 3,832
  • Joined: 01-February 13

Re: automatic calculation and numeric validation

Posted 18 February 2013 - 07:05 PM

Hey jab8288,
You could use something like this in the text changed event to control it not going over 400 and do something just like it to control the number from being under 0.
        If Val(txtPower.Text) > 400 Then txtPower.Text = "400"



and in these lines instead of setting the text to "" you could set it to "0".
        If Not IsNumeric(txtPower.Text) Then
            MessageBox.Show("Please enter numeric values only")
            Me.txtPower.Text = ""
        End If



Is this for homework ?

This post has been edited by IronRazer: 18 February 2013 - 07:06 PM

Was This Post Helpful? 0
  • +
  • -

#3 andrewsw   User is online

  • head thrashing
  • member icon

Reputation: 6623
  • View blog
  • Posts: 27,046
  • Joined: 12-December 12

Re: automatic calculation and numeric validation

Posted 18 February 2013 - 07:31 PM

Quote

I am also trying to get the text box to automatically generate the calcultaion instead of using the calculate button.

You need to decide when this should happen; that is, what event triggers it. If you want it to happen each time a textbox is completed, having checked that the other controls hold suitable values, then I would probably create a separate helper-function for this.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is online

  • head thrashing
  • member icon

Reputation: 6623
  • View blog
  • Posts: 27,046
  • Joined: 12-December 12

Re: automatic calculation and numeric validation

Posted 18 February 2013 - 07:37 PM

BTW At some point you might look into the Validating event for controls. This allows you to set up validation rules for controls and not have to worry about the various combinations of values, and to introduce an ErrorProvider icon. But, as I say, at some point..
Was This Post Helpful? 0
  • +
  • -

#5 jab8288   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 25-June 12

Re: automatic calculation and numeric validation

Posted 18 February 2013 - 08:16 PM

View PostIronRazer, on 18 February 2013 - 07:05 PM, said:

Hey jab8288,
You could use something like this in the text changed event to control it not going over 400 and do something just like it to control the number from being under 0.
        If Val(txtPower.Text) > 400 Then txtPower.Text = "400"



and in these lines instead of setting the text to "" you could set it to "0".
        If Not IsNumeric(txtPower.Text) Then
            MessageBox.Show("Please enter numeric values only")
            Me.txtPower.Text = ""
        End If



Is this for homework ?

Thanks IronRazor!

I have been staring at this for hours. I had figured out how to everything you see but could just not go any further. Now that i see it, I can't believe I even asked. Brain block I guess. Yes this is for an assignment. Also, I should probably set it to have to be > 0 seeng as how the multiplication of 0 = 0 shouldn't I?
Was This Post Helpful? 0
  • +
  • -

#6 jab8288   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 25-June 12

Re: automatic calculation and numeric validation

Posted 18 February 2013 - 08:23 PM

View Postandrewsw, on 18 February 2013 - 07:31 PM, said:

Quote

I am also trying to get the text box to automatically generate the calcultaion instead of using the calculate button.

You need to decide when this should happen; that is, what event triggers it. If you want it to happen each time a textbox is completed, having checked that the other controls hold suitable values, then I would probably create a separate helper-function for this.

What is a helper-function?
Was This Post Helpful? 0
  • +
  • -

#7 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1515
  • View blog
  • Posts: 3,832
  • Joined: 01-February 13

Re: automatic calculation and numeric validation

Posted 18 February 2013 - 08:36 PM

[quote name='jab8288' date='18 February 2013 - 10:16 PM' timestamp='1361243763' post='1805334']

View PostIronRazer, on 18 February 2013 - 07:05 PM, said:

I have been staring at this for hours. I had figured out how to everything you see but could just not go any further. Now that i see it, I can't believe I even asked. Brain block I guess. Yes this is for an assignment. Also, I should probably set it to have to be > 0 seeng as how the multiplication of 0 = 0 shouldn't I?


Well, I didn`t really test anything out or look too hard at it but, yes it would need to bee greater than 0. Don`t fret about the brain block it happens to me too. I just wanted to check if homework because we are not suppose to write the code for homework but, we can give pointers and some examples. If you need more help i will be checking in every few minutes as i am working on a program of my own at the same time. So sorry about the delay.
Was This Post Helpful? 0
  • +
  • -

#8 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1515
  • View blog
  • Posts: 3,832
  • Joined: 01-February 13

Re: automatic calculation and numeric validation

Posted 18 February 2013 - 09:03 PM

Have you learned how to use the (Else) in the (If) statements yet ? If so you could check if any of the textboxes = 0 in the (btnCalculate_Click) event to tell the user that all textboxes are not filled in correct before multiplying. Like :

        If x = 0 Or y = 0 Or z = 0 Then
            MessageBox.Show("Information is not filled in")
        Else
            'do the math functions
        End If


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1