Here below, I have created the TextBox control with browable properties (TextBox Mode: Alphabet only, Number only, Alphabet & Special Character and Number & Special Character)
To do this, Let me create an user control as below
To do this, Let me create an user control as below
My myTextBox user control's designer cs file code:
partial class myTextBox
{
/// <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.txtValue = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// txtValue
//
this.txtValue.Dock = System.Windows.Forms.DockStyle.Fill;
this.txtValue.Location = new System.Drawing.Point(0, 0);
this.txtValue.Multiline = true;
this.txtValue.Name = "txtValue";
this.txtValue.Size = new System.Drawing.Size(143, 21);
this.txtValue.TabIndex = 0;
this.txtValue.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtValue_KeyPress);
//
// myTextBox
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.txtValue);
this.Name = "myTextBox";
this.Size = new System.Drawing.Size(143, 21);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox txtValue;
}
My myTextBox user control's code behind cs file code:
[ToolboxBitmap(typeof(DataGrid))]
public partial class myTextBox : UserControl
{
TextBoxTypeSettings.Mode textBoxTypeSettings; //= new TextBoxTypeSettings();
public myTextBox()
{
InitializeComponent();
}
[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Always)]
public TextBoxTypeSettings.Mode TextBoxType
{
get
{
return this.textBoxTypeSettings;
}
set
{
this.textBoxTypeSettings = value;
}
}
private void txtValue_KeyPress(object sender, KeyPressEventArgs e)
{
switch (textBoxTypeSettings)
{
case TextBoxTypeSettings.Mode.AlphabetOnly:
e.Handled = !(char.IsLetter(e.KeyChar) || e.KeyChar == (char)Keys.Back);
break;
case TextBoxTypeSettings.Mode.NumberOnly:
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && (e.KeyChar != '.'))
{
e.Handled = true;
}
// only allow one decimal point
if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1))
{
e.Handled = true;
}
break;
}
}
}
My TextBoxTypeSettings class file:
[TypeConverter(typeof(TextBoxTypeSettingsConverter))]
public class TextBoxTypeSettings
{
private Mode _TextBoxMode;
[Serializable]
public enum Mode
{
NumberOnly,
AlphabetOnly
}
[Browsable(true)]
[DefaultValue("")]
[Description("Gets and sets the textbox's mode")]
[NotifyParentProperty(true)]
[EditorBrowsable(EditorBrowsableState.Always)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public Mode TextBoxMode
{
get
{
return _TextBoxMode;
}
set
{
_TextBoxMode = value;
}
}
}
My TextBoxTypeSettingsConverter class:
public class TextBoxTypeSettingsConverter : ExpandableObjectConverter
{
// This override prevents the PropertyGrid from
// displaying the full type name in the value cell.
public override object ConvertTo(
ITypeDescriptorContext context,
CultureInfo culture,
object value,
Type destinationType)
{
if (destinationType == typeof(string))
{
return "";
}
return base.ConvertTo(
context,
culture,
value,
destinationType);
}
}