Sie sind auf Seite 1von 8

Componente em C#

TextBox com validao de CPF, CNPJ e mascara dinmica


Posted on 23/09/2010
Seguindo a linha do ultimo post, mais um componente.
Um componente do tipo TextBox, com validao de CPF e CNPJ e com mascara dinmica,
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
f
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
o
044
o, CNPJ
045
046
047
048
049
050
051

using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Windows.Forms;
System.Drawing;
System.ComponentModel;

//Criado do Wagner Braz Rodrigues


//Data 23/09/2010
//Ao utilizar este componente
//por favor, referencie o criador
namespace Componentes
{
class TextBoxCpfCnpj : TextBox
{
private String tipo;
//Property Tipo onde o programador vai definir o tipo cnpj ou cp
public String Tipo
{
get { return tipo; }
set
{
try
{
tipo = value;
//Verifica qual o tipo para atribuir
//o tamanho maximo da string
if (tipo.ToUpper() == "CPF")
{
MaxLength = 14;
}
else if (tipo.ToUpper() == "CNPJ")
{
MaxLength = 18;
}
}
catch
{
//Exception criada para caso o objeto esteja sem tip
throw new Exception("TextBoxCpfCnpj: Entre com o Tip
ou CPF");
}
}
}
//Sobescreve o metodo OnLeave
//Verifica qual o tipo CNPJ ou CPF

052
//e chama a validao, caso retorne
053
//falso retorna o foco ao objeto
054
//e emite um beep
055
protected override void OnLeave(EventArgs e)
056
{
057
base.OnLeave(e);
058
059
if (tipo.ToUpper() == "CPF")
060
{
061
if (!ValidaCPF(Text))
062
{
063
System.Console.Beep(100,100);
064
this.Focus();
065
}
066
}
067
068
if (tipo.ToUpper() == "CNPJ")
069
{
070
if (!ValidaCnpj(Text))
071
{
072
System.Console.Beep(100,100);
073
this.Focus();
074
}
075
}
076
}
077
078
//Metodo que cria a mascara dinamica para CPF
079
private void AjustaMascaraCPF()
080
{
081
int cont = 0;
082
int cursorPos = SelectionStart;
083
084
foreach (Char c in Text)
085
{
086
if (((cont == 3) || (cont == 7)) && (c != '.') && (Text.
Length >= cont))
087
{
088
Text = Text.Insert(cont,".");
089
SelectionStart = cursorPos + 1;
090
}
091
if ( (c == '.') && (Text.Length >= cont) && (cont != 3)
&& (cont != 7) )
092
{
093
Text = Text.Remove(cont,1);
094
SelectionStart = cursorPos;
095
}
096
097
if ( (cont == 11) && (c != '-') && (Text.Length >= cont)
)
098
{
099
Text = Text.Insert(11, "-");
100
SelectionStart = cursorPos + 1;
101
}
102
if ((c == '-') && (cont != 11) && (Text.Length >= cont))
103
{
104
Text = Text.Remove(cont,1);
105
SelectionStart = cursorPos;
106
}
107
108
cont++;

109
}
110
}
111
112
//Metodo que cria a mascara dinamica para CNPJ
113
private void AjustaMascaraCnpj()
114
{
115
int cont = 0;
116
int cursorPos = SelectionStart;
117
118
foreach (Char c in Text)
119
{
120
if (((cont == 2) || (cont == 6)) && (c != '.') && (Text.
Length >= cont))
121
{
122
Text = Text.Insert(cont, ".");
123
SelectionStart = cursorPos + 1;
124
}
125
if ((c == '.') && (Text.Length >= cont) && (cont != 2) &
& (cont != 6))
126
{
127
Text = Text.Remove(cont, 1);
128
SelectionStart = cursorPos;
129
}
130
131
if ((cont == 10) && (c != '\\') && (Text.Length >= cont)
)
132
{
133
Text = Text.Insert(10, @"\");
134
SelectionStart = cursorPos + 1;
135
}
136
if ((c == '\\') && (cont != 10) && (Text.Length >= cont)
)
137
{
138
Text = Text.Remove(cont, 1);
139
SelectionStart = cursorPos;
140
}
141
142
if ((cont == 15) && (c != '-') && (Text.Length >= cont))
143
{
144
Text = Text.Insert(15, "-");
145
SelectionStart = cursorPos + 1;
146
}
147
if ((c == '-') && (cont != 15) && (Text.Length >= cont))
148
{
149
Text = Text.Remove(cont, 1);
150
SelectionStart = cursorPos;
151
}
152
153
cont++;
154
}
155
}
156
157
158
//Sobescreve o metodo OnKeyUp
159
//Verifica qual o tipo CPF ou CNPJ
160
//e e chama o metodo para a mascara dinamica
161
protected override void OnKeyUp(KeyEventArgs e)
162
{
163
base.OnKeyUp(e);
164

165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
2 };
195
4, 3, 2 };
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
tiplicador1[i];
214
215
216
217
218
219
220
221

if (tipo.ToUpper() == "CPF")
AjustaMascaraCPF();
else if (tipo.ToUpper() == "CNPJ")
AjustaMascaraCnpj();
}
//Sobescreve o metodo onKeyPress, para evitar que
//o usuario entre com um valor no numerico
protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress(e);
if ((!Char.IsDigit(e.KeyChar)) && (e.KeyChar != '\x0008'))
{
e.KeyChar = '\x0000';
}
}
//Sobrecarga de construtores
//permitindo criar o objeto em tempo de execuo
//atribuindo o tipo
public TextBoxCpfCnpj(String t){ Tipo = t;}
public TextBoxCpfCnpj(){}
//Valida CPF
private bool ValidaCPF(String cpf)
{
Boolean valida = true;
int[] multiplicador1 = new int[9] { 10, 9, 8, 7, 6, 5, 4, 3,
int[] multiplicador2 = new int[10] { 11, 10, 9, 8, 7, 6, 5,
String tempCpf;
String digito;
String verifica;
int soma;
int resto;
cpf = cpf.Trim();
cpf = cpf.Replace("-","").Replace(".","");
if (cpf.Length == 11)
{
verifica = cpf.Substring(9);
tempCpf = cpf.Substring(0, 9);
soma = 0;
for (int i = 0; i < 9; i++)
{
soma = soma + int.Parse(tempCpf[i].ToString()) * mul
}
resto = soma % 11;
resto = resto < 2 ? resto = 0 : resto = 11 - resto; ;
digito = resto.ToString();

222
223
224
225
226
227
tiplicador2[i];
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280

tempCpf = tempCpf + digito;


soma = 0;
for (int i = 0; i < 10; i++)
{
soma = soma + int.Parse(tempCpf[i].ToString()) * mul
}
resto = soma % 11;
resto = resto < 2 ? resto = 0 : resto = 11 - resto; ;
digito = digito + resto.ToString();
if (digito != verifica)
{
valida = false;
}
switch (cpf)
{
case "00000000000":
{
valida = false;
break;
}
case "11111111111":
{
valida = false;
break;
}
case " 22222222222 ":
{
valida = false;
break;
}
case "33333333333":
{
valida = false;
break;
}
case "44444444444":
{
valida = false;
break;
}
case "55555555555":
{
valida = false;
break;
}
case "66666666666":
{
valida = false;
break;
}
case "77777777777":
{
valida = false;
break;

281
}
282
case "88888888888":
283
{
284
valida = false;
285
break;
286
}
287
case "99999999999":
288
{
289
valida = false;
290
break;
291
}
292
}
293
}
294
else
295
{
296
valida = false;
297
}
298
return valida;
299
}
300
301
//Valida CNPJ
302
private bool ValidaCnpj(String cnpj)
303
{
304
Boolean valida = true;
305
int[] multiplicador1 = new int[12] { 5, 4, 3, 2, 9, 8, 7, 6,
5, 4, 3, 2 };
306
int[] multiplicador2 = new int[13] { 6, 5, 4, 3, 2, 9, 8, 7,
6, 5, 4, 3, 2 };
307
String tempCnpj;
308
String digito;
309
String verifica;
310
int soma;
311
int resto;
312
313
cnpj = cnpj.Trim();
314
cnpj = cnpj.Replace("/","").Replace(".","").Replace("-","");
315
316
if (cnpj.Length == 14)
317
{
318
verifica = cnpj.Substring(12);
319
tempCnpj = cnpj.Substring(0, 12);
320
soma = 0;
321
322
for (int i = 0; i < 12; i++)
323
{
324
soma = soma + int.Parse(tempCnpj[i].ToString()) * mu
ltiplicador1[i];
325
}
326
327
resto = soma % 11;
328
resto = resto < 2 ? resto = 0 : resto = 11 - resto; ;
329
digito = resto.ToString();
330
331
tempCnpj = tempCnpj + digito;
332
333
soma = 0;
334
for (int i = 0; i < 13; i++)
335
{
336
soma = soma + int.Parse(tempCnpj[i].ToString()) * mu
ltiplicador2[i];

337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396

}
resto = soma % 11;
resto = resto < 2 ? resto = 0 : resto = 11 - resto; ;
digito = digito + resto.ToString();
if (digito != verifica)
{
valida = false;
}
switch (cnpj)
{
case "11111111111111":
{
valida = false;
break;
}
case "22222222222222":
{
valida = false;
break;
}
case "33333333333333":
{
valida = false;
break;
}
case "44444444444444":
{
valida = false;
break;
}
case "55555555555555":
{
valida = false;
break;
}
case "66666666666666":
{
valida = false;
break;
}
case "77777777777777":
{
valida = false;
break;
}
case "88888888888888":
{
valida = false;
break;
}
case "99999999999999":
{
valida = false;
break;
}
}
}

397
398
399
400
401
402
403
404

else
{
valida = false;
}
return valida;
}
}
}

Das könnte Ihnen auch gefallen