Epiphany1105
asked on
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.textb ox 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.TextB ox
#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.ICon tainer
'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.Debugg erStepThro ugh()> 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(B yVal 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(B yVal sender As Object, ByVal e As System.Windows.Forms.KeyPr essEventAr gs) 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
Imports System.ComponentModel
Public Class CurrencyTextbox
Inherits System.Windows.Forms.TextB
#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.ICon
'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.Debugg
'
'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(B
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(B
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(
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
ASKER
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.
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
ASKER
Imports System.ComponentModel
Public Class CurrencyTextbox
Inherits System.Windows.Forms.TextB
#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.ICon
'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.Debugg
'
'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(B
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(B
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(
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