Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Setting Default Properties For a Control

Posted on 2005-04-04
5
Medium Priority
?
197 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
  • 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month15 days, 14 hours left to enroll

580 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