Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Persisting properties

Persisting properties

Scheduled Pinned Locked Moved C#
helptutorial
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    chettu
    wrote on last edited by
    #1

    Hi I extended a textbox control and created a property i.e public enum myVarEnum {Apple,Mango,Peach}; protected myVarEnum myvar1; public myVar MyProperty { get { return myvar1; } set { myvar1 = value; } } Now when i use this extended control i am getting the property as a dropdown and i am able to select, the problem is the value i select doesnot persist..for example if i select "Mango" it goes back to the first one "Apple" after i build the solution. i guess its a small mistake plz help me overcome this... Regards Deepak.S

    A 2 Replies Last reply
    0
    • C chettu

      Hi I extended a textbox control and created a property i.e public enum myVarEnum {Apple,Mango,Peach}; protected myVarEnum myvar1; public myVar MyProperty { get { return myvar1; } set { myvar1 = value; } } Now when i use this extended control i am getting the property as a dropdown and i am able to select, the problem is the value i select doesnot persist..for example if i select "Mango" it goes back to the first one "Apple" after i build the solution. i guess its a small mistake plz help me overcome this... Regards Deepak.S

      A Offline
      A Offline
      Ashok Dhamija
      wrote on last edited by
      #2

      Apparently, the problem may be due to the fact that Visual C# does not allow you to create parameterized properties except for the indexer. The indexer is a specialized property that allows one to expose a group of values or objects on the name of the object. The name used in the indexer is required to be "this", i.e., the name of the object itself would be required to be used to access the property. An example would be like this:

      //declare an array for values to be exposed by Indexer
      private int[] myArray;
      //Now use the index variable to retrieve the correct from the array by defining the property as under
      public int this[int index]
      {
      get
      {
      return myArray[index];
      }
      set
      {
      myArray[index] = value;
      }
      }

      Try the above method and see whether it works.

      C 1 Reply Last reply
      0
      • A Ashok Dhamija

        Apparently, the problem may be due to the fact that Visual C# does not allow you to create parameterized properties except for the indexer. The indexer is a specialized property that allows one to expose a group of values or objects on the name of the object. The name used in the indexer is required to be "this", i.e., the name of the object itself would be required to be used to access the property. An example would be like this:

        //declare an array for values to be exposed by Indexer
        private int[] myArray;
        //Now use the index variable to retrieve the correct from the array by defining the property as under
        public int this[int index]
        {
        get
        {
        return myArray[index];
        }
        set
        {
        myArray[index] = value;
        }
        }

        Try the above method and see whether it works.

        C Offline
        C Offline
        chettu
        wrote on last edited by
        #3

        Indexers allows to expose a group of values but how can i show those values in the designer so that the user can select one. I have to set other properties based on the selection made by the user. i guess indexers are availabe only in code on a object of that class. Any other suggestions....

        S 1 Reply Last reply
        0
        • C chettu

          Indexers allows to expose a group of values but how can i show those values in the designer so that the user can select one. I have to set other properties based on the selection made by the user. i guess indexers are availabe only in code on a object of that class. Any other suggestions....

          S Offline
          S Offline
          spif2001
          wrote on last edited by
          #4

          This works for objects: [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] public myVar MyProperty { get { return myvar1; } set { myvar1 = value; } } don't know about enums though - maybe this will work [DefaultValue(false)] public myVar MyProperty { get { return myvar1; } set { myvar1 = value; } } -spif2001

          1 Reply Last reply
          0
          • C chettu

            Hi I extended a textbox control and created a property i.e public enum myVarEnum {Apple,Mango,Peach}; protected myVarEnum myvar1; public myVar MyProperty { get { return myvar1; } set { myvar1 = value; } } Now when i use this extended control i am getting the property as a dropdown and i am able to select, the problem is the value i select doesnot persist..for example if i select "Mango" it goes back to the first one "Apple" after i build the solution. i guess its a small mistake plz help me overcome this... Regards Deepak.S

            A Offline
            A Offline
            Ashok Dhamija
            wrote on last edited by
            #5

            In fact, in the limited feature desired by you, I tried the method being used by you, and good news for you, it works! The only change I made in your code (as shown above) is that I have used "public myVarEnum MyProperty" instead of "public myVar MyProperty" as used in your code (which appears to be a typing mistake), while defining the property. In the sample small project prepared by me, the values "Mango" and "Peach" also persist if set at design time from the Property window from the dropdown. If you wish, I can separately send you the sample project files. Otherwise, I am including the source code of the MyButton.cs file (the inherited control) and the Form1.cs hereinbelow for your information:

            //MyTextBox.cs file
            using System;

            namespace TestControl2
            {
            /// /// Summary description for MyTextBox.
            ///
            public class MyTextBox : System.Windows.Forms.TextBox
            {
            public enum myVarEnum {Apple,Mango,Peach};
            protected myVarEnum myvar1;

            	public myVarEnum MyProperty
            	{
            		get { return myvar1; }
            		set { myvar1 = value; }
            	}
            	public MyTextBox()
            	{
            		//
            		// TODO: Add constructor logic here
            		//
            	}
            }
            

            }

            //Form1.cs file
            using System;
            using System.Drawing;
            using System.Collections;
            using System.ComponentModel;
            using System.Windows.Forms;
            using System.Data;

            namespace TestControl2
            {
            /// /// Summary description for Form1.
            ///
            public class Form1 : System.Windows.Forms.Form
            {
            private TestControl2.MyTextBox myTextBox1;
            private System.Windows.Forms.Button button1;
            private System.Windows.Forms.Label label1;
            /// /// Required designer variable.
            ///
            private System.ComponentModel.Container components = null;

            	public Form1()
            	{
            		//
            		// Required for Windows Form Designer support
            		//
            		InitializeComponent();
            
            		//
            		// TODO: Add any constructor code after InitializeComponent call
            		//
            	}
            
            	/// /// Clean up any resources being used.
            	/// 
            	protected override void Dispose( bool disposing )
            	{
            		if( disposing )
            		{
            			if (components != null) 
            			{
            				components.Dispose();
            			}
            		}
            		base.Dispose( disposing );
            	}
            
            	#region Windows Form Designer generated code
            	/// /// Required method for Designer support - do not modify
            	/// the contents of this method with the code editor.
            	/// 
            	private void InitializeComponent()
            	{
            		this.myTextBox1 = new TestControl2.MyText
            
            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups