I want to create a property off a WinForm custom control that I can browse to an object in my solution and set a string. I am thinking that there is already a browser editor control but I could not find any example on Google or Bing. Basically I am creating an enum drop down that extends the normal drop down. I want to pick the enum type at design time.
Example (see the question marks):
[Browsable(true),
Category("Custom"),
Description("Specifies the object Type"),
Editor(typeof(?), typeof(?))]
public string ObjectType
{
}
|
| Greg Finzer Thursday, September 17, 2009 12:48 PM |
Is this what you are trying to do? class DateOfWeekBox : EnumBox<DayOfWeek> { } That is the concrete class that the Form Designer will use. The following is the base class that the Form Designer will not use. public abstract class EnumBox<T> : UserControl { private T values; [Browsable(true)] [Category("Custom")] [Description("Specifies the object Type")] [RefreshProperties(RefreshProperties.All)] public T Values { get { return values; } set { values = value; this.comboBox1.SelectedItem = value; } } public EnumBox() { InitializeComponent(); InitializeDropDownList(); } private void InitializeDropDownList() { foreach (T val in Enum.GetValues(typeof(T))) { this.comboBox1.Items.Add(val); } } #region Form Designer code /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Component Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.comboBox1 = new System.Windows.Forms.ComboBox(); this.SuspendLayout(); // // comboBox1 // this.comboBox1.FormattingEnabled = true; this.comboBox1.Location = new System.Drawing.Point(0, 0); this.comboBox1.Name = "comboBox1"; this.comboBox1.Size = new System.Drawing.Size(121, 21); this.comboBox1.TabIndex = 0; // // EnumBox // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.Controls.Add(this.comboBox1); this.Name = "EnumBox"; this.Size = new System.Drawing.Size(121, 21); this.ResumeLayout(false); } #endregion private System.Windows.Forms.ComboBox comboBox1; #endregion } The control takes any enum that you declare as the type parameter and displays it in the drop down list of a ComboBox. There is no error checking. Rudedog =8^D
Mark the best replies as answers. "Fooling computers since 1971."- Marked As Answer byGreg Finzer Thursday, September 17, 2009 8:36 PM
-
|
| Rudedog2 Thursday, September 17, 2009 6:03 PM |
I don't understand what it is that you mean. "I am creating an enum drop down that extends the normal drop down." I do not think what you seek is possible. Basically, what you are looking to do is to create a generic control. The Form Designer needs the type information before it can instantiate the object on the form. [Browsable(true)] [Category("Custom")] [Description("Specifies the object Type")] [RefreshProperties(RefreshProperties.All)] The custom editor attribute is used to specify an editor to be used to change or modify the property, which means the type must already be known to the Form Designer. You can create a class that defines a generic control, and then inherit this control in a class that specifies type info. You would then have a control that should be usable with the Form Designer. class GenericControl<T> { } class MyControl : GenericControl<Int32> { }
Mark the best replies as answers. "Fooling computers since 1971." |
| Rudedog2 Thursday, September 17, 2009 2:10 PM |
If I do the generics route, how would I specify that it at least needs to be some kind of enum? |
| Greg Finzer Thursday, September 17, 2009 3:24 PM |
Decorate the enum property declaration with those attributes I posted. Mark the best replies as answers. "Fooling computers since 1971." |
| Rudedog2 Thursday, September 17, 2009 3:46 PM |
You can't use generics in Windows Forms controls, the designer doesn't support it. I really don't get what you are trying to accomplish, a Solution doesn't have objects, it has projects.
Hans Passant. |
| nobugz Thursday, September 17, 2009 4:09 PM |
You can't use generics in Windows Forms controls, the designer doesn't support it. I really don't get what you are trying to accomplish, a Solution doesn't have objects, it has projects.
Hans Passant.
It does if you define a class that inherits from one, and provide type information. The Designer seems happy with that.
Mark the best replies as answers. "Fooling computers since 1971." |
| Rudedog2 Thursday, September 17, 2009 4:19 PM |
So would it be something like this:
class GenericControl<T> where T : enum
{
}
|
| Greg Finzer Thursday, September 17, 2009 4:58 PM |
That's a double whammy, generics doesn't support enums as a constraint. Still don't know what you are trying to do but I bet you need a TypeConverter.
Hans Passant. |
| nobugz Thursday, September 17, 2009 5:14 PM |
No, like this... class GenericControl<T> : UserControl { } // this class is not compatible with Designer. class MyControl : GenericControl<Int32> { } // this class will work with Designer. And Hans makes an excellent point, you cannot use enum as a constraint. There is no way to restrict to just enums. The closest you're gonna get is not good, struct. class GenericControl<T> where T : struct { } class SomeControl : GenericControl<DayOfWeek> { }
Mark the best replies as answers. "Fooling computers since 1971." |
| Rudedog2 Thursday, September 17, 2009 5:45 PM |
Is this what you are trying to do? class DateOfWeekBox : EnumBox<DayOfWeek> { } That is the concrete class that the Form Designer will use. The following is the base class that the Form Designer will not use. public abstract class EnumBox<T> : UserControl { private T values; [Browsable(true)] [Category("Custom")] [Description("Specifies the object Type")] [RefreshProperties(RefreshProperties.All)] public T Values { get { return values; } set { values = value; this.comboBox1.SelectedItem = value; } } public EnumBox() { InitializeComponent(); InitializeDropDownList(); } private void InitializeDropDownList() { foreach (T val in Enum.GetValues(typeof(T))) { this.comboBox1.Items.Add(val); } } #region Form Designer code /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Component Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.comboBox1 = new System.Windows.Forms.ComboBox(); this.SuspendLayout(); // // comboBox1 // this.comboBox1.FormattingEnabled = true; this.comboBox1.Location = new System.Drawing.Point(0, 0); this.comboBox1.Name = "comboBox1"; this.comboBox1.Size = new System.Drawing.Size(121, 21); this.comboBox1.TabIndex = 0; // // EnumBox // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.Controls.Add(this.comboBox1); this.Name = "EnumBox"; this.Size = new System.Drawing.Size(121, 21); this.ResumeLayout(false); } #endregion private System.Windows.Forms.ComboBox comboBox1; #endregion } The control takes any enum that you declare as the type parameter and displays it in the drop down list of a ComboBox. There is no error checking. Rudedog =8^D
Mark the best replies as answers. "Fooling computers since 1971."- Marked As Answer byGreg Finzer Thursday, September 17, 2009 8:36 PM
-
|
| Rudedog2 Thursday, September 17, 2009 6:03 PM |
Thanks for your help Mark. This is great. |
| Greg Finzer Thursday, September 17, 2009 8:37 PM |