Setting Default Properties For a Control

Ok i've spent two day trying to figure this out and it's driving me crazy. I'm trying to create a textbox control that inherits the system.windows.forms.textbox control.  My textbox control will be used for currency only but I am running into a problem.  I tried to override the text property because when the user drops my currency_textbox on a form, I do not want the default text of it to be the same as the control name.  I have looked everywhere and tried everything and cannot get it to work.  Please someone tell me what im doing wrong.

Imports System.ComponentModel
Public Class CurrencyTextbox
    Inherits System.Windows.Forms.TextBox
#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call
    End Sub

    'UserControl1 overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        '
        'CurrencyTextbox
        '
        Me.Name = "CurrencyTextbox"
    End Sub

#End Region
    Public Enum NDT
        Normal
        Red
        Parenthesis
        Red_Parenthesis
    End Enum
    Dim NegativeDisplay As NDT
    Dim SelAllOnFocus As Boolean
    Dim AN As Boolean
    Dim DT As String = "$0.00"


    <Description("Formatting Style For Negative Numbers"), _
         Category("Appearance"), DefaultValue(NDT.Normal)> _
        Property NegativeStyle() As NDT
        Get
            NegativeStyle = NegativeDisplay
        End Get
        Set(ByVal Value As NDT)
            NegativeDisplay = Value
            Invalidate()
        End Set
    End Property
    <Description("Highlight All Text When Control Receives Focus"), _
            Category("Behavior"), DefaultValue(False)> _
        Property SelectTextOnFocus() As Boolean
        Get
            SelectTextOnFocus = SelAllOnFocus
        End Get
        Set(ByVal Value As Boolean)
            SelAllOnFocus = Value
        End Set
    End Property


    <Description("Allow Negative Numbers To Be Entered. If set to False, Negative Numbers Will Be Converted To $0.00"), _
            Category("Behavior"), DefaultValue(True)> _
        Property AllowNegativeNumbers() As Boolean
        Get
            AllowNegativeNumbers = AN
        End Get
        Set(ByVal Value As Boolean)
            AN = Value
        End Set
    End Property

    Private Sub CurrencyTextbox_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.GotFocus
        If SelAllOnFocus Then
            If Len(Me.Text) > 0 Then
                Me.SelectionStart = 0
                Me.SelectionLength = Len(Me.Text)
            End If
        End If
    End Sub

    Private Sub CurrencyTextbox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
        If IsNumeric(e.KeyChar) Then
            'ok to add to textbox
        ElseIf e.KeyChar = "." Then
            'ok to add to textbox
        ElseIf e.KeyChar = "$" Then
            'ok to add to textbox
        ElseIf Asc(e.KeyChar) = 8 Then 'Backspace Key
            'ok to add to textbox
        ElseIf e.KeyChar = "-" Then
            'ok to add to textbox
        Else
            e.Handled = True
        End If
    End Sub

    Private Sub CurrencyTextbox_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.LostFocus
        Dim TT As String
        TT = Me.Text.Replace("$", "")
        If Strings.Left(TT, 1) = "." Then
            TT = Strings.Right(TT, Len(TT) - 1)
        End If
        If Strings.Right(TT, 1) = "." Then
            TT = Strings.Left(TT, Len(TT) - 1)
        End If
        If Len(TT) = 0 Then
            TT = "0"
        End If
        If AN = False Then
            If CDbl(TT) < 0 Then
                TT = "0"
            End If
        End If
        If IsNumeric(TT) = False Then
            TT = "0"
        End If
        Select Case NegativeDisplay
            Case NDT.Parenthesis
                Me.ForeColor = Color.Black
                Me.Text = CStr(FormatCurrency(TT, 2, , TriState.True))
            Case NDT.Red
                If CDbl(TT) < 0 Then
                    Me.ForeColor = Color.Red
                Else
                    Me.ForeColor = Color.Black
                End If
                Me.Text = CStr(FormatCurrency(TT, 2, , TriState.False))
            Case NDT.Red_Parenthesis
                If CDbl(TT) < 0 Then
                    Me.ForeColor = Color.Red
                Else
                    Me.ForeColor = Color.Black
                End If
                Me.Text = CStr(FormatCurrency(TT, 2, , TriState.True))
            Case NDT.Normal
                Me.ForeColor = Color.Black
                Me.Text = CStr(FormatCurrency(TT, 2, , TriState.False))
        End Select
    End Sub

    Public Sub ClearText()
        Me.Text = ""
    End Sub
    <Browsable(True)> _
    Public Overrides Property Text() As String
        Get
            Return DT
        End Get
        Set(ByVal Value As String)
            DT = Value
        End Set
    End Property
End Class
LVL 1
Epiphany1105Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

S-TwilleyCommented:
Im not sure if there is a better way of doing this... but I created a simple inherited textbox control...

In the "Sub New":
    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call
        MyBase.Text = "HELLO"
    End Sub


In The "Text" Property:

    Public Overrides Property Text() As String
        Get
            Return MyBase.Text
        End Get
        Set(ByVal Value As String)
            If IsValid(Value) Then
                MyBase.Text = Value
            Else
                MyBase.Text = "HELLO"
            End If
        End Set
    End Property

    Private Function IsValid(ByVal NewValue As String) As Boolean
        If NewValue = "HELLO" Then
            Return True
        Else
            Return False
        End If
    End Function

----------------

This will need changing a bit to work with your control... but it prevents the designer from assigning its own default property... you can use the IsValid to parse for a valid format (currency i guess), and when it fails, it puts in a default value...   then, to validate user input, you could use the validating event... this is a hashed up solution, but least gives you something to play around with till you find a better solution.

If it's a bit confusing, let me know and I'll try and work it into your class
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Epiphany1105Author Commented:
The default text shows up the same in the properties window but there is no text on the actual control.  Updated code.

Imports System.ComponentModel
Public Class CurrencyTextbox
    Inherits System.Windows.Forms.TextBox
#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call
        MyBase.Text = "$0.00"
    End Sub

    'UserControl1 overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        '
        'CurrencyTextbox
        '
        Me.Name = "CurrencyTextbox"
    End Sub

#End Region
    Public Enum NDT
        Normal
        Red
        Parenthesis
        Red_Parenthesis
    End Enum
    Dim NegativeDisplay As NDT
    Dim SelAllOnFocus As Boolean
    Dim AN As Boolean


    <Description("Formatting Style For Negative Numbers"), _
         Category("Appearance"), DefaultValue(NDT.Normal)> _
        Property NegativeStyle() As NDT
        Get
            NegativeStyle = NegativeDisplay
        End Get
        Set(ByVal Value As NDT)
            NegativeDisplay = Value
            Invalidate()
        End Set
    End Property
    <Description("Highlight All Text When Control Receives Focus"), _
            Category("Behavior"), DefaultValue(False)> _
        Property SelectTextOnFocus() As Boolean
        Get
            SelectTextOnFocus = SelAllOnFocus
        End Get
        Set(ByVal Value As Boolean)
            SelAllOnFocus = Value
        End Set
    End Property


    <Description("Allow Negative Numbers To Be Entered. If set to False, Negative Numbers Will Be Converted To $0.00"), _
            Category("Behavior"), DefaultValue(True)> _
        Property AllowNegativeNumbers() As Boolean
        Get
            AllowNegativeNumbers = AN
        End Get
        Set(ByVal Value As Boolean)
            AN = Value
        End Set
    End Property

    Private Sub CurrencyTextbox_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.GotFocus
        If SelAllOnFocus Then
            If Len(Me.Text) > 0 Then
                Me.SelectionStart = 0
                Me.SelectionLength = Len(Me.Text)
            End If
        End If
    End Sub

    Private Sub CurrencyTextbox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
        If IsNumeric(e.KeyChar) Then
            'ok to add to textbox
        ElseIf e.KeyChar = "." Then
            'ok to add to textbox
        ElseIf e.KeyChar = "$" Then
            'ok to add to textbox
        ElseIf Asc(e.KeyChar) = 8 Then 'Backspace Key
            'ok to add to textbox
        ElseIf e.KeyChar = "-" Then
            'ok to add to textbox
        Else
            e.Handled = True
        End If
    End Sub

    Private Sub CurrencyTextbox_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.LostFocus
        Dim tt As String = Me.Text
        If IsNumeric(tt) = False Then
            tt = "0"
        End If
        If AN = False Then
            If CDbl(tt) < 0 Then
                tt = "0"
            End If
        End If
        Select Case NegativeDisplay
            Case NDT.Parenthesis
                Me.ForeColor = Color.Black
                Me.Text = CStr(FormatCurrency(tt, 2, , TriState.True))
            Case NDT.Red
                If CDbl(tt) < 0 Then
                    Me.ForeColor = Color.Red
                Else
                    Me.ForeColor = Color.Black
                End If
                Me.Text = CStr(FormatCurrency(tt, 2, , TriState.False))
            Case NDT.Red_Parenthesis
                If CDbl(tt) < 0 Then
                    Me.ForeColor = Color.Red
                Else
                    Me.ForeColor = Color.Black
                End If
                Me.Text = CStr(FormatCurrency(tt, 2, , TriState.True))
            Case NDT.Normal
                Me.ForeColor = Color.Black
                Me.Text = CStr(FormatCurrency(tt, 2, , TriState.False))
        End Select
    End Sub

    Public Sub ClearText()
        Me.Text = ""
    End Sub
    Public Overrides Property Text() As String
        Get
            Return MyBase.Text
        End Get
        Set(ByVal Value As String)
            If IsValid(Value) Then
                MyBase.Text = Value
            Else
                MyBase.Text = "$0.00"
            End If
        End Set
    End Property

    Private Function IsValid(ByVal NewValue As String) As Boolean
        If IsNumeric(NewValue) Then
            IsValid = True
        Else
            IsValid = False
        End If
    End Function
End Class

0
S-TwilleyCommented:
I just copy pasted that code into my project and it displayed it in the control and in the properties... admitedly I changed the name of the control to match the one in my toolbox, but that shouldn't make any difference.
0
Epiphany1105Author Commented:
Strange.  I put the code into a new project and it worked!  I'm not sure what the problem was with the original project but it doesn't matter because it works now :)   Thanks for the help S-Twilley.
0
S-TwilleyCommented:
I've had problems with custom controls and the toolbox not updating when i just do a copy paste... and have had similar problems with the project not updating... seems to be some sort of flaw/bug in VS
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.