Results 1 to 12 of 12

Thread: Need Help

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2004
    Posts
    108

    Unhappy 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
    ===========================================

  2. #2
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    I think the problem is, you are providing optional characters for the end of your format string, try this:
    VB Code:
    1. Private Sub TextBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.LostFocus
    2.         Dim GIDollars As Double = Val(Me.TextBox1.Text)
    3.  
    4.         Me.TextBox1.Text = Format(GIDollars, "#,###.00")
    5.     End Sub
    Whadayamean it doesn't work....
    It works fine on my machine!

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2004
    Posts
    108
    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.

  4. #4
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    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:
    1. Private GIDollars As Decimal = 0
    2.     Private Deductions As Int16 = 0
    3.     Private Parents As Int16 = 0
    4.     Private Bonus As Decimal = 0
    5.     Private Personal As Decimal = 0
    6.     Private EPF As Decimal = 0
    7.  
    8.  
    9.     Private Sub gross_TextBox_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles gross_TextBox.Leave
    10.         GIDollars = Val(Me.gross_TextBox.Text)
    11.         Me.gross_TextBox.Text = Format(GIDollars, "#,###.00")
    12.         Me.taxable_TextBox.Text = Format(GIDollars + Bonus - Deductions - Personal - Parents - EPF, "#,###.00")
    13.     End Sub
    14.  
    15.     Private Sub bonus_TextBox_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles bonus_TextBox.Leave
    16.         Bonus = Val(Me.bonus_TextBox.Text)
    17.         Me.bonus_TextBox.Text = Format(Bonus, "#,###.00")
    18.         Me.taxable_TextBox.Text = Format(GIDollars + Bonus - Deductions - Personal - Parents - EPF, "#,###.00")
    19.     End Sub
    Whadayamean it doesn't work....
    It works fine on my machine!

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2004
    Posts
    108
    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

  6. #6
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    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!

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Mar 2004
    Posts
    108
    thanx i will try first

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Mar 2004
    Posts
    108
    thanx the code is working

    if i really wanna use the TextChanged format is there any solution ????

  9. #9
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    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!

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Mar 2004
    Posts
    108
    ok ~
    thanx for helping me


  11. #11
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    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:
    1. 'Form level variables
    2.     Private WithEvents ctl As Control
    3.     Private refCtl As New TextBox
    4.     Private GIDollars As Double = 0
    5.     Private Deductions As Double = 0
    6.     Private Parents As Double = 0
    7.     Private Bonus As Double = 0
    8.     Private Personal As Double = 0
    9.     Private EPF As Double = 0
    10.  
    11.     'Sets the initial reference for ctl to the forms active control
    12.     Private Sub frmComboExample_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated
    13.         Me.ctl = Me.ActiveControl
    14.     End Sub
    15.  
    16.     'Single event handler for all controls on the form, can be extended...
    17.     Private Sub ctl_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles ctl.Leave
    18.         'If the length of the text in the text box is 0, an error occurs...
    19.         'Probably better if a try block were here...
    20.         If ctl.GetType.Equals(refCtl.GetType) And ctl.Text.Length < 1 Then Exit Sub
    21.         Select Case ctl.Name
    22.             Case Me.gross_TextBox.Name
    23.                 GIDollars = Convert.ToDouble(Me.gross_TextBox.Text)
    24.                 ctl.Text = Format(GIDollars, "#,###.00")
    25.             Case Me.bonus_TextBox.Name
    26.                 Bonus = Convert.ToDouble(Me.bonus_TextBox.Text)
    27.                 ctl.Text = Format(Bonus, "#,###.00")
    28.         End Select
    29.         Me.ctl = Me.ActiveControl
    30.         Me.taxable_TextBox.Text = Format(GIDollars + Bonus - Deductions - Personal - Parents - EPF, "#,###.00")
    31.     End Sub
    Whadayamean it doesn't work....
    It works fine on my machine!

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Mar 2004
    Posts
    108
    ok ~
    thanx for the helping
    this will make me learn more

    thanx for your help ~


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width