begin process at 2010 02 10 06:20:23
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

.NET

 > MODIFIER SES PROPRES CONTROLS

MODIFIER SES PROPRES CONTROLS


 Information sur la source

Note :
Aucune note
Catégorie :.NET Source .NET ( DotNet ) Classé sous :control, modifier Niveau :Débutant Date de création :12/05/2004 Date de mise à jour :12/05/2004 12:44:43 Vu :7 714

Auteur : bezhas

Ecrire un message privé
Commentaire sur cette source (2)
Ajouter un commentaire et/ou une note

 Description

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

  • 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";
  • }
  • }
  • }
  • }
  • }
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";
				}			
				
			}
		}

	}
}




 Sources du même auteur

Source avec Zip Source .NET (Dotnet) UN PROGRESSBAR AMÉLIORÉ
Source .NET (Dotnet) ECRIRE ET LIRE DANS LA BASE DES REGISTRES WINDOWS
Source .NET (Dotnet) IMPRIMER UN DATASET EN MULTIPAGES
Source .NET (Dotnet) COMMENT DESSINER SON PROPRE MENU

 Sources de la même categorie

Source avec Zip CHAT SERVER-CLIENT par abderrahmenbilog
Source avec Zip Source avec une capture Source .NET (Dotnet) SIMULATION DE CONSOLE POUR WINDOWS MOBILE par originalcompo
Source avec Zip Source .NET (Dotnet) BASE DE DONNÉES EN XML par DanMor498
Source avec Zip Source avec une capture Source .NET (Dotnet) SIMPLECONV - APPLICATION DE CONVERSION MONÉTAIRE AVEC TAUX E... par Jeffrey_
Source avec Zip Source .NET (Dotnet) TRAITEUR D'IMAGE (MINI) par ycyril

 Sources en rapport avec celle ci

Source avec Zip Source avec une capture Source .NET (Dotnet) LIBRAIRIE TRÈS COMPLETTE DE CONTROLS WINFORMS PERSONNALISÉS par jmenfous
Source avec Zip Source avec une capture Source .NET (Dotnet) USERCONTROL PYROWINDOW par alvinp
Source avec Zip Source avec une capture Source .NET (Dotnet) CONTROLE D'ONGLET PERSONNALISABLE par Yxion
Source avec Zip Source avec une capture Source .NET (Dotnet) GRAPHMONITOR par wizad
Source avec Zip Source avec une capture Source .NET (Dotnet) UNE FORM QUI S'INSÈRE PARTOUT par Yxion

Commentaires et avis

Commentaire de etudiant_developpeur le 25/08/2006 13:15:20

salut
votre code semble tres bien fai mai est ce que tu peu le faire en c# application web et merciii

Commentaire de angaladon le 10/05/2007 16:49:11

Juste une question :
Comment créer un tel controle ? Je suppose que ce n'est pas un controle utilisateur. Est ce une bête classe que je colle dans ma dll ? Un component ?

 Ajouter un commentaire


Discussions en rapport avec ce code source dans le forum

user control [ par cudenetf ] bonjour,Ppeut-on et comment faire pour avoir des controles ou des proprietes  contenues dans un user control  atteignable par le designer d'un forumla ajouter un evenement a usercontrol [ par cudenetf ] bonjour,j'ai un formulair equi utilise un user control (ce dernier possede plusieurs couleurs)j'aimmerais pouvoir definir ds le designer du formulaire "Anchor" un Control a une GraphicsPath. [ par D0X ] Salut! J'ai une "bizzarre" question: j'ai un UserControl et dans ce control j'ai une Textbox. Ce UserControl peut étre redimensionnée et on a la "Anc Résolution écran [ par RMI ] Bonjour,J'ai besoin d'une méthode pour modifier la résolution écran sans modifier la posion des icônes sur le bureau. Quelqu'un aurait-il une méthode probleme avec heritage [ par cudenetf ] bonjour,j'ai une form heritée d'une autre.QUand je modifie le form parent , j'ai un probleme avec le designer plus rien ne fonctionne (enfin j'ai ce m Modifier le nom d'une propriété héritée d'une classe mère [ par olivierbalagizi ] Bonjour chers Csharpiens.Je ne sais pas s'il ya un moyen de modifier, dans une classe fille, le nom d'une propriété hérité d'une classe mère dans C#.n comment modifier un élément de menu avec une fenêtre fille. [ par PascalCmoa ] Bonjour à tous,Voilà je suis en train de réaliser une application de type gestion des ventes.J'utilise le système de fenetres filles qui me semble pra Modifier les valeurs dans un datagrid [ par brute ] Bonjour, Dans mon application, j'ai une table que j'affiche dans un datagrid. Pour les 2 premieres colonnes, j'ai des valeurs entiere ( 1 ou 2). J'ai Equivalent à Control.Invoke? [ par leprov ] Existe-t-il un équivalent à la méthode control.invoke qui aie la meme fonctionnalité, mais lorsque l'on ne dispose pas d'un controle? c'est plus une c modifier le titre d'un objet de type GraphObject dans Crystal report [ par abn1981 ] Comment  je peut chager le tire d'un objet de type GraphOject.je essayer avec ce code mais l'objet de type GraphicObject  ne contient pas une propriet


Nos sponsors


Sondage...

CalendriCode

Février 2010
LMMJVSD
1234567
891011121314
15161718192021
22232425262728

Consulter la suite du CalendriCode

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,796 sec (3)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales