Il n'y a rien à creuser... On vient de te livrer une solution bien plus complète et tu t'acharnes sur la solution de départ ?!
Ce sont juste les équivalents numériques des caractères. Et encore une fois, ca ne gèrera que les frappes clavier.
Version imprimable
Il n'y a rien à creuser... On vient de te livrer une solution bien plus complète et tu t'acharnes sur la solution de départ ?!
Ce sont juste les équivalents numériques des caractères. Et encore une fois, ca ne gèrera que les frappes clavier.
Effectivement mon erreur provient du fait que je testais dans le bloc if. Pour les parenthèse c'est ce que je pensais car cela ne faisait pas la différence lorsque je les enlevais. Cependant j'aimais mieux demander car on ne sais jamais :)
Bob -> Je te conseil vivement d'utiliser le unsercontrol que nous te proposons.
Voici les dernier modif :
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109 using System; using System.Collections.Generic; using System.Text; using System.Drawing; using System.Globalization; using System.Windows.Forms; namespace PrestigeGestion { public class NumericTextBox : TextBox { private const int WM_PASTE = 0x0302; protected override void WndProc(ref Message msg) { switch (msg.Msg) { case WM_PASTE: // received a paste event, handle it here HandlePasteData(); break; default: // let the event continue its way base.WndProc(ref msg); break; } } protected override void OnKeyPress(KeyPressEventArgs e) { char c = e.KeyChar; // Char.IsControl let Copy/Paste pass if (Char.IsControl(c) || IsValidChar(c)) { base.OnKeyPress(e); } else { // suppress the key press e.Handled = true; } } private void HandlePasteData() { // retrieve clipboard data IDataObject clipboard = Clipboard.GetDataObject(); // check if the clipboard contains text data if (clipboard.GetDataPresent(DataFormats.Text)) { string copyStr = (string)clipboard.GetData(DataFormats.Text); // clean the text from unwanted characters copyStr = RemoveUnWantedChars(copyStr); // initializes a StringBuilder with ... StringBuilder sb = new StringBuilder(); // ... the text before the selection ... sb.Append(Text.Substring(0, SelectionStart)); // ... the cleaned text ... sb.Append(copyStr); // ... and the text after the selection sb.Append(Text.Substring(SelectionStart + SelectionLength)); // truncate the text length to MaxLength of the TextBox sb.Length = Math.Min(sb.Length, MaxLength); // mimics the paste behavior for windows TextBox : the caret is moved after the pasted data int newSelStart = SelectionStart + copyStr.Length; // set the nex text Text = sb.ToString(); ; // ensure the caret is not after MaxLength, to avoir ArgumentOutOfRangeException SelectionStart = Math.Min(newSelStart, MaxLength); // no selection SelectionLength = 0; } } private static string RemoveUnWantedChars(string s) { StringBuilder sb = new StringBuilder(); foreach (char c in s) { if (IsValidChar(c)) { sb.Append(c); } } return sb.ToString(); } private static bool IsValidChar(char c) { if (Char.IsDigit(c)) { return true; } else if (c == ',' || c == '.') { return true; } else return false; } } }