Source code of text box which enables
alphanumeric input
only letter
upper case (all characters are converted to upper case)
1: using System;
2: using System.Collections.Generic;
3: using System.ComponentModel;
4: using System.Data;
5: using System.Drawing;
6: using System.Linq;
7: using System.Text;
8: using System.Windows.Forms;
9:
10: namespace Master
11: {
12: public enum InptTyp
13: {
14: alphanum,
15: onlyUpper,
16: onlyUpperLetter,
17:
18: }
19: public partial class AdvTextBox : TextBox
20: {
21: private InptTyp inputType;
22: public InptTyp InputType
23: {
24: get
25: {
26: return inputType;
27: }
28: set
29: {
30: inputType = value;
31: }
32:
33: }
34: public AdvTextBox()
35: {
36: InitializeComponent();
37: }
38:
39: protected override void OnPaint(PaintEventArgs pe)
40: {
41: base.OnPaint(pe);
42: }
43: protected override void OnKeyPress(KeyPressEventArgs e)
44: {
45: int i = (int)e.KeyChar;
46: switch (inputType)
47: {
48: case InptTyp.alphanum:
49: if ((i < 43 || i > 60) && i != 8)//did not use char.isnumeric because this allows to add - and . like characters
50: e.Handled = true;
51: break;
52: case InptTyp.onlyUpperLetter:
53: if (!Char.IsLetter(e.KeyChar))
54: {
55: e.Handled = true;
56: return;
57: }
58: goto case InptTyp.onlyUpper;
59: case InptTyp.onlyUpper:
60: e.KeyChar = Char.ToUpper(e.KeyChar);
61: break;
62: }
63:
64: base.OnKeyPress(e);
65:
66: }
67:
68:
69: }
70: }
No comments:
Post a Comment