Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i created a user control to accept only Numerical Digits.
I used this control on my form to calculate total of 2 numbers
i entered some values in both controls. digits are showing (ex 10, 20)
but iam not getting the total. why & what to do.

i had written the following code in my usercontrol
VB
Public Class Number2
    Public Event TxtN_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Private Sub txtN_KeyPress1(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtN.KeyPress
        If e.KeyChar <> "." And (e.KeyChar < "0" Or e.KeyChar > "9") Then
            e.Handled = True
        End If
    End Sub
    Private Sub txtN_LostFocus1(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtN.LostFocus
        txtN.Text = FormatNumber(Val(txtN.Text), 2, , , False)
    End Sub
    Private Sub txtN_TextChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtN.TextChanged
        RaiseEvent TxtN_TextChanged(sender, e)
    End Sub
    Property txtNtext() As String
        Get
            txtNtext = txtN.Text
        End Get
        Set(ByVal value As String)
            txtN.Text = value
        End Set
    End Property

End Class
' and in form i am using it as
    Private Sub txtN1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtN1.LostFocus
        txtN3.Text = FormatNumber(Val(txtN1.Text) + Val(txtN2.Text), 3, , , TriState.False)
    End Sub
but nothing is calculating in its lostfocus
Posted
Updated 21-Mar-13 16:09pm
v3

1 solution

You got some right ideas, but heavily messed up things. You probably intended to introduce the property txtNtext, but you did not make it public or internal, and did not use. Instead, you use txtN1.Text. If txtN1 is of the type Number2, it's not clear where it the property Text, you don't show it. The event LostFocus should be handled on the member Number2.txtN, and it's handling used in implementation of some public/internal event of Number2, then you could use it in the form.

What do to? Start with observing good Microsoft naming conventions. At least, don't use any numerals in name (1, 2), make the code readable. Then relax and design it all without rush. It looks like you know almost everything; you only need to put thing in proper order.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900