Click here to Skip to main content
12,998,759 members (70,500 online)
Rate this:
Please Sign up or sign in to 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
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
            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 21-Mar-13 16:05pm
Updated 21-Mar-13 16:09pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.


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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170622.1 | Last Updated 21 Mar 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100