Modifier ses propres controls

Contenu du snippet

si on veut traiter un TextBox de façon à afficher le numéro de Tél ou SSN ou à n'afficher que des valeurs Double ou entières donc on est obligé de traiter chacun de ses controls ibdividuellement, donc le travail deviens pénible et fatiguant.
D'où la nécissité de dériver un control et de le traiter de manière à satsifaire toutes ses types de validation,
Dans cet exemple j'ai Dérivé un TextBox et j'ai traiter déffirent types de validation

Source / Exemple :


using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;

//Une DLL pour les controls personnalisész
namespace MesControls
{
public enum ValidationType
{
	Text=0,
	Numeric,
	Tel,
	Nom,
	DoubleValue,
}
public class MyTextBox : System.Windows.Forms.TextBox
{
	int intValidType=(int)ValidationType.Text;
	Color focusColor = Color.White;
	bool letKeyBoardAction = true;

	public MyTextBox()
	{
		this.BorderStyle = BorderStyle.FixedSingle;	
		this.Text = "";

		this.KeyPress    += new KeyPressEventHandler(ssOnKeyPress);
		this.LostFocus  +=new EventHandler(ssOnLostFocus); 
		this.GotFocus += new EventHandler(ssOnGotFocus);
		this.Validating += new CancelEventHandler(ssOnValidating);
		this.KeyDown += new KeyEventHandler(ssOnKeyDown);
	}

		[Description("La couleru utilisé lorsque le control a le Focus")]
	public Color FocusColor
	{
		get
		{
			return focusColor;
		}
		set
		{
			focusColor = value;
		}
	}
		
	public ValidationType ValidateFor
	{
		get
		{
			return (ValidationType)intValidType;
		}
		set	
		{
			intValidType =(int)value;
		}
	}
		
	public bool LetKeyBoardAction
	{
		get
		{
			return letKeyBoardAction;
		}
		set	
		{
			letKeyBoardAction = value;
		}
	}
		

	protected void ssOnLostFocus(object sender,EventArgs e)
	{
		this.BackColor = Color.White;
	}
	protected void ssOnGotFocus(object sender,EventArgs e)
	{
		this.BackColor = FocusColor;
	}

	private void ssOnKeyPress(object sender, KeyPressEventArgs e)
	{
		try{
			switch(intValidType){
			case (int)ValidationType.Text:
				switch(this.SelectionStart){
					case 0:case 1:
						if(!char.IsLetterOrDigit(e.KeyChar)&& e.KeyChar != 8)
								e.Handled = true;
							break;
					}
				break;
			case (int)ValidationType.Numeric:
				if(!char.IsDigit(e.KeyChar) && e.KeyChar != 8){
							e.Handled = true;
					}
				break;
				case (int)ValidationType.Tel:
				switch(this.SelectionStart){
					case 0:case 1:case 2:case 4:case 5:case 7:case 8:case 10:case 11:
						if(!char.IsDigit(e.KeyChar)&& e.KeyChar != 8)
								e.Handled = true;
							break;
						case 3:case 6:case 9:
							if(char.Parse("/") != e.KeyChar && e.KeyChar != 8)
								e.Handled = true;
							break;	
					}
						break;
					case (int)ValidationType.Nom:
					switch(this.SelectionStart){
						case 0:case 1:
							if(!char.IsLetter(e.KeyChar)&& e.KeyChar != 8)
								e.Handled = true;
							break;
						default:
							if(!char.IsLetter(e.KeyChar) && e.KeyChar != 8 && !char.IsWhiteSpace(e.KeyChar))
								e.Handled = true;
							break;	
					}
						break;
					case (int)ValidationType.DoubleValue:
						string decSep = NumberFormatInfo.CurrentInfo.NumberDecimalSeparator;
						if(char.Parse(decSep) != e.KeyChar && !char.IsDigit(e.KeyChar) &&
							e.KeyChar != 8)
						{

							e.Handled = true;
						}
						else 
						{
							if(char.Parse(decSep) == e.KeyChar)
							{
								if(Text.IndexOf(decSep) != -1)
									e.Handled = true;
							}						
						}
						break;
				}
			}
			catch{}
		}
		private void ssOnKeyDown(object sender, KeyEventArgs e)
		{
			if(letKeyBoardAction == true)
			{
				if(e.Modifiers == System.Windows.Forms.Keys.Control && e.KeyCode == System.Windows.Forms.Keys.C)
					this.Copy();
				if(e.Modifiers == System.Windows.Forms.Keys.Control && e.KeyCode == System.Windows.Forms.Keys.V)
					this.Paste();
				if(e.Modifiers == System.Windows.Forms.Keys.Control && e.KeyCode == System.Windows.Forms.Keys.X)
					this.Cut();
			}
		}
		private void ssOnValidating(object sender, CancelEventArgs e)
		{
			if(intValidType == (int)ValidationType.DoubleValue)
			{
				try
				{
					double val = 0;
					val = double.Parse(Text);
					Text = val.ToString();
				}
				catch
				{
					Text = "0";
				}			
				
			}
		}

	}
}

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.