没有从我的UserControl的Value获得 [英] Not getting from Value from my UserControl

查看:55
本文介绍了没有从我的UserControl的Value获得的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个用户控件,只接受数字数字。

我在表单上使用此控件计算总共2个数字

我在两个控件中输入了一些值。数字显示(前10,20)

但我没有得到总数。为什么&怎么做。



i在我的usercontrol中写了以下代码

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

但是它的lostfocus中没有任何计算

but nothing is calculating in its lostfocus

推荐答案

你有一些正确的想法,但是很重要把事搞砸了。您可能打算引入属性 txtNtext ,但您没有公开或内部,并且没有使用。相反,您使用 txtN1.Text 。如果 txtN1 属于 Number2 类型,则不清楚该属性的位置文字,你没有表现出来。一些事件 LostFocus 应该在成员 Number2.txtN 上处理,并且它的处理用于执行某些 Number2 的公共/内部事件,然后您可以在表单中使用它。



怎么办?首先要遵守良好的Microsoft命名约定。至少,不要在名称(1,2)中使用任何数字,使代码可读。然后放松并设计一切,不要急于求成。看起来你几乎知道一切;你只需要按正确的顺序放置东西。



-SA
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


这篇关于没有从我的UserControl的Value获得的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆