Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB8.0
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
        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 21-Mar-13 17:05pm
Edited 21-Mar-13 17:09pm
v3

1 solution

Rate this: bad
good
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.
 
—SA
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 539
1 Maciej Los 300
2 DamithSL 233
3 Sergey Alexandrovich Kryukov 209
4 BillWoodruff 200
0 OriginalGriff 7,168
1 Sergey Alexandrovich Kryukov 6,377
2 DamithSL 5,461
3 Manas Bhardwaj 4,876
4 Maciej Los 4,450


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 21 Mar 2013
Copyright © CodeProject, 1999-2014
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