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
UNE JOLIE-HORLOGE ET PAS QU'UN PEU !UNE JOLIE-HORLOGE ET PAS QU'UN PEU ! par neodante
Pour les possesseurs d'iPhone, ça y est Bijin Tokei - qui se traduit littéralement en Français par " Jolie Horloge " - est arrivé et GRATUITEMENT s'il vous plaît ! Après la version Tokyo, Hokkaido, night club, racing, Gal, "pour les mademoiselles'", . voi...
Cliquez pour lire la suite de l'article par neodante TECHDAYS PARIS 2010 : CONNECTEZ VOS DONNéES à SHAREPOINT 2010 AVEC LES BUSINESS CONNECTIVITY SERVICESTECHDAYS PARIS 2010 : CONNECTEZ VOS DONNéES à SHAREPOINT 2010 AVEC LES BUSINESS CONNECTIVITY SERVICES par ROMELARD Fabrice
Animé par: Gaetan Bouveret et Julien Chomarat Business Connectivity Services (BCS) est dans SharePoint 2010 la version 2 de Business Data Catalog (BDC dans SharePoint 2007). Il s'agit de la solution permettant de visualiser des données provenan...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice [DIVERS] SUIVRE VOS SéRIES PRéFéRéS SUR LA TOILE[DIVERS] SUIVRE VOS SéRIES PRéFéRéS SUR LA TOILE par orion
Comme de nombreux geek, je suis un grand amateur de série TV et je rate régulièrement des épisodes de mes séries préférés. Une solution s'offre à vous avec ce merveilleux site : Tv Gorge - www.tvgorge.com Moteur de recherche à l'appui, vous pouvez ...
Cliquez pour lire la suite de l'article par orion TECHDAYS PARIS 2010 : LA BI DANS SHAREPOINT 2010TECHDAYS PARIS 2010 : LA BI DANS SHAREPOINT 2010 par ROMELARD Fabrice
Animé par: Vincent Bellet et Baptiste Giraudier La BI dans SharePoint 2010, Les nouveaux services d'application dans SP2010 et SQL Server Reporting services 2008 R2. La BI dans SharePoint est généralisée pour tous afin de permettre à tous les coll...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Logiciels
DB-MAIN (9.1.0)DB-MAIN (9.1.0)DB-MAIN is a data-modeling and data-architecture tool. It is designed to help developers and anal... Cliquez pour télécharger DB-MAIN Xilisoft DPG Convertisseur (5.1.37.0120)XILISOFT DPG CONVERTISSEUR (5.1.37.0120)Xilisoft DPG Convertisseur offre aux fans de Nintendo DS une bonne solution leur permettant de dé... Cliquez pour télécharger Xilisoft DPG Convertisseur GraphicsGale (2.01.01)GRAPHICSGALE (2.01.01)GraphicsGale est un logiciel de PixelArt avec de nombreuse fonctionnalités permettant de réalisé ... Cliquez pour télécharger GraphicsGale Architecte 3D (Platinum 2010)ARCHITECTE 3D (PLATINUM 2010)Architecte 3D Platinium vous permet de concevoir facilement les plans votre future maison, de l'é... Cliquez pour télécharger Architecte 3D TeamViewer 5 (TeamViewer 5)TEAMVIEWER 5 (TEAMVIEWER 5)Dépanner un ami,expliquer une manipulation devient un jeu d'enfant.
Prise en main d'un autre ord... Cliquez pour télécharger TeamViewer 5
|