Accueil > > > MODIFIER SES PROPRES CONTROLS
MODIFIER SES PROPRES CONTROLS
Information sur la source
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
Sources de la même categorie
Commentaires et avis
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
|
Derniers Blogs
[TECHDAYS2012] OUI J'Y SERAI![TECHDAYS2012] OUI J'Y SERAI! par JeremyJeanson
Bonsoir, Certes, je l'annonce avec un peu de retard, mais je serai effectivement au Techdays demain. Comme l'an dernier, je participerai au programme ATE (Ask The Expert). Si vous avez des questions Workflow, WCF, AppFabric ou plus généralement .net, n'hé...
Cliquez pour lire la suite de l'article par JeremyJeanson TFS INTEGRATION TOOLS - SUIVI DES SYNCHRONISATIONS AVEC REPORTING SERVICESTFS INTEGRATION TOOLS - SUIVI DES SYNCHRONISATIONS AVEC REPORTING SERVICES par vfabing
Afin de s'assurer du bon fonctionnement des différentes synchronisations effectuées par les TFS Integration Tools, 2 rapports sont présents dès l'installation. Il suffit alors d'effectuer les manipulations suivantes pour pouvoir les visualiser : Loca...
Cliquez pour lire la suite de l'article par vfabing CSS CONTENT STATE SELECTORS (PERSONNAL DRAFT)CSS CONTENT STATE SELECTORS (PERSONNAL DRAFT) par FREMYCOMPANY
Bonjour à tous, Je viens de publier une proposition comprenant 5 pseudo-classes pour le CSS Working Group ayant trait à l'état de chargement d'un élément (ex: IMG,VIDEO,AUDIO,OBJECT pour l'HTML.). Si le c½ur vous en dit, vous pouvez retrouver cette p...
Cliquez pour lire la suite de l'article par FREMYCOMPANY MBA : POURQUOI FAIRE ET COMMENT LE CHOISIR ?MBA : POURQUOI FAIRE ET COMMENT LE CHOISIR ? par ROMELARD Fabrice
Formation initiale Durant la formation, le découpage classique est le suivant (je donnerai les équivalences Suisse lorsque je les connaîtrais) : Ecole primaire jusqu'au Collège : Formation générale permettant d'obtenir les méthodes...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice Y'A DES ERREURS QUI PEUVENT RENDRE LE DéVELOPPEUR VIOLENTY'A DES ERREURS QUI PEUVENT RENDRE LE DéVELOPPEUR VIOLENT par Aleks
Quand on a ce genre d'erreur sans log :
Et bas on a juste envie de choper le gas de Microsoft qu'a développé ça et lui foutre des baffes de Coboye ! ...
Cliquez pour lire la suite de l'article par Aleks
Logiciels
Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System Easy-Planning (1.0.0.1)EASY-PLANNING (1.0.0.1)Basé sur les mêmes principes que MyPlanning, Easy-Planning permet de créer des plannings sous la ... Cliquez pour télécharger Easy-Planning COLLECTOR PLUS (3.00B)COLLECTOR PLUS (3.00B)COLLECTOR PLUS version 3.00B est un logiciel utilisant une base de données alimentée par :
- L... Cliquez pour télécharger COLLECTOR PLUS PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO LettresFaciles 2011 (8.0.0.1)LETTRESFACILES 2011 (8.0.0.1)LettresFaciles est un logiciel facilitant la création et la rédaction de lettres types.
Son inte... Cliquez pour télécharger LettresFaciles 2011
|