Hello,

J'ai une librairie dans laquelles je mets pas mal de méthode d'extension, et comme je suis actuellement en train de développer une appli winforms, j'aimerais créé des "validators" à la ASP.Net style. J'ai suivi un article sur MSDN qui explique un peu comment faire (source: https://msdn.microsoft.com/en-us/library/ms950965.aspx). Je trouve l'idée vraiment bien sachant que je suis déjà habitué aux contrôles de validation ASP.Net et que c'est vraiment dommage qu'il n'y en ai pas en WinForms.

J'ai donc le code suivant :

Code C# : 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
84
85
86
87
88
89
90
91
92
93
94
95
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
 
namespace Common.FormsControl
{
 
    // usefull ressource : https://msdn.microsoft.com/en-us/library/ms950965.aspx
 
    [ToolboxBitmap(typeof(RequiredFieldValidator), "RequiredFieldValidator.ico")]
    class RequiredFieldValidator : System.ComponentModel.Component
    {
        private Control _controlToValidate;
        private string _errorMessage;
        private Icon _icon;
        private string _initialValue;
        private bool _isValid;
        private ErrorProvider _errorProvider;
 
        [Category("Behavior")]
        [Description("Gets or sets the control to validate")]
        Control ControlToValidate {
            get { return _controlToValidate; } 
            set {
                _controlToValidate = value;
                if ((_controlToValidate != null) && (!DesignMode))
                {
                    _controlToValidate.Validating += new System.ComponentModel.CancelEventHandler(_controlToValidate_Validating);
                }
            }
 
        }
 
        private void _controlToValidate_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            // There is no e.Cancel because we don't want focus to stay on the control and block the user
            Validate();
        }
 
        [Category("Appearance")]
        [Description("Gets or sets the text for the error message.")]
        string ErrorMessage {
            get { return _errorMessage; }
            set { _errorMessage = value; } 
        }
 
        [Category("Appearance")]
        [Description("Gets or sets the Icon to display ErrorMessage.")]
        Icon Icon {
            get { return _icon; }
            set { _icon = value; }
        }
 
        [Category("Behaviour")]
        [Description("Gets or sets the default value to validate against.")]
        string InitialValue {
            get { return _initialValue; }
            set { _initialValue = value; }
        }
 
        [Category("Behaviour")]
        [Description("Gets or sets the value to know if it's valid or not.")]
        bool IsValid {
            get { return _isValid; }
            set { _isValid = value; }
        }
 
        [Category("Behaviour")]
        [Description("Performs the validation.")]
        void Validate()
        {
            // is valid if different from initial value
            string controlValue = ControlToValidate.Text;
            string initialValue;
            if (_initialValue == null) initialValue = "";
            else initialValue = _initialValue;
            _isValid = (controlValue != initialValue);
            // display an error if control to validate is invalid
            string errorMessage = "";
            if (!_isValid)
            {
                errorMessage = _errorMessage;
                _errorProvider.Icon = _icon;
            }
 
            _errorProvider.SetError(ControlToValidate, errorMessage);
        }
    }
}

Sauf qu'il n'apparaît pas dans la toolbox du project qui contient pas mes classes d'extension, ni même dans ma toolbox du projet contenant l'application. Je n'ai pas d'erreurs dans mon code non plus. D'ou peut venir mon erreur?

Je prévois de faire pareils avec d'autres validations genre RegularExpressionValidator, et même avoir un CustomValidator mais pour le moment je suis bloqué là dessus

Merci bien pour votre aide

L