Bonjour à tous,

je débute en C#. Je teste les delegate et la récupération de valeurs de champs.

Je vous explique mon soucis.

j'ai une fenetre principale appelée Appli. Sur celle ci, j'ai 2 boutons (prenom et nom) qui ouvre chacun un nouvelle fenetre. Le resultat encodé par l'utilisateur s'affiche dans la fenetre Appli séparément. Sur cette même fenetre Appli j'ai un champ Resultat et un bouton valider. Le but du bouton valider est que lorsqu'on clique dessus, le prenom et le nom encodées viennent s'inscrire dans le champ Resultat.

J espere que c'est clair.

Cela n'a pas beaucoup d'intéret si ce n'est que de m'entrainer à récupérer des données entre fenêtres .

J'arrive à récupérer les valeurs de chacune des fentres mais je n'arrive pas à récupérer le tout (depuis chacune des fenetre) pour inscrire les prenom et nom dans le champs resultat.

Pouvez-vous m'aider?

Merci

Je vous mets les code ci-dessous :

le fichier Appli
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
 
namespace Delegate_Test_ListView
{
    public partial class Appli : Form
    {
        // Delegate Total
        public delegate void ClickTotal(string prenom, string nom);
 
        // Event
        public event ClickTotal EventTotal;
 
 
        public Appli()
        {
            InitializeComponent();
        }
 
        private void btnPrenom_Click(object sender, EventArgs e)
        {
            FormPrenom fenPrenom = new FormPrenom();
            fenPrenom.EventPrenom += new FormPrenom.ClickPrenom(RecupPrenom);
            fenPrenom.Show();
        }
 
        private void btnNom_Click(object sender, EventArgs e)
        {
            FormNom fenNom = new FormNom();
            fenNom.EventNom += new FormNom.ClickNom(RecupNom);
            fenNom.Show();
        }
 
        private void RecupPrenom(string prenom)
        {
            labPrenom.Text += " " + prenom; 
        }
 
        private void RecupNom(string nom)
        {
            labNom.Text += " " + nom; 
        }
 
        private void btnValider_Click(object sender, EventArgs e)
        {
           Appli fenTotal = new Appli();
           fenTotal.EventTotal += new Appli.ClickTotal(RecupTotal);
        }
 
        private void RecupTotal(string prenom, string nom)
        {
            txtResultat.Text = prenom + " " + nom;
        }
    }
}
le formPrenom

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
 
namespace Delegate_Test_ListView
{
    public partial class FormPrenom : Form
    {
        // Creation dun Type ClickPrenom
        public delegate void ClickPrenom(string prenom);
        // Inbstantiation d'un Event de type ClickPrenom
        public event ClickPrenom EventPrenom;
 
        public FormPrenom()
        {
            InitializeComponent();
        }
 
        private void btnRecordP_Click(object sender, EventArgs e)
        {
            EventPrenom(txtPrenom.Text);
            this.Close();
        }
 
        private void txtPrenom_TextChanged(object sender, EventArgs e)
        {
 
        }
    }
}
le FormNom

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
 
namespace Delegate_Test_ListView
{
    public partial class FormNom : Form
    {
        // Creation dun Type ClickNnom
        public delegate void ClickNom(string nom);
 
        // Inbstantiation d'un Event de type ClickNom
        public event ClickNom EventNom;
 
        public FormNom()
        {
            InitializeComponent();
        }
 
        private void btnRecordN_Click(object sender, EventArgs e)
        {
            EventNom(txtNom.Text);
            this.Close();
        }
    }
}