IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Windows Forms Discussion :

Modification d'un Control via une classe [Débutant]


Sujet :

Windows Forms

  1. #1
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Février 2013
    Messages
    10
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2013
    Messages : 10
    Points : 5
    Points
    5
    Par défaut Modification d'un Control via une classe
    Bonjour,

    Tout d'abord, je suis au courant qu'il existe d'autres (anciens) sujets répondant à ce problème, mais si je crée un nouveau post, c'est que ma demande est différente

    Je vous présente rapidement mon projet :
    Je dispose d'une base de données Access contenant des commandes de produits (qui sont, pour info, associées à un site en particulier et à un service au sein de l'entreprise).

    Mon objectif est le suivant : rendre visible des labels et modifier leur contenu, en ayant le minimum de code sur la page contenant les événements du formulaire (pour des caprices du prof qui risque d'examiner mon projet), soit donc en utilisant une fonction définie dans ma classe Commande.
    Si je ne trouve pas de solution, tant pis pour le prof, je retournerai à la méthode classique, en créant une fonction directement sur la page où se trouve le code de l'événement...

    Un utilisateur peut rechercher une commande : il saisit la référence et clique sur le bouton "Rechercher".
    Voici le code déclencheur (faisant partie de la page où j'aimerais le moins de code possible) :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    private void btChercherCommande_Click(object sender, EventArgs e)
    {
        string refCmdSaisie = SaisieRefCommande.Text;
        if (refCmdSaisie != "")
       {
            Commande commandeCherchee = new Commande(refCmdSaisie);
     
            // Si la référence existe, on affiche les détails de la commande
            commandeCherchee.afficherCommande(refCmdSaisie);
        }
    }
    Voici les méthodes de ma classe Commande :

    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
     
            // Cette fonction est utilisée pour faire apparaître les informations principales d'une commande
            // (avec le label avec libellé d'info et le label de données)
     
            // labelTxt : Nom du label qui contiendra la valeur
            // labelLib : Nom du label qui contient le libellé associé au champ
     
            public void labelCmd(Label labelTxt, string message, Label labelLib)
            {
                labelTxt.Text = message;
                labelTxt.Visible = true;
                labelLib.Visible = true;
            }
     
            // Cette fonction vérifie si la commande recherchée existe
            // Si oui, on affiche ses détails
            // Si non, on affiche un message d'erreur
     
            public void afficherCommande(string refCommandeForm)
            {
                // Objet du formulaire où se situent les données à afficher
                formAccueil accueil = new formAccueil();
     
                maConnexion = ConnexionSingleton.GetLinstance();
                maConnexion.Ouvrir();
                maCommand = maConnexion.GetConnexion().CreateCommand();
     
                string reqInfosCommande = "SELECT Se.libelleService, Si.nomClinique, C.fournisseurCommande, "
                + "C.dateCommande, C.dateLivraison, C.nomNegociateur, C.prenomNegociateur "
                + "FROM commande C, site Si, service Se "
                + "WHERE C.refClinique = Si.refClinique "
                + "AND C.codeService = Se.codeService "
                + "AND C.refCommande = '" + refCommandeForm + "' ";
                maCommand.CommandText = reqInfosCommande;
     
                DbDataReader data = maCommand.ExecuteReader();
     
                // Si la requête a un résultat --> que la référence existe, on affiche les infos de la commande
                if (data.HasRows)
                {
                    accueil.lbAlerteCmd.Visible = false;
     
                    while (data.Read())
                    {
                        // Mise à jour et "apparition" des labels
     
                        labelCmd(accueil.lbValClinique, data["nomClinique"].ToString(), accueil.lbLibClinique);
                        labelCmd(accueil.lbValService, data["libelleService"].ToString(), accueil.lbLibService);
                        labelCmd(accueil.lbValFournisseur, data["fournisseurCommande"].ToString(), accueil.lbLibFournisseur);
                        labelCmd(accueil.lbValDateCmd, data["dateCommande"].ToString(), accueil.lbLibDateCmd);
                        labelCmd(accueil.lbValDateLivraison, data["dateLivraison"].ToString(), accueil.lbLibDateLivraison);
                        labelCmd(accueil.lbValNomNego, data["nomNegociateur"].ToString(), accueil.lbLibNomNego);
                        labelCmd(accueil.lbValPrenomNego, data["prenomNegociateur"].ToString(), accueil.lbLibPrenomNego);
                    }
                }
     
                maConnexion.Fermer();
            }
        }

    Mon problème est le suivant : j'ai mis la propriété "Modifiers" des labels en "public" (ce qui je suppose autorise la modification depuis une classe).
    Je n'ai aucune erreur de compilation, et pourtant les labels ne se mettent pas à jour.
    J'ai fait plusieurs tests, il ne semble pas y avoir de problèmes de format de données.
    Le programme passe bien dans la boucle, la référence de commande saisie est correcte, et la base de données est bien remplie.
    (Sur mon vrai code, j'ai mis toutes les boucles else pour afficher un message quand si les champs sont vides, quand la requête ne donne pas de résultat etc...)

    Il y a de grandes chances pour que l'erreur soit bête, mais étant donné que je me suis mis depuis peu au C#, j'ai quelques difficultés à l’apercevoir
    J'espère avoir été assez clair !

    Je vous remercie donc d'avance pour vos solutions et/ou vos conseils

    Cordialement,
    Owl'

  2. #2
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Février 2013
    Messages
    10
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2013
    Messages : 10
    Points : 5
    Points
    5
    Par défaut
    J'ai modifié ainsi ma fonction labelCmd :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    public void labelCmd(ref Label labelTxt, string message, ref Label labelLib)
    {
         labelTxt.Text = message;
         labelTxt.Visible = true;
         labelLib.Visible = true;
    }
    --> Ajout des ref, que j'ai aussi ajouté à l'appel de la fonction.

    Mais, le problème subsiste toujours.

  3. #3
    Membre éclairé Avatar de chamamo
    Profil pro
    Inscrit en
    Juin 2006
    Messages
    588
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2006
    Messages : 588
    Points : 735
    Points
    735
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    public void afficherCommande(string refCommandeForm)
            {
                // Objet du formulaire où se situent les données à afficher
                formAccueil accueil = new formAccueil();
    }
    Déjà à première vu tu n'auras rien sur ton formulaire accueil car il est instancié dans ta méthode, à la sortie de la méthode cet objet n'est plus valable, il faut que tu le passes dans la méthode.

    Autre remarque, pas besoin de mettre le "ref" car tu passe un objet de type référence, le ref ou out sont utilisé pour les type valeur (int, string ...)

  4. #4
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Février 2013
    Messages
    10
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2013
    Messages : 10
    Points : 5
    Points
    5
    Par défaut
    Citation Envoyé par chamamo Voir le message
    Déjà à première vu tu n'auras rien sur ton formulaire accueil car il est instancié dans ta méthode, à la sortie de la méthode cet objet n'est plus valable, il faut que tu le passes dans la méthode.

    Autre remarque, pas besoin de mettre le "ref" car tu passe un objet de type référence, le ref ou out sont utilisé pour les type valeur (int, string ...)
    Je prends note pour le ref

    Sinon, que voulais-tu dire par "le passer dans la méthode" ? Le mettre dans les paramètres ?
    J'avoue que je ne suis pas sûr de te suivre.
    (J'ai fait le test en passant l'objet dans les paramètres de afficherCommande, aucun changement)

    Si l'objet n'est valable que pour la méthode, en raison de sa portée, je ne vois pas où est le problème étant donné que les seules fois où je l'utilise, c'est dans cette même méthode où je l'ai défini

    (Détail que j'ai omis dans mon premier post : les labels font partie d'un tabControl, dont la page est bien définie comme étant publique)

  5. #5
    Membre éclairé Avatar de chamamo
    Profil pro
    Inscrit en
    Juin 2006
    Messages
    588
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2006
    Messages : 588
    Points : 735
    Points
    735
    Par défaut
    ce serait plus simple si tu postes tout ton projet

  6. #6
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Février 2013
    Messages
    10
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2013
    Messages : 10
    Points : 5
    Points
    5
    Par défaut
    Je te prends au mot :p

    Voici le code complet de la partie concernée :

    Code lié au formulaire (comportant une textbox pour la référence de commande, un bouton pour valider la recherche, et les labels allant être modifiés) :

    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
     
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.Common;
    using System.Data.SqlClient;
     
    namespace ApplicaStore
    {
        public partial class formAccueil : Form
        {
     
            public formAccueil()
            {
                InitializeComponent();
            }
     
            private void btChercherCommande_Click(object sender, EventArgs e)
            {
                string refCmdSaisie = SaisieRefCommande.Text;
     
                // Si le champ n'est pas vide, on vérifie si la référence existe
                if (refCmdSaisie != "")
                {
                    Commande commandeCherchee = new Commande(refCmdSaisie);
     
                    // Si la référence existe, on affiche les détails de la commande
                    commandeCherchee.afficherCommande(refCmdSaisie);
                }
                else
                {
                    lbAlerteCmd.Text = "Le champ est vide.";
                    lbAlerteCmd.Visible = true;
                }
            }
     
        }
    }
    Ensuite, le code du designer ; on voit bien que les labels sont en public (à la fin du code) :

    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
    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
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
     
    namespace ApplicaStore
    {
        partial class formAccueil
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;
     
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
     
            #region Windows Form Designer generated code
     
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.ongletsAccueil = new System.Windows.Forms.TabControl();
                this.pageCommande = new System.Windows.Forms.TabPage();
                this.lbLibPrenomNego = new System.Windows.Forms.Label();
                this.lbValPrenomNego = new System.Windows.Forms.Label();
                this.lbValNomNego = new System.Windows.Forms.Label();
                this.lbLibNomNego = new System.Windows.Forms.Label();
                this.lbValDateLivraison = new System.Windows.Forms.Label();
                this.lbLibDateLivraison = new System.Windows.Forms.Label();
                this.lbValDateCmd = new System.Windows.Forms.Label();
                this.lbLibDateCmd = new System.Windows.Forms.Label();
                this.lbValFournisseur = new System.Windows.Forms.Label();
                this.lbLibFournisseur = new System.Windows.Forms.Label();
                this.lbValClinique = new System.Windows.Forms.Label();
                this.lbLibClinique = new System.Windows.Forms.Label();
                this.lbValService = new System.Windows.Forms.Label();
                this.lbLibService = new System.Windows.Forms.Label();
                this.lbAlerteCmd = new System.Windows.Forms.Label();
                this.btChercherCommande = new System.Windows.Forms.Button();
                this.SaisieRefCommande = new System.Windows.Forms.TextBox();
                this.lbRefCommande = new System.Windows.Forms.Label();
                this.pageProduit = new System.Windows.Forms.TabPage();
                this.pageService = new System.Windows.Forms.TabPage();
                this.pageSite = new System.Windows.Forms.TabPage();
                this.ongletsAccueil.SuspendLayout();
                this.pageCommande.SuspendLayout();
                this.SuspendLayout();
                // 
                // ongletsAccueil
                // 
                this.ongletsAccueil.Controls.Add(this.pageCommande);
                this.ongletsAccueil.Controls.Add(this.pageProduit);
                this.ongletsAccueil.Controls.Add(this.pageService);
                this.ongletsAccueil.Controls.Add(this.pageSite);
                this.ongletsAccueil.Cursor = System.Windows.Forms.Cursors.Default;
                this.ongletsAccueil.Location = new System.Drawing.Point(0, 9);
                this.ongletsAccueil.Margin = new System.Windows.Forms.Padding(0);
                this.ongletsAccueil.Name = "ongletsAccueil";
                this.ongletsAccueil.SelectedIndex = 0;
                this.ongletsAccueil.Size = new System.Drawing.Size(675, 450);
                this.ongletsAccueil.TabIndex = 1;
                // 
                // pageCommande
                // 
                this.pageCommande.BackColor = System.Drawing.SystemColors.Control;
                this.pageCommande.Controls.Add(this.lbLibPrenomNego);
                this.pageCommande.Controls.Add(this.lbValPrenomNego);
                this.pageCommande.Controls.Add(this.lbValNomNego);
                this.pageCommande.Controls.Add(this.lbLibNomNego);
                this.pageCommande.Controls.Add(this.lbValDateLivraison);
                this.pageCommande.Controls.Add(this.lbLibDateLivraison);
                this.pageCommande.Controls.Add(this.lbValDateCmd);
                this.pageCommande.Controls.Add(this.lbLibDateCmd);
                this.pageCommande.Controls.Add(this.lbValFournisseur);
                this.pageCommande.Controls.Add(this.lbLibFournisseur);
                this.pageCommande.Controls.Add(this.lbValClinique);
                this.pageCommande.Controls.Add(this.lbLibClinique);
                this.pageCommande.Controls.Add(this.lbValService);
                this.pageCommande.Controls.Add(this.lbLibService);
                this.pageCommande.Controls.Add(this.lbAlerteCmd);
                this.pageCommande.Controls.Add(this.btChercherCommande);
                this.pageCommande.Controls.Add(this.SaisieRefCommande);
                this.pageCommande.Controls.Add(this.lbRefCommande);
                this.pageCommande.ImeMode = System.Windows.Forms.ImeMode.NoControl;
                this.pageCommande.Location = new System.Drawing.Point(4, 22);
                this.pageCommande.Name = "pageCommande";
                this.pageCommande.Padding = new System.Windows.Forms.Padding(3);
                this.pageCommande.Size = new System.Drawing.Size(667, 424);
                this.pageCommande.TabIndex = 0;
                this.pageCommande.Text = "Consulter une commande";
                // 
                // lbLibPrenomNego
                // 
                this.lbLibPrenomNego.AutoSize = true;
                this.lbLibPrenomNego.Location = new System.Drawing.Point(343, 150);
                this.lbLibPrenomNego.Name = "lbLibPrenomNego";
                this.lbLibPrenomNego.Size = new System.Drawing.Size(123, 13);
                this.lbLibPrenomNego.TabIndex = 18;
                this.lbLibPrenomNego.Text = "Prénom du négociateur :";
                this.lbLibPrenomNego.Visible = false;
                // 
                // lbValPrenomNego
                // 
                this.lbValPrenomNego.AutoSize = true;
                this.lbValPrenomNego.Location = new System.Drawing.Point(472, 150);
                this.lbValPrenomNego.Name = "lbValPrenomNego";
                this.lbValPrenomNego.Size = new System.Drawing.Size(35, 13);
                this.lbValPrenomNego.TabIndex = 17;
                this.lbValPrenomNego.Text = "label2";
                this.lbValPrenomNego.Visible = false;
                // 
                // lbValNomNego
                // 
                this.lbValNomNego.AutoSize = true;
                this.lbValNomNego.Location = new System.Drawing.Point(472, 127);
                this.lbValNomNego.Name = "lbValNomNego";
                this.lbValNomNego.Size = new System.Drawing.Size(35, 13);
                this.lbValNomNego.TabIndex = 15;
                this.lbValNomNego.Text = "label2";
                this.lbValNomNego.Visible = false;
                // 
                // lbLibNomNego
                // 
                this.lbLibNomNego.AutoSize = true;
                this.lbLibNomNego.Location = new System.Drawing.Point(357, 127);
                this.lbLibNomNego.Name = "lbLibNomNego";
                this.lbLibNomNego.Size = new System.Drawing.Size(109, 13);
                this.lbLibNomNego.TabIndex = 14;
                this.lbLibNomNego.Text = "Nom du négociateur :";
                this.lbLibNomNego.Visible = false;
                // 
                // lbValDateLivraison
                // 
                this.lbValDateLivraison.AutoSize = true;
                this.lbValDateLivraison.Location = new System.Drawing.Point(192, 150);
                this.lbValDateLivraison.Name = "lbValDateLivraison";
                this.lbValDateLivraison.Size = new System.Drawing.Size(35, 13);
                this.lbValDateLivraison.TabIndex = 13;
                this.lbValDateLivraison.Text = "label2";
                this.lbValDateLivraison.Visible = false;
                // 
                // lbLibDateLivraison
                // 
                this.lbLibDateLivraison.AutoSize = true;
                this.lbLibDateLivraison.Location = new System.Drawing.Point(94, 150);
                this.lbLibDateLivraison.Name = "lbLibDateLivraison";
                this.lbLibDateLivraison.Size = new System.Drawing.Size(92, 13);
                this.lbLibDateLivraison.TabIndex = 12;
                this.lbLibDateLivraison.Text = "Date de livraison :";
                this.lbLibDateLivraison.Visible = false;
                // 
                // lbValDateCmd
                // 
                this.lbValDateCmd.AutoSize = true;
                this.lbValDateCmd.Location = new System.Drawing.Point(192, 127);
                this.lbValDateCmd.Name = "lbValDateCmd";
                this.lbValDateCmd.Size = new System.Drawing.Size(35, 13);
                this.lbValDateCmd.TabIndex = 11;
                this.lbValDateCmd.Text = "label2";
                this.lbValDateCmd.Visible = false;
                // 
                // lbLibDateCmd
                // 
                this.lbLibDateCmd.AutoSize = true;
                this.lbLibDateCmd.Location = new System.Drawing.Point(80, 127);
                this.lbLibDateCmd.Name = "lbLibDateCmd";
                this.lbLibDateCmd.Size = new System.Drawing.Size(106, 13);
                this.lbLibDateCmd.TabIndex = 10;
                this.lbLibDateCmd.Text = "Date de commande :";
                this.lbLibDateCmd.Visible = false;
                // 
                // lbValFournisseur
                // 
                this.lbValFournisseur.AutoSize = true;
                this.lbValFournisseur.Location = new System.Drawing.Point(192, 104);
                this.lbValFournisseur.Name = "lbValFournisseur";
                this.lbValFournisseur.Size = new System.Drawing.Size(35, 13);
                this.lbValFournisseur.TabIndex = 9;
                this.lbValFournisseur.Text = "label4";
                this.lbValFournisseur.Visible = false;
                // 
                // lbLibFournisseur
                // 
                this.lbLibFournisseur.AutoSize = true;
                this.lbLibFournisseur.Location = new System.Drawing.Point(82, 104);
                this.lbLibFournisseur.Name = "lbLibFournisseur";
                this.lbLibFournisseur.Size = new System.Drawing.Size(104, 13);
                this.lbLibFournisseur.TabIndex = 8;
                this.lbLibFournisseur.Text = "Nom du fournisseur :";
                this.lbLibFournisseur.Visible = false;
                // 
                // lbValClinique
                // 
                this.lbValClinique.AutoSize = true;
                this.lbValClinique.Location = new System.Drawing.Point(326, 73);
                this.lbValClinique.Name = "lbValClinique";
                this.lbValClinique.Size = new System.Drawing.Size(35, 13);
                this.lbValClinique.TabIndex = 7;
                this.lbValClinique.Text = "label4";
                this.lbValClinique.Visible = false;
                // 
                // lbLibClinique
                // 
                this.lbLibClinique.AutoSize = true;
                this.lbLibClinique.Location = new System.Drawing.Point(225, 73);
                this.lbLibClinique.Name = "lbLibClinique";
                this.lbLibClinique.Size = new System.Drawing.Size(95, 13);
                this.lbLibClinique.TabIndex = 6;
                this.lbLibClinique.Text = "Clinique associée :";
                this.lbLibClinique.Visible = false;
                // 
                // lbValService
                // 
                this.lbValService.AutoSize = true;
                this.lbValService.Location = new System.Drawing.Point(472, 104);
                this.lbValService.Name = "lbValService";
                this.lbValService.Size = new System.Drawing.Size(35, 13);
                this.lbValService.TabIndex = 5;
                this.lbValService.Text = "label2";
                this.lbValService.Visible = false;
                // 
                // lbLibService
                // 
                this.lbLibService.AutoSize = true;
                this.lbLibService.Location = new System.Drawing.Point(373, 104);
                this.lbLibService.Name = "lbLibService";
                this.lbLibService.Size = new System.Drawing.Size(93, 13);
                this.lbLibService.TabIndex = 4;
                this.lbLibService.Text = "Service émetteur :";
                this.lbLibService.Visible = false;
                // 
                // lbAlerteCmd
                // 
                this.lbAlerteCmd.AutoSize = true;
                this.lbAlerteCmd.Location = new System.Drawing.Point(271, 47);
                this.lbAlerteCmd.Name = "lbAlerteCmd";
                this.lbAlerteCmd.Size = new System.Drawing.Size(15, 13);
                this.lbAlerteCmd.TabIndex = 3;
                this.lbAlerteCmd.Text = "lb";
                // 
                // btChercherCommande
                // 
                this.btChercherCommande.Location = new System.Drawing.Point(402, 22);
                this.btChercherCommande.Name = "btChercherCommande";
                this.btChercherCommande.Size = new System.Drawing.Size(79, 23);
                this.btChercherCommande.TabIndex = 2;
                this.btChercherCommande.Text = "Rechercher";
                this.btChercherCommande.UseVisualStyleBackColor = true;
                this.btChercherCommande.Click += new System.EventHandler(this.btChercherCommande_Click);
                // 
                // SaisieRefCommande
                // 
                this.SaisieRefCommande.Location = new System.Drawing.Point(265, 24);
                this.SaisieRefCommande.Name = "SaisieRefCommande";
                this.SaisieRefCommande.Size = new System.Drawing.Size(116, 20);
                this.SaisieRefCommande.TabIndex = 1;
                // 
                // lbRefCommande
                // 
                this.lbRefCommande.AutoSize = true;
                this.lbRefCommande.Location = new System.Drawing.Point(114, 27);
                this.lbRefCommande.Name = "lbRefCommande";
                this.lbRefCommande.Size = new System.Drawing.Size(147, 13);
                this.lbRefCommande.TabIndex = 0;
                this.lbRefCommande.Text = "Référence d\'une commande :";
                // 
                // pageProduit
                // 
                this.pageProduit.BackColor = System.Drawing.SystemColors.Control;
                this.pageProduit.Location = new System.Drawing.Point(4, 22);
                this.pageProduit.Name = "pageProduit";
                this.pageProduit.Padding = new System.Windows.Forms.Padding(3);
                this.pageProduit.Size = new System.Drawing.Size(667, 424);
                this.pageProduit.TabIndex = 1;
                this.pageProduit.Text = "Ajout d\'un produit";
                // 
                // pageService
                // 
                this.pageService.BackColor = System.Drawing.SystemColors.Control;
                this.pageService.Location = new System.Drawing.Point(4, 22);
                this.pageService.Name = "pageService";
                this.pageService.Size = new System.Drawing.Size(667, 424);
                this.pageService.TabIndex = 2;
                this.pageService.Text = "Ajout d\'un service";
                // 
                // pageSite
                // 
                this.pageSite.BackColor = System.Drawing.SystemColors.Control;
                this.pageSite.Location = new System.Drawing.Point(4, 22);
                this.pageSite.Name = "pageSite";
                this.pageSite.Size = new System.Drawing.Size(667, 424);
                this.pageSite.TabIndex = 3;
                this.pageSite.Text = "Ajout d\'un site";
                // 
                // formAccueil
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(674, 446);
                this.Controls.Add(this.ongletsAccueil);
                this.Name = "formAccueil";
                this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
                this.Text = "ApplicaStore";
                this.ongletsAccueil.ResumeLayout(false);
                this.pageCommande.ResumeLayout(false);
                this.pageCommande.PerformLayout();
                this.ResumeLayout(false);
     
            }
     
            #endregion
     
            private System.Windows.Forms.Button btChercherCommande;
            private System.Windows.Forms.TextBox SaisieRefCommande;
            private System.Windows.Forms.Label lbRefCommande;
            public System.Windows.Forms.TabPage pageCommande;
            public System.Windows.Forms.Label lbValService;
            public System.Windows.Forms.Label lbValClinique;
            public System.Windows.Forms.Label lbLibClinique;
            public System.Windows.Forms.Label lbLibService;
            public System.Windows.Forms.Label lbValFournisseur;
            public System.Windows.Forms.Label lbLibFournisseur;
            public System.Windows.Forms.Label lbValDateCmd;
            public System.Windows.Forms.Label lbLibDateCmd;
            public System.Windows.Forms.Label lbValDateLivraison;
            public System.Windows.Forms.Label lbLibDateLivraison;
            public System.Windows.Forms.Label lbValPrenomNego;
            public System.Windows.Forms.Label lbValNomNego;
            public System.Windows.Forms.Label lbLibNomNego;
            public System.Windows.Forms.Label lbLibPrenomNego;
            public System.Windows.Forms.TabControl ongletsAccueil;
            public System.Windows.Forms.Label lbAlerteCmd;
            public System.Windows.Forms.TabPage pageProduit;
            public System.Windows.Forms.TabPage pageService;
            public System.Windows.Forms.TabPage pageSite;
     
        }
    }
    Et enfin, le code de la classe Commande :

    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
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
     
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.Common;
    using System.Data.SqlClient;
     
    namespace ApplicaStore
    {
        class Commande
        {
            DbCommand maCommand;
            ConnexionSingleton maConnexion;
     
            private string refCommande;
            private int codeServiceCmd;
            private string refCliniqueCmd;
            private string fournisseurCmd;
            private string nomNegociateurCmd;
            private string prenomNegociateurCmd;
            private string dateCommande;
            private string dateLivraisonCmd;
            private string dateGenerationFichierCmd;
     
            public Commande(string uneRefCommande)
            {
                this.refCommande = uneRefCommande;
            }
     
            // ENSEMBLE DES ACCESSEURS ET DES MUTATEURS
            // Je te les épargne... 
            // FIN DES ACCESSEURS ET DES MUTATEURS
     
     
            // Cette fonction est utilisée pour faire apparaître les informations principales d'une commande
     
            public void labelCmd(Label labelTxt, string message, Label labelLib)
            {
                labelTxt.Text = message;
                labelTxt.Visible = true;
                labelLib.Visible = true;
            }
     
            // Cette fonction vérifie si la commande recherchée existe
            // Si oui, on affiche ses détails
            // Si non, on affiche un message d'erreur
     
            public void afficherCommande(string refCommandeForm)
            {
                formAccueil accueil = new formAccueil();
     
                maConnexion = ConnexionSingleton.GetLinstance();
                maConnexion.Ouvrir();
                maCommand = maConnexion.GetConnexion().CreateCommand();
     
                string reqInfosCommande = "SELECT Se.libelleService, Si.nomClinique, C.fournisseurCommande, "
                + "C.dateCommande, C.dateLivraison, C.nomNegociateur, C.prenomNegociateur "
                + "FROM commande C, site Si, service Se "
                + "WHERE C.refClinique = Si.refClinique "
                + "AND C.codeService = Se.codeService "
                + "AND C.refCommande = '" + refCommandeForm + "' ";
                maCommand.CommandText = reqInfosCommande;
     
                DbDataReader data = maCommand.ExecuteReader();
     
                // Si la requête a un résultat --> que la référence existe, on affiche les infos de la commande
                if (data.HasRows)
                {
                    accueil.lbAlerteCmd.Visible = false;
     
                    while (data.Read())
                    {
                        labelCmd(accueil.lbValClinique, data["nomClinique"].ToString(), accueil.lbLibClinique);
                        labelCmd(accueil.lbValService, data["libelleService"].ToString(), accueil.lbLibService);
                        labelCmd(accueil.lbValFournisseur, data["fournisseurCommande"].ToString(), accueil.lbLibFournisseur);
                        labelCmd(accueil.lbValDateCmd, data["dateCommande"].ToString(), accueil.lbLibDateCmd);
                        labelCmd(accueil.lbValDateLivraison, data["dateLivraison"].ToString(), accueil.lbLibDateLivraison);
                        labelCmd(accueil.lbValNomNego, data["nomNegociateur"].ToString(), accueil.lbLibNomNego);
                        labelCmd(accueil.lbValPrenomNego, data["prenomNegociateur"].ToString(), accueil.lbLibPrenomNego);
                    }
                }
                // Sinon on signale à l'utilisateur que la commande n'est pas enregistrée dans la base de données
                else
                {
                    accueil.lbAlerteCmd.Text = "Commande inconnue.";
                    accueil.lbAlerteCmd.Visible = true;
                }
     
                maConnexion.Fermer();
            }
        }
    }
    Je n'ai pas mis le code de la connexion, y'a aucun soucis de ce côté.

    Merci pour ton aide !

    Owl'

  7. #7
    Membre éclairé Avatar de chamamo
    Profil pro
    Inscrit en
    Juin 2006
    Messages
    588
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2006
    Messages : 588
    Points : 735
    Points
    735
    Par défaut
    Je redis ce que j'avais dis, dans ta méthode afficerCommande tu fais

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    formAccueil accueil = new formAccueil();
    la tu ne vas pas manipuler le formulaire que tu vois à l'écran, il faudrait que tu récupère l'objet afficher, tu le verras dans ton Main, tu as quoi dans ton Main?

  8. #8
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Février 2013
    Messages
    10
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2013
    Messages : 10
    Points : 5
    Points
    5
    Par défaut
    Avant que je n'y touche (soit y'a deux minutes) pour faire des tests, le main était comme ceci :

    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
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;
     
    namespace ApplicaStore
    {
        static class Program
        {
            /// <summary>
            /// Point d'entrée principal de l'application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new formConnexion());
            }
        }
    }
    Autant dire que je n'y ai pas touché quoi.

    J'ai enfin (je l'espère) compris pourquoi l'utilisation de l'objet directement dans la méthode n'aurait pas marché; désolé d'avoir été long à la détente.

    Concrètement, ce que tu m'incites à faire, c'est de déclarer un objet formAccueil qui sera passé en paramètre de la fonction afficherCommande ?

    Instancier l'objet dans la classe partielle formAccueil n'y change rien (voir ci-dessous) ; je suppose qu'il faut donc le faire dans le Main, étant donné que rien n'est "plus haut placé" ?

    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
    namespace ApplicaStore
    {
        public partial class formAccueil : Form
        {
            public formAccueil()
            {
                InitializeComponent();
            }
    
            private void btChercherCommande_Click(object sender, EventArgs e)
            {
                string refCmdSaisie = SaisieRefCommande.Text;
    
                // Si le champ n'est pas vide, on vérifie si la référence existe
                if (refCmdSaisie != "")
                {
                    Commande commandeCherchee = new Commande(refCmdSaisie);
                    formAccueil accueil = new formAccueil();
    
                    // Si la référence existe, on affiche les détails de la commande
                    commandeCherchee.afficherCommande(refCmdSaisie, accueil);
                }
                else
                {
                    lbAlerteCmd.Text = "Le champ est vide.";
                    lbAlerteCmd.Visible = true;
                }
            }
    
        }
    }
    Encore désolé de mes lacunes, et merci à toi de passer du temps à m'aider !

  9. #9
    Membre éclairé Avatar de chamamo
    Profil pro
    Inscrit en
    Juin 2006
    Messages
    588
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2006
    Messages : 588
    Points : 735
    Points
    735
    Par défaut
    Pour récuperer l'objet courant (pas la classe), dans ton cas c'est l'instance de la classe formAccueil, tu utilises le mot clé this

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    private void btChercherCommande_Click(object sender, EventArgs e)
            {
                    .......
                    commandeCherchee.afficherCommande(refCmdSaisie, this);
     
            }
    Du coup dans ta méthode afficherCommande, tu vas pouvoir manipuler l'objet formAccueil.

    Ca va marcher mais ça reste crad, essaie de charger les données dans un DataTable et faire du binding des composants de ton formulaire.

  10. #10
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Février 2013
    Messages
    10
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2013
    Messages : 10
    Points : 5
    Points
    5
    Par défaut
    Ok !
    Je comprends enfin pourquoi créer un objet était inefficace... malheureusement tard, mais mieux vaut tard que jamais...

    Effectivement, ça fonctionne; c'est peut-être un peu moche comme tu le dis, mais je vais d'abord fonctionner sur cette base et rattraper le 'temps perdu' par cette erreur, et ensuite j'uniformiserai le projet en mettant des bind.

    Encore merci pour l'explication et de m'avoir débloqué !

    Je met en résolu.

    Owl'

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Acces à un control via une class perso
    Par parabole33 dans le forum C#
    Réponses: 15
    Dernier message: 27/07/2009, 11h03
  2. Un tableau via une classe
    Par MaitrePylos dans le forum Ruby on Rails
    Réponses: 4
    Dernier message: 02/04/2007, 09h49
  3. Réponses: 9
    Dernier message: 25/02/2007, 13h46
  4. Mise à jour d'une controle via une classe.
    Par Andry dans le forum Delphi
    Réponses: 4
    Dernier message: 23/06/2006, 15h00
  5. [C#] Accéder à un control depuis une class
    Par choas dans le forum Windows Forms
    Réponses: 4
    Dernier message: 02/05/2006, 13h43

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo