创建数字文本框(.Net 4.0-Winforms)时出现问题. [英] Problem in creating numeric textbox (.Net 4.0 - Winforms).

查看:161
本文介绍了创建数字文本框(.Net 4.0-Winforms)时出现问题.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在尝试创建自己的数字文本框,该文本框将处理小数以及整数值.该概念与常规文本框不同,该文本框将接受``Double''数据类型的值并以正确的格式显示它,例如前导数字,数字分组等.调用文本框值时,它将返回实际的"Double"数据类型值.整个过程都是通过"Text"属性完成的;其中常规的文本"属性被阴影化,并且使用一种具有双精度"数据类型的属性.
问题是,在设计器属性窗口中,Text属性始终保持只读状态,并且其值显示对象引用未设置为对象的实例".我怎么了?
我的代码如下:

Hi,
I''m trying to create my own numeric textbox that will handle decimal as well as whole number values. The concept is, unlike the regular textbox, this one will accept a value of ''Double'' data type and show it with proper formatting e.g. Leading Digits, Digit Grouping etc. When the textbox value is called, it returns the actual ''Double'' data type value. The entire thing is done through the ''Text'' property; wherein the regular ''Text'' property is shadowed and one with ''Double'' data type is used.
The problem is, in the designer properties window, the Text property always remains readonly and its value says ''Object reference not set to an instance of an object''. What''s my mistake?
My code follows:

Imports System.Windows.Forms
Imports System.ComponentModel
Imports System.Runtime.InteropServices

Public Class NumericTextBox
    Inherits TextBox
    Private _IsDecimal As Boolean

    <System.Diagnostics.DebuggerNonUserCode()> _
    Public Sub New()
        MyBase.New()

        'This call is required by the Component Designer.
        InitializeComponent()
        Me.TextAlign = HorizontalAlignment.Right
    End Sub

    <DefaultValue(False)>
    <ComVisible(True)> _
    Public Property IsDecimal As Boolean
        Get
            Return _IsDecimal
        End Get
        Set(ByVal value As Boolean)
            _IsDecimal = value
        End Set
    End Property

    <DefaultValue(0)>
    <Description("The text associated with the control.")> _
    Public Shadows Property Text As Double
        Get
            Dim ReturnValue As Double
            Double.TryParse(MyBase.Text, ReturnValue)
            Return ReturnValue
        End Get
        Set(ByVal value As Double)
            If Not IsDecimal Then
                MyBase.Text = FormatNumber(value, 0, TriState.True, TriState.False, TriState.True)
            Else
                MyBase.Text = FormatNumber(value, 2, TriState.True, TriState.False, TriState.True)
            End If
        End Set
    End Property

    <DefaultValue(GetType(HorizontalAlignment), "Right")> _
    Public Shadows Property TextAlign As HorizontalAlignment
        Get
            Return MyBase.TextAlign
        End Get
        Set(ByVal value As HorizontalAlignment)
            MyBase.TextAlign = value
        End Set
    End Property

    Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
        MyBase.OnKeyPress(e)

        If Not IsDecimal Then
            If Not Asc(e.KeyChar) = 8 And "0123456789".IndexOf(e.KeyChar) = -1 Then
                e.Handled = True
                Exit Sub
            End If
        Else
            If Not Asc(e.KeyChar) = 8 And "0123456789.".IndexOf(e.KeyChar) = -1 Then
                e.Handled = True
                Exit Sub
            End If
        End If
    End Sub

    Protected Overrides Sub OnValidating(ByVal e As System.ComponentModel.CancelEventArgs)
        MyBase.OnValidating(e)

        If Trim(MyBase.Text) <> "" And Not IsNumeric(MyBase.Text) Then
            MsgBox("Enter a valid numeric value.", MsgBoxStyle.Exclamation, "Invalid Data")
            e.Cancel = True
        Else
            If Not IsDecimal Then
                MyBase.Text = FormatNumber(Text, 0, TriState.True, TriState.False, TriState.True)
            Else
                MyBase.Text = FormatNumber(Text, 2, TriState.True, TriState.False, TriState.True)
            End If
        End If
    End Sub
End Class

推荐答案

尝试以下
Shadows Text属性隐藏基类Text属性.因此,我认为Mybase.text的使用不成功.因此,使方法重写并将文本用作字符串.有另一个属性来报告双倍价值.在设计器中隐藏文本值并报告双精度值.报告成功与否.
Try the following
Shadows Text property hides the base class Text property. Hence usage of Mybase.text in my opinion is not successful. So, make the method overrides and use text as string. Have another property to report double value. In the designer hide the text value and report the double. Report the success or otherwise of it.


这篇关于创建数字文本框(.Net 4.0-Winforms)时出现问题.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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