Automatic calculation

Is it possible to replace 'calculate' button?

Page 1 of 1

8 Replies - 2949 Views - Last Post: 12 October 2009 - 09:23 AM Rate Topic: -----

#1 wasatchu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-September 09

Automatic calculation

Post icon  Posted 07 October 2009 - 01:37 PM

Is it possible to replace the calculate button with a statement so that the percent complete automatically calculates when a box is checked or the text box is modified?

I have not been able to find anything except a 'do loop' that even comes close. Thanks in advance.


 Public Class Form1

	Dim C1, C2, C3, C4, Tb, Tb1 As Decimal
	Dim PctCmp As Decimal = C1 + C2 + C3 + C4 + Tb

	Public Sub CBoxCharter_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CBoxCharter.CheckedChanged

		If CBoxCharter.Checked = True Then C1 = 15 Else C1 = 0

	End Sub

	Public Sub CBoxPE_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CBoxPE.CheckedChanged

		If CBoxPE.Checked = True Then C2 = 10 Else C2 = 0

	End Sub

	Public Sub CBoxPCM_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CBoxPCM.CheckedChanged

		If CBoxPCM.Checked = True Then C3 = 10 Else C3 = 0

	End Sub

	Public Sub CBoxPL_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CBoxPL.CheckedChanged

		If CBoxPL.Checked = True Then C4 = 15 Else C4 = 0

	End Sub

	Private Sub Calc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Calc.Click
		Tb1 = CDec(TxBxBasis.Text)
		Tb = Tb1 / 2
		PctCmp = C1 + C2 + C3 + C4 + Tb
		TxBxFEL1.Text = CStr(PctCmp)
		ProgressBar1.Value = CInt(PctCmp)
	End Sub

End Class



Is This A Good Question/Topic? 0
  • +

Replies To: Automatic calculation

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Automatic calculation

Posted 07 October 2009 - 01:52 PM

You could put the code that is in the button_click event into the textbox_leave event and when you tab out of the textbox, it would perform the calculation.

You could even do it on the keypress event of the textbox so that it calculates each time a character is typed.

using your code, you could create a subroutine and put the code from your button_click event in it then call it in your checkchanged events

This post has been edited by CharlieMay: 07 October 2009 - 01:54 PM

Was This Post Helpful? 0
  • +
  • -

#3 CapeCAD  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 64
  • Joined: 10-August 09

Re: Automatic calculation

Posted 07 October 2009 - 03:47 PM

Add "handles" for each item you want to handle to your Calc_Click and it will execute every time

	Private Sub Calc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CBoxCharter.CheckedChanged, CBoxPE.CheckedChanged, CBoxPCM.CheckedChanged, CBoxPL.CheckedChanged, TxBxBasis.TextChanged


You can check IsNumeric(TxBxBasis.Text) or surround your code in a try/catch to handle a blank value in CDec(TxBxBasis.Text)
Try
		Tb1 = CDec(TxBxBasis.Text)
		Tb = Tb1 / 2
		PctCmp = C1 + C2 + C3 + C4 + Tb
		TxBxFEL1.Text = CStr(PctCmp)
		ProgressBar1.Value = CInt(PctCmp)
Catch ex As Exception

End Try

Was This Post Helpful? 1
  • +
  • -

#4 RBSprogram101  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 38
  • Joined: 28-March 09

Re: Automatic calculation

Posted 07 October 2009 - 03:52 PM

You could use a timer
Was This Post Helpful? 0
  • +
  • -

#5 wasatchu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-September 09

Re: Automatic calculation

Posted 08 October 2009 - 09:14 AM

Thanks CapeCAD, works just fine except that it seems to subtract instead of add and vice versa the CBoxCharter element. Using the same code inside the "Try" with the old Calculate button no problem - gives the correct answer. Still looking but nothing is jumping out.

Nice solution though, especially when I get it working. I have added the new code just in case. Again, thanks.

Public Class Form1

	Dim C1, C2, C3, C4, Tb, Tb1 As Decimal
	Dim PctCmp As Decimal = C1 + C2 + C3 + C4 + Tb

	Public Sub CBoxCharter_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CBoxCharter.CheckedChanged

		If CBoxCharter.Checked = True Then C1 = 15 Else C1 = 0

	End Sub

	Public Sub CBoxPE_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CBoxPE.CheckedChanged

		If CBoxPE.Checked = True Then C2 = 10 Else C2 = 0

	End Sub
	

	Public Sub CBoxPCM_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CBoxPCM.CheckedChanged

		If CBoxPCM.Checked = True Then C3 = 10 Else C3 = 0

	End Sub

	Public Sub CBoxPL_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CBoxPL.CheckedChanged

		If CBoxPL.Checked = True Then C4 = 15 Else C4 = 0

	End Sub
	Private Sub Calc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CBoxCharter.CheckedChanged, CBoxPE.CheckedChanged, CBoxPCM.CheckedChanged, CBoxPL.CheckedChanged, TxBxBasis.TextChanged

		Try
			Tb1 = CDec(TxBxBasis.Text)
			Tb = Tb1 / 2
			PctCmp = C1 + C2 + C3 + C4 + Tb
			TxBxFEL1.Text = CStr(PctCmp)
			ProgressBar1.Value = CInt(PctCmp)
		Catch ex As Exception

		End Try
	End Sub

	'Private Sub Calc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Calc.Click

	'	Tb1 = CDec(TxBxBasis.Text)
	'	Tb = Tb1 / 2
	'	PctCmp = C1 + C2 + C3 + C4 + Tb
	'	TxBxFEL1.Text = CStr(PctCmp)
	'	ProgressBar1.Value = CInt(PctCmp)

	'End Sub
End Class


Was This Post Helpful? 0
  • +
  • -

#6 CapeCAD  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 64
  • Joined: 10-August 09

Re: Automatic calculation

Posted 08 October 2009 - 03:41 PM

To insure the CheckBox value is changed prior to the calc, you can reduce the code as follows.

Public Class Form1

	Dim C1, C2, C3, C4, Tb, Tb1 As Decimal
	Dim PctCmp As Decimal = C1 + C2 + C3 + C4 + Tb

	Private Sub Calc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CBoxCharter.CheckedChanged, CBoxPE.CheckedChanged, CBoxPCM.CheckedChanged, CBoxPL.CheckedChanged, TxBxBasis.TextChanged
		Try
			If TypeOf (sender) Is CheckBox Then
				Select Case DirectCast(sender, System.Windows.Forms.CheckBox).Text
					Case "CBoxCharter"	'Replace with your CBoxCharter.text value
						If DirectCast(sender, System.Windows.Forms.CheckBox).Checked = True Then C1 = 15 Else C1 = 0
					Case "CBoxPE"	'Replace with your CBoxPE.text value
						If DirectCast(sender, System.Windows.Forms.CheckBox).Checked = True Then C2 = 10 Else C2 = 0
					Case "CBoxPCM"	'Replace with your CBoxPCM.text value
						If DirectCast(sender, System.Windows.Forms.CheckBox).Checked = True Then C3 = 10 Else C3 = 0
					Case "CBoxPL"	'Replace with your CBoxPL.text value
						If DirectCast(sender, System.Windows.Forms.CheckBox).Checked = True Then C4 = 15 Else C4 = 0
				End Select
			End If
			Tb1 = CDec(TxBxBasis.Text)
			Tb = Tb1 / 2
			PctCmp = Tb + C1 + C2 + C3 + C4
			TxBxFEL1.Text = CStr(PctCmp)
			ProgressBar1.Value = CInt(PctCmp)
		Catch ex As Exception

		End Try

	End Sub
End Class

This post has been edited by CapeCAD: 08 October 2009 - 06:07 PM

Was This Post Helpful? 0
  • +
  • -

#7 wasatchu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-September 09

Re: Automatic calculation

Posted 09 October 2009 - 09:28 AM

CapeCAD;

No joy. Tried the replacement code and nothing happens. A bit like what I had without the 'calculate' button, though your code is far beyond my comprehension.

Any idea why your original code is giving the strange response for CBoxCharter?

I appreciate your patience

This post has been edited by wasatchu: 09 October 2009 - 09:32 AM

Was This Post Helpful? 0
  • +
  • -

#8 CapeCAD  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 64
  • Joined: 10-August 09

Re: Automatic calculation

Posted 09 October 2009 - 04:04 PM

View Postwasatchu, on 9 Oct, 2009 - 10:28 AM, said:

CapeCAD;

No joy. Tried the replacement code and nothing happens. A bit like what I had without the 'calculate' button, though your code is far beyond my comprehension.

Any idea why your original code is giving the strange response for CBoxCharter?

I appreciate your patience


The original code has two handles for each CheckBox. Both handles will fire, but the Calc is firing before the CBoxCharter has a chance to update the values.

Another alternative is to call calc from each checkbox_checkchanged and textbox_textchanged like this:

Public Class Form1

	Dim C1, C2, C3, C4, Tb, Tb1 As Decimal
	Dim PctCmp As Decimal = C1 + C2 + C3 + C4 + Tb

	Public Sub CBoxCharter_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CBoxCharter.CheckedChanged

		If CBoxCharter.Checked = True Then C1 = 15 Else C1 = 0
		Calc_Click()
	End Sub

	Public Sub CBoxPE_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CBoxPE.CheckedChanged

		If CBoxPE.Checked = True Then C2 = 10 Else C2 = 0
		Calc_Click()
	End Sub
	

	Public Sub CBoxPCM_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CBoxPCM.CheckedChanged

		If CBoxPCM.Checked = True Then C3 = 10 Else C3 = 0
		Calc_Click()
	End Sub

	Public Sub CBoxPL_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CBoxPL.CheckedChanged

		If CBoxPL.Checked = True Then C4 = 15 Else C4 = 0
		Calc_Click()
	End Sub

	Private Sub TxBxBasis_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxBxBasis.TextChanged
		Calc_Click()
	End Sub

	Private Sub Calc_Click()

		Try
			Tb1 = CDec(TxBxBasis.Text)
			Tb = Tb1 / 2
			PctCmp = C1 + C2 + C3 + C4 + Tb
			TxBxFEL1.Text = CStr(PctCmp)
			ProgressBar1.Value = CInt(PctCmp)
		Catch ex As Exception

		End Try
	End Sub

	'Private Sub Calc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Calc.Click

	'	Tb1 = CDec(TxBxBasis.Text)
	'	Tb = Tb1 / 2
	'	PctCmp = C1 + C2 + C3 + C4 + Tb
	'	TxBxFEL1.Text = CStr(PctCmp)
	'	ProgressBar1.Value = CInt(PctCmp)

	'End Sub
End Class 

Was This Post Helpful? 0
  • +
  • -

#9 wasatchu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-September 09

Re: Automatic calculation

Posted 12 October 2009 - 09:23 AM

CapeCAD;

Seems to work perfectly and with code I can understand. Nice technique to use in the future. Almost missed the TextBasis change. Thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1