Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Space between words in enum type?

Status
Not open for further replies.

mrdance

Programmer
Apr 17, 2001
308
0
0
SE
Is the following possible in any way?

public enum Country {
"United Kingdom" = 1
}

--- neteject.com - Internet Solutions ---
 
Not that I'm aware of.

Chip H.


____________________________________________________________________
Click here to learn Ways to help with Tsunami Relief
If you want to get the best response to a question, please read FAQ222-2244 first
 
With such syntax there is no way using enum but using a Hashtable could be a solution of what you search:
Code:
Hashtable ht = new Hashtable();
ht.Add("United Kingdom",1);
ht.Add("Brazil", 25);
ht.Add("USA",19);
ht.Add(1,"United Kingdom");
ht.Add(25,"Brazil");
ht.Add(19,"USA");
object val=null;
object key=null;
if (ht.ContainsKey("United Kingdom"))
{
	val = ht["United Kingdom"];
}
if (ht.ContainsValue(19))
{
	key = ht[19];
}
obislavu
 
Thanks but I want the contents to be displayed at designtime.


--- neteject.com - Internet Solutions ---
 
Use an underscore instead of a space, then.

Chip H.

____________________________________________________________________
Click here to learn Ways to help with Tsunami Relief
If you want to get the best response to a question, please read FAQ222-2244 first
 
With Enum you will have restriction, you can't have free text format.
So if you need exactly what you need as a text description for the option which can have spaces and special characters you must use class inherited from UITypeEditor:
Here is a complete code:
Code:
        /// <summary>
        /// Specifies the _Format for displaying and printing numbers, dates, times, and text.
        /// </summary>
        [Bindable(true), Category("Text"), DefaultValue(""), Editor(typeof(FormatEnum), typeof(UITypeEditor)), 
        Description("Specifies the _Format for displaying and printing numbers, dates, times, and text.")]
        public string Format
        {
            get{return this._Format;}
            set
            {
                if (this._Format != value)
                {
                    this._Format = value;
                    //this.Mask = ReplaceFormatCharacters(value);
                }
            }
        }


    #region enum designer
    /// <summary>
    /// Klasa vo koja se menuva propertyto vo sloboden tekst
    /// </summary>
    public class FormatEnum : UITypeEditor
    {
        private IWindowsFormsEditorService edSvc = null;
//        private ToolTip tooltipControl;
        private ListBox lst;
        private string[] formats = {"Only Text", "Only numeric"};
        /// <summary>
        /// Internal class used for storing custom data in listviewitems
        /// </summary>
        internal class lbItem
        {
            private string str;
            private int value;
            private string tooltip;
            /// <summary>
            /// Creates a new instance of the <c>lbItem</c>
            /// </summary>
            /// <param name="str">The string to display in the <c>ToString</c> method. 
            /// It will contains the name of the flag</param>
            /// <param name="value">The integer value of the flag</param>
            /// <param name="tooltip">The tooltip to display in the <see cref="CheckedListBox"/></param>
            public lbItem(string str, int value, string tooltip)
            {
                this.str = str;
                this.value = value;
                this.tooltip = tooltip;
            }
            /// <summary>
            /// Gets the int value for this item
            /// </summary>
            public int Value
            {
                get{return value;}
            }
            /// <summary>
            /// Gets the tooltip for this item
            /// </summary>
            public string Tooltip
            {
                get{return tooltip;}
            }
            /// <summary>
            /// Gets the name of this item
            /// </summary>
            /// <returns>The name passed in the constructor</returns>
            public override string ToString()
            {
                return str;
            }
        }
        /// <summary>
        /// Overrides the method used to provide basic behaviour for selecting editor.
        /// Shows our custom control for editing the value.
        /// </summary>
        /// <param name="context">The context of the editing control</param>
        /// <param name="provider">A valid service provider</param>
        /// <param name="value">The current value of the object to edit</param>
        /// <returns>The new value of the object</returns>
        public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, System.IServiceProvider provider, object value)
        {
            if (context != null
                && context.Instance != null
                && provider != null) 
            {

                edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
                if (edSvc != null) 
                {
                    lst = new ListBox();
                    lst.SelectedValueChanged += new EventHandler(ValueChanged);
                    lst.Items.AddRange(formats);
                    lst.Text = (string)lst.Items[lst.FindString((string)value)];
                    edSvc.DropDownControl(lst);
                    return lst.Text;
                }
            }
            return value;
        }
        /// <summary>
        /// Shows a dropdown icon in the property editor
        /// </summary>
        /// <param name="context">The context of the editing control</param>
        /// <returns>Returns <c>UITypeEditorEditStyle.DropDown</c></returns>
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) 
        {
            if (context != null && context.Instance != null)
                return UITypeEditorEditStyle.DropDown;
            else
                return base.GetEditStyle(context);
        }
        private bool handleLostfocus = false;
        /// <summary>
        /// When got the focus, handle the lost focus event.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnMouseDown(object sender, MouseEventArgs e) 
        {
            if(!handleLostfocus && lst.ClientRectangle.Contains(lst.PointToClient(new Point(e.X, e.Y))))
            {
                lst.SelectedIndexChanged += new EventHandler(this.ValueChanged);
                handleLostfocus = true;
            }
        }
        /// <summary>
        /// Close the dropdowncontrol when the user has selected a value
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ValueChanged(object sender, EventArgs e) 
        {
            if (edSvc != null) 
            {
                edSvc.CloseDropDown();
            }
        }
    }
    #endregion
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top