|
-
Jul 6th, 2004, 04:23 AM
#1
Thread Starter
Lively Member
Need Help
i have a textbox and when the user key in 1000000
it will change the format to 10,000.00
and when user key in 1000050 it will help me to change the format to 10,000.50
can i know how to change the format ???? without click any button
===========================================
Private Sub gross_TextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gross_TextBox.TextChanged
gross_TextBox.Text = Format(Val(gross_TextBox.Text), "#,###.##")
taxable_TextBox.Text = Val(gross_TextBox.Text) + Val(bonus_TextBox.Text) - Val(children_TextBox.Text) - Val(personal_TextBox.Text) - Val(parent_TextBox.Text) - Val(epf_TextBox.Text)
End Sub
===========================================
-
Jul 6th, 2004, 07:57 AM
#2
Hyperactive Member
I think the problem is, you are providing optional characters for the end of your format string, try this:
VB Code:
Private Sub TextBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.LostFocus
Dim GIDollars As Double = Val(Me.TextBox1.Text)
Me.TextBox1.Text = Format(GIDollars, "#,###.00")
End Sub
Whadayamean it doesn't work....
It works fine on my machine!

-
Jul 6th, 2004, 08:46 AM
#3
Thread Starter
Lively Member
if i add in the coding above i get a problem is that the textbox event will get a problem that all the textbox cannot be sum up
coz every textbox i have an event that is sum it up
if i change the format of the text box it will give me the wrong answer
example if i enter 1000
at the textbox show me 1,000.00
but the textbox that show me the sum of it just display 1
and give me an idea how to do ?
Last edited by ninjaX; Jul 6th, 2004 at 09:15 AM.
-
Jul 6th, 2004, 09:19 AM
#4
Hyperactive Member
well, my first guess would be that there is no text in your other textboxes so you are trying to perform calculations with values of nothing.
What you should do is have form level variables that hold adjustments to your calculations, then in each text box you should implement a handler that captures the changed value and assigns it to the module level variable and then performs the calculation on those variables.
Try this:
VB Code:
Private GIDollars As Decimal = 0
Private Deductions As Int16 = 0
Private Parents As Int16 = 0
Private Bonus As Decimal = 0
Private Personal As Decimal = 0
Private EPF As Decimal = 0
Private Sub gross_TextBox_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles gross_TextBox.Leave
GIDollars = Val(Me.gross_TextBox.Text)
Me.gross_TextBox.Text = Format(GIDollars, "#,###.00")
Me.taxable_TextBox.Text = Format(GIDollars + Bonus - Deductions - Personal - Parents - EPF, "#,###.00")
End Sub
Private Sub bonus_TextBox_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles bonus_TextBox.Leave
Bonus = Val(Me.bonus_TextBox.Text)
Me.bonus_TextBox.Text = Format(Bonus, "#,###.00")
Me.taxable_TextBox.Text = Format(GIDollars + Bonus - Deductions - Personal - Parents - EPF, "#,###.00")
End Sub
Whadayamean it doesn't work....
It works fine on my machine!

-
Jul 6th, 2004, 09:34 AM
#5
Thread Starter
Lively Member
sorry, i got a new problem after i change the coding below the gross textbox only can enter not more than 5 value
example 100.00 is the maximum number
when i add in one more 0 it change become 1.00
not 1,000.00 what is the problem is that ??
and i only can enter the 0 between the 1 and the .00 what make it happen ??
Private Sub gross_TextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gross_TextBox.TextChanged
TempGIDollars = Val(Me.gross_TextBox.Text)
TempBonus = Val(Me.bonus_TextBox.Text)
TempChildren = Val(Me.children_TextBox.Text)
TempPersonal = Val(Me.personal_TextBox.Text)
TempParents = Val(Me.parent_TextBox.Text)
TempEPF = Val(Me.epf_TextBox.Text)
Me.gross_TextBox.Text = Format(TempGIDollars, "#,###.00")
Me.taxable_TextBox.Text = Format(TempGIDollars + TempBonus - TempChildren - TempPersonal - TempParents - TempEPF, "#,###.00")
End Sub
-
Jul 6th, 2004, 09:36 AM
#6
Hyperactive Member
The TextChanged event fires with every keystroke, so everytime you press a key, this event fires. I would recommend against using this event for the purpose specified. Use the Leave event.
Whadayamean it doesn't work....
It works fine on my machine!

-
Jul 6th, 2004, 09:50 AM
#7
Thread Starter
Lively Member
-
Jul 6th, 2004, 09:54 AM
#8
Thread Starter
Lively Member
thanx the code is working
if i really wanna use the TextChanged format is there any solution ????
-
Jul 6th, 2004, 09:59 AM
#9
Hyperactive Member
Not without overriding the event's default behavior. The event is self describing and so it should be obvious that any time the text is changed (eg. pressing a key while the control has focus) the event will fire.
Whadayamean it doesn't work....
It works fine on my machine!

-
Jul 6th, 2004, 10:09 AM
#10
Thread Starter
Lively Member
ok ~
thanx for helping me
-
Jul 6th, 2004, 12:49 PM
#11
Hyperactive Member
I found some significant errors with the code I provided you earlier. I would suggest trying this in your project, it's much simpler and more easily extensible.
VB Code:
'Form level variables
Private WithEvents ctl As Control
Private refCtl As New TextBox
Private GIDollars As Double = 0
Private Deductions As Double = 0
Private Parents As Double = 0
Private Bonus As Double = 0
Private Personal As Double = 0
Private EPF As Double = 0
'Sets the initial reference for ctl to the forms active control
Private Sub frmComboExample_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated
Me.ctl = Me.ActiveControl
End Sub
'Single event handler for all controls on the form, can be extended...
Private Sub ctl_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles ctl.Leave
'If the length of the text in the text box is 0, an error occurs...
'Probably better if a try block were here...
If ctl.GetType.Equals(refCtl.GetType) And ctl.Text.Length < 1 Then Exit Sub
Select Case ctl.Name
Case Me.gross_TextBox.Name
GIDollars = Convert.ToDouble(Me.gross_TextBox.Text)
ctl.Text = Format(GIDollars, "#,###.00")
Case Me.bonus_TextBox.Name
Bonus = Convert.ToDouble(Me.bonus_TextBox.Text)
ctl.Text = Format(Bonus, "#,###.00")
End Select
Me.ctl = Me.ActiveControl
Me.taxable_TextBox.Text = Format(GIDollars + Bonus - Deductions - Personal - Parents - EPF, "#,###.00")
End Sub
Whadayamean it doesn't work....
It works fine on my machine!

-
Jul 7th, 2004, 03:40 AM
#12
Thread Starter
Lively Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|