?
Solved

Setting Default Properties For a Control

Posted on 2005-04-04
5
Medium Priority
?
196 Views
Last Modified: 2010-04-23
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
0
Comment
Question by:Epiphany1105
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 12

Accepted Solution

by:
S-Twilley earned 1000 total points
ID: 13699446
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
 
LVL 1

Author Comment

by:Epiphany1105
ID: 13699582
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
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13699701
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
 
LVL 1

Author Comment

by:Epiphany1105
ID: 13700174
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
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13700282
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question