When inheriting from TextBox, how do you replace the default property (Text)?

KaseyM
KaseyM used Ask the Experts™
on

In a class which inherits System.Windows.Forms.TextBox, I'm attempting to replace the default property so that, in the property sheet in design view, I can open Databindings|Advanced, for example, and my property will be the default selected at the top. I assume the little "cylinder" icon will also be displayed next to it, as it is by default on the "Text" property.

In my attempt, I have declared my property as default. A default parameter is required. I have added one but am unable to get any further, as I do not know how to satisfy the argument.

I believe I may also need to override the Text property so that it is not also declared as default - but I'm not sure.

Imports System.ComponentModel
Imports System.Windows.Forms

<ToolboxBitmap(GetType(TextBox))>
Public Class NullableTextBox
    Inherits System.Windows.Forms.TextBox
    Implements INotifyPropertyChanged

    Public Event PropertyChanged(ByVal sender As Object, ByVal e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged

    Public Sub New()
        MyBase.New()
        Me.DataBindings.DefaultDataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged
    End Sub

    <Browsable(True), Bindable(True), ListBindable(True), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)>
    Default Public Property NullableText(ByVal defaultValue As String) As Object
        Get
            If Text.Trim.Length = 0 Then
                Return DBNull.Value
            End If
            Return Me.Text
        End Get
        Set(ByVal value As Object)
            If value Is DBNull.Value Then
                value = String.Empty
            Else
                Me.Text = value.ToString
            End If
        End Set
    End Property

    Protected Sub OnPropertyChanged(ByVal PropertyName As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(PropertyName))
    End Sub

    Private Sub NullableTextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.TextChanged
        OnPropertyChanged("NullableText")
    End Sub
End Class

Open in new window


This class compiles, but when I try to use the component on a form, the designer adds it to the designer.vb file and complains of course that the argument was not supplied.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Class MyTextBox
     Inherits TextBox
      Public Shadows Property Text As String
         Get
             '   Get Code
         End Get
         Set(ByVal value As String)
             '   Set code
          End Set
     End Property
End Class

Author

Commented:
That gets me part of the way there. I'd tried that but in the setter, you can't set the .Text property because it would be called recursively. However, taking your cue, in the setter I'm now simply setting the value of my new default property. Seems to work ok. In the Databindings|Advanced dialog, my property is now, apparently, the default. The cylinder icon is attached. Your points. Thanks very much.

    <Browsable(True), Bindable(True), ListBindable(True), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)>
    Public Property NullableText() As Object
        Get
            If Text.Trim.Length = 0 Then
                Return DBNull.Value
            End If
            Return Me.Text
        End Get
        Set(ByVal value As Object)
            If value Is DBNull.Value Then
                value = String.Empty
            Else
                Me.Text = value.ToString
            End If
        End Set
    End Property

    <Browsable(True), Bindable(True), ListBindable(True), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)>
    Public Shadows Property Text() As String
        Get
            Return MyBase.Text
        End Get
        Set(ByVal value As String)
            If value Is DBNull.Value Then
                value = String.Empty
            Else
                Me.NullableText = value
            End If
        End Set
    End Property

Open in new window

Your Set method seems not storing anything if value is DBNull, you only set value to String.Empty then quit, in fact you can set and get like this:
Get
            If MyBase.Text.Trim.Length = 0 Then
                Return DBNull.Value
            Else
                Return MyBase.Text
            End If
End Get
Set (ByVal value as String)
   If value is DBNull.Value Then
       MyBase.Text = ""
   Else
       MyBase.Text = value
   End If
End Set

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial