none
How To Set A Default Property Value For A User Control

    Question

  • [Note: Please see my reply below for more information.  Thanks]

    I have a String property named CriteriaLabel1 in a user control and I set a default value for it in the constructor.  Then I expose a property to let the form which uses the control change the label text.  But when I place the control on a form the value of that property is set to empty.  Why doesn't the value stay in that property?  I don't want to manually enter that text in every instance of the user control that I place on the form.  I want the default property to appear in the Properties window of that control automatically.  There must be a way.  Can you tell me how?

     // User Control class
    
     public partial class FilterCriteria : UserControl
    
     {
    
      private String msCriteriaLabel1;
    
    
    
      public FilterCriteria()
    
      {
    
       InitializeComponent();
    
       msCriteriaLabel1 = "Enter multiple values separated by a comma";
    
      }
    
      public String CriteriaLabel1
    
      {
    
       get { return msCriteriaLabel1; }
    
       set { msCriteriaLabel1 = value; }
    
      }
    
     }
    
    
    
    
    
    

    MCSD .NET developer in Dallas, Texas
    • Edited by DallasSteve Friday, February 18, 2011 3:43 PM
    • Moved by Leo Liu - MSFT Tuesday, February 22, 2011 5:45 AM Off-topic, moved for better support. (From:Visual C# General)
    Friday, February 18, 2011 3:40 PM

Answers

All replies

  • Wait a minute.  I did something wrong.  The existing user controls all have empty for this property value.  However, I just placed a new copy of the user control on the form and it has the default property value set like I want.  Why don't the others have that default value?  Did I set the default value in the constructor after I had already placed those controls on the form and they don't get reset when I change the constructor?

     


    MCSD .NET developer in Dallas, Texas
    Friday, February 18, 2011 3:43 PM
  • Attributes in Windows Forms Controls 

    Most controls use more than one attribute to achieve a desired behavior in the Properties Window at Design Time.  Questions about topics such as these are bested on the forum at this link.

    http://social.msdn.microsoft.com/Forums/en/winformsdesigner/threads

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://rudedog2.spaces.live.com/default.aspx

    • Proposed as answer by Aspen VJ Tuesday, February 22, 2011 8:26 AM
    • Marked as answer by Aspen VJ Tuesday, March 01, 2011 6:20 AM
    Friday, February 18, 2011 4:43 PM