Bonjours quand je clique sur le bouton close de ma forme cet event et sensé se produire :
je dispose d'un textbox ou je verifie si il est vide ou non :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5 private void btCancel_Click(object sender, EventArgs e) { this.Close(); }
si la textbox n a pas le focus ok il ferme la form, si la textbox a le focus il fait la validation trouve une erreur (s il y en a une) et ne ferme pas la forme car ne passe jamais dans l'event click je comprend pas pourquoi? il devrait fermer ma forme quand même !
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 private void TxtValidating(object sender, CancelEventArgs e) { btValid.Enabled = IsValid(); } private void SetErrorProviderWithCount(Control control, string message) { if (string.IsNullOrWhiteSpace(message)) { if (errorProvider.GetError(control) != "") nbError--; } else if (errorProvider.GetError(control) == "") nbError++; errorProvider.SetError(control, message); } private bool HasErrorProvider() { return nbError != 0; } private bool IsValid() { if (string.IsNullOrWhiteSpace(txtServerAddress.Text)) { SetErrorProviderWithCount(txtServerAddress, "Field cannot be empty."); } else { SetErrorProviderWithCount(txtServerAddress, ""); } if (string.IsNullOrWhiteSpace(txtDefaultLoungeName.Text)) { SetErrorProviderWithCount(txtDefaultLoungeName, "Field cannot be empty."); } else { SetErrorProviderWithCount(txtDefaultLoungeName, ""); } if (string.IsNullOrWhiteSpace(txtServerInfo.Text)) { SetErrorProviderWithCount(txtServerInfo, "Field cannot be empty."); } else { SetErrorProviderWithCount(txtServerInfo, ""); } return !HasErrorProvider(); }
dans le doute voici le code complet :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 private int nbError = 0; public frm() { InitializeComponent(); //this.ActiveControl = lbServerAddress; btValid.Enabled = false; } private void frm_FormClosing(object sender, FormClosingEventArgs e) { if (btValid.Enabled) { if (IsValid()) { } } } private void btCancel_Click(object sender, EventArgs e) { this.Close(); } private void TxtValidating(object sender, CancelEventArgs e) { btValid.Enabled = IsValid(); } private void SetErrorProviderWithCount(Control control, string message) { if (string.IsNullOrWhiteSpace(message)) { if (errorProvider.GetError(control) != "") nbError--; } else if (errorProvider.GetError(control) == "") nbError++; errorProvider.SetError(control, message); } private bool HasErrorProvider() { return nbError != 0; } private bool IsValid() { if (string.IsNullOrWhiteSpace(txtServerAddress.Text)) { SetErrorProviderWithCount(txtServerAddress, "Field cannot be empty."); } else { SetErrorProviderWithCount(txtServerAddress, ""); } if (string.IsNullOrWhiteSpace(txtDefaultLoungeName.Text)) { SetErrorProviderWithCount(txtDefaultLoungeName, "Field cannot be empty."); } else { SetErrorProviderWithCount(txtDefaultLoungeName, ""); } if (string.IsNullOrWhiteSpace(txtServerInfo.Text)) { SetErrorProviderWithCount(txtServerInfo, "Field cannot be empty."); } else { SetErrorProviderWithCount(txtServerInfo, ""); } return !HasErrorProvider(); }
Partager