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!

Custom textbox control

Status
Not open for further replies.

soodvarun

Programmer
Apr 21, 2001
40
0
0
CA
Hey frnds !
I am designing a custom textbox in which I have my own property in the property panel "Format" when I select this I should get a drop down combo from which I can select various options like "Only Text". "Only numeric" etc....

Can anyone let me know how to get this?

Thanks in advance

Varun

soodvarun@foxysolutions.com
 
You can use Enumerated datatype to do this. As an example

Code:
'This is what going to show up in dropdown 
Public Enum allowedFormats

  OnlyText
  OnlyNumber
  Both
End Enum

'Property that is going to show up in Property window.
Public Property allowedFormat() As allowedFormats
  Get

           
  End Get
  Set(ByVal Value As allowedFormats)

  End Set
End Property

-Kris
 
Yes, make an Enum that the calling program can access. For example:

Public Enum DataType
Only_Text = 0
Only_Numeric = 1
End Enum

Then set your property to that type:

Public Property Format As DataType
....

 
Sorry Kris. I was obviously typing at the same time as you.
 
Here is how I did for my textbox.

Define an Enum and then use it as the Type for the Property

Code:
<Category("Format"), _
    Description("Conversion Type for a string."), _
    BrowsableAttribute(True), _
    DefaultValue(jytxConvType.None)> _
    Public Property Conversion() As jytxConvType
        'Blah
    End Propety

<Category("Format"), _
    Description("DataType to which input is to be converted."), _
    BrowsableAttribute(True)> _
    Public Property Datatype() As jytxDataType
       'Blah
    End Propety

    Public Enum jytxConvType
        None            ' No conversions
        ToUpper       ' Convert a-z to A-Z
        ToLower       ' Convert A-Z to a-z
    End Enum
    '*
    '*****
    '* Data Type of eventual destination of data
    '*****
    Public Enum jytxDataType
        Any          ' 
        Alpha        ' 
        AlphaNumeric ' 
        Int16        ' signed or unsigned. no decimals,
        ' &H and &O formats,
        ' -32,768 to < 0 if sign allowed
        ' 0 to 32,767
        Int32      ' signed or unsigned, no decimals,
        ' &H and &O formats,
        ' -2,147,483,648 to 2,147,483,647
        Int64         ' signed or unsigned, no decimals,
        ' &H and &O formats,
        ' -9,223,372,036,854,775,808 through 9,223,372,036,854,775,807. 
        Currency     ' signed or unsigned, 0 to 4 decimals
        ' -922,337,203,685,477.5808 to < 0 if sign allowed,
        ' 0 to 922,337,203,685,477.5807 always
        [Double]       ' Double, signed or unsigned, unlimited decimals,
        ' E and D formats,
        ' -1.79769313486231E308 to -4.94065645841247E-324 for negative values if sign allowed,
        ' 4.94065645841247E-324 to 1.79769313486232E308 for positive values.
        [Single]       ' Single, signed or unsigned,
        ' E and D formats.
        ' -3.402823E38 to -1.401298E-45 for negative values if sign allowed
        ' 1.401298E-45 to 3.402823E38 for positive values
        [Decimal]      '0 through +/-79,228,162,514,264,337,593,543,950,335 with no decimal point; 
        '0 through +/-7.9228162514264337593543950335 with 28 places to the right of the decimal; smallest nonzero number is 
        '+/-0.0000000000000000000000000001 (+/-1E-28). 
    End Enum

Forms/Controls Resizing/Tabbing
Compare Code
Generate Sort Class in VB
Check To MS)
 
I understand what the code is supposed to do, but am not sure where this is placed. Where would this code get used to create the new textbox?

 
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