Persisting properties
-
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
-
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
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.
-
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.
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....
-
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....
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
-
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
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