none
Property not saving default value in custom control RRS feed

  • Question

  • Hi,

    I have a custom control, it is a simple panel that hosts a textbox inside, so when I add the control in a winform it has the "Text" property in the properties toolbox, if I add a default value it appears in the control, so far so good, but in runtime the control is empty:


     <Browsable(True), Bindable(True), DefaultValue(True)>
        Public Overrides Property Text() As String
            Get
                Return txt.Text
            End Get
            Set(ByVal value As String)
                txt.Text = value
            End Set
        End Property

    What am I doing wrong?

    Thanks !!!


    G.Waters


    Saturday, April 7, 2018 7:53 AM

Answers

  • May This Help You

    Try by DesignerSerializationVisibility

    <Browsable(True), EditorBrowsable(EditorBrowsableState.Always), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible), Bindable(True), DefaultValue(True)>
        Public Overrides Property Text() As String
            Get
                Return txt.Text
            End Get
            Set(ByVal value As String)
                txt.Text = value
            End Set
        End Property


    • Marked as answer by George Waters Saturday, April 7, 2018 9:10 AM
    Saturday, April 7, 2018 8:31 AM

All replies

  • May This Help You

    Try by DesignerSerializationVisibility

    <Browsable(True), EditorBrowsable(EditorBrowsableState.Always), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible), Bindable(True), DefaultValue(True)>
        Public Overrides Property Text() As String
            Get
                Return txt.Text
            End Get
            Set(ByVal value As String)
                txt.Text = value
            End Set
        End Property


    • Marked as answer by George Waters Saturday, April 7, 2018 9:10 AM
    Saturday, April 7, 2018 8:31 AM
  • if I add a default value it appears in the control, so far so good, but in runtime the control is empty:

    If the default is required at runtime only you can use the custom control constructor.

    Imports System
    Imports System.ComponentModel
    
    Public Class UserControl1
    
        Public Sub New()
            ' This call is required by the designer.
            InitializeComponent()
    
            Me.Text = txt.Text
    
        End Sub
    
        <Browsable(True), Bindable(True), DefaultValue(True)>
        Public Overrides Property Text() As String
            Get
                Return txt.Text
            End Get
            Set(ByVal value As String)
                txt.Text = value
            End Set
        End Property
    
    End Class

    Saturday, April 7, 2018 8:35 AM
  • Thanks Acamar, I tried your suggestion but it doesn't work :(

    G.Waters

    Saturday, April 7, 2018 9:08 AM
  • Thanks Vikram, that did the job !!!

    G.Waters

    Saturday, April 7, 2018 9:11 AM
  •  You should also be aware that the DefaultValue Attribute is to set the default value of the Property.  Your Property is a String type and you are setting the default value to a Boolean type value of True.  Either the DefaultValue attribute should be removed from the Property,  or it should be set to the String value for the default.  The default should be of the same type as the property is.

    For example,  if I have a String type Property and want it's DefaultValue set to "Hello",  it would be like this...

        <DefaultValue("Hello")>
        Public Property MyStringProperty As String
    

     

     If I had a boolean type property,  then I can set the default value to a Boolean type...

        <DefaultValue(False)>
        Public Property MyBooleanProperty As Boolean
    


    If you say it can`t be done then i`ll try it

    Saturday, April 7, 2018 11:15 AM