Decimator

I am making an accounting program which uses a series of text boxes to make calculations.

My problem is that, i cannot get the text box to update automatically when data is changed in other text boxes.

Private Sub TextBox6 .............. Handles TextBox6.TextChanged
TextBox6.Text = txtCost1.Text * txtQuantity1.Text

Above is my code for a 'total' text box(Textbox6), which i want to display the sum of cost1 multiplied by quanity1.

What i am after is for textbox6 to update as soon as i enter two value into cost1 and quanity1.

Currently i can only get the statement -

TextBox6.Text = txtCost1.Text * txtQuantity1.Text

to occur when i click inside textbox6.

Thanks
Decimator



Re: Visual Basic Express Edition Dynamic Methods

frxstrem

Here's a way to do it.

Timer.Tick

Code Snippet

Private Sub Timer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer.Tick

Try

TextBox6.Text = txtCost1.Text * txtQuantity.Text

Catch ex As expection

MsgBox("Error - " & ex)

End Try

End Sub

Just remember to set Timer.Enabled to True.






Re: Visual Basic Express Edition Dynamic Methods

spotty

The Following should show what you need.

Create a form with 3 textboxes, when the contents of textbox1 and textbox2 are numeric values it will recalculate textbox3 as the result of a multiplication. Change the values of textbox 1 or 2 and it will recalculate, put a non numeric in and it will clear textbox3.

Code Snippet

Public Class Form1

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged
If IsNumeric(TextBox1.Text) And IsNumeric(TextBox2.Text) Then
TextBox3.Text = CType(Val(TextBox1.Text) * Val(TextBox2.Text), String)
else

TextBox3.Text = ""

End If
End Sub
End Class






Re: Visual Basic Express Edition Dynamic Methods

spotty

Using a timer seems completely unneccessary. Us the textchanged event.





Re: Visual Basic Express Edition Dynamic Methods

Decimator

Thanks "frxstrem" it worked exactly the way i was hoping for.


Is this the only way in VB that this sort of thing can be done, Because although it works i need my code to be efficient as possible (as i am using it as a school project). Also the Message box MsgBox("Error - " & ex) Seems to generate a build error when i enter it into the code.


Thanks any way.





Re: Visual Basic Express Edition Dynamic Methods

nobugz

You are handling the wrong event. According to your code, the value of TextBox6 depends on either the value of txtCost1 or txtQuantity1. To automatically calculate the value, the Handles clause should be like this:

Private Sub UpdateTextbox6(ByVal sender As Object, ByVal e As EventArgs) Handles txtCost1.TextChanged, txtQuantity1.TextChanged
Try
TextBox6.Text = String.Format("{0:N2}", CDbl(txtCost1.Text) * CDbl(txtQuantity1.Text))
Catch ex As InvalidCastException
TextBox6.Text = ""
End Try
End Sub