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 :

[C# / dotNet 2.0] Accès à des Winforms (sans doute trivial mais…)


Sujet :

Windows Forms

  1. #1
    Futur Membre du Club
    Inscrit en
    Novembre 2006
    Messages
    9
    Détails du profil
    Informations forums :
    Inscription : Novembre 2006
    Messages : 9
    Points : 6
    Points
    6
    Par défaut [C# / dotNet 2.0] Accès à des Winforms (sans doute trivial mais…)
    Ce post va sans doute poser une question ridicule mais je suis un débutant en objet et en .net donc je supplie l'assistance de bien vouloir être indulgente :o

    Je tente de faire une appli tout con avec une Fenêtre (Form1) et une classe abstract (Feed) donc héritent 3 classes: MusicFeed, TalkFeed et VideoFeed.

    Depuis une méthode de MusicFeed (et des autres par la suite) je voudrais modifier le contenu Text de certains controles, en l'espèce, des System.Windows.Forms.TextBox et des System.Windows.Forms.ComboBox.
    Problème: par défaut ces contrôles étaient des membres private de Form1 j'ai donc modifié la propriété Modifiers en public (internal devrait suffire mais bon).

    Dans le Form1.Designer.cs, on trouve désormais:

    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
     
     
     
    namespace Podcast1
    {
        partial class Form1
        {
            /// <summary>
            /// Variable nécessaire au concepteur.
            /// </summary>
            private System.ComponentModel.IContainer components = null;
     
            /// <summary>
            /// Nettoyage des ressources utilisées.
            /// </summary>
            /// <param name="disposing">true si les ressources managées doivent être supprimées ; sinon, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
     
            #region Code généré par le Concepteur Windows Form
     
            /// <summary>
            /// Méthode requise pour la prise en charge du concepteur - ne modifiez pas
            /// le contenu de cette méthode avec l'éditeur de code.
            /// </summary>
            private void InitializeComponent()
            {
                this.listeFlux = new System.Windows.Forms.ListBox();
                this.inputUrl = new System.Windows.Forms.TextBox();
                this.btnAdd = new System.Windows.Forms.Button();
                this.btnDel = new System.Windows.Forms.Button();
                this.btnSave = new System.Windows.Forms.Button();
                this.grpList = new System.Windows.Forms.GroupBox();
                this.grpPodcast = new System.Windows.Forms.GroupBox();
                this.inputGenre = new System.Windows.Forms.ComboBox();
                this.label4 = new System.Windows.Forms.Label();
                this.inputType = new System.Windows.Forms.ComboBox();
                this.label3 = new System.Windows.Forms.Label();
                this.label2 = new System.Windows.Forms.Label();
                this.label1 = new System.Windows.Forms.Label();
                this.inputName = new System.Windows.Forms.TextBox();
                this.grpList.SuspendLayout();
                this.grpPodcast.SuspendLayout();
                this.SuspendLayout();
                // 
                // listeFlux
                // 
                this.listeFlux.FormattingEnabled = true;
                this.listeFlux.Location = new System.Drawing.Point(7, 20);
                this.listeFlux.Name = "listeFlux";
                this.listeFlux.Size = new System.Drawing.Size(598, 264);
                this.listeFlux.Sorted = true;
                this.listeFlux.TabIndex = 0;
                this.listeFlux.SelectedValueChanged += new System.EventHandler(this.listeFlux_SelectedValueChanged);
                // 
                // inputUrl
                // 
                this.inputUrl.Location = new System.Drawing.Point(42, 20);
                this.inputUrl.Name = "inputUrl";
                this.inputUrl.Size = new System.Drawing.Size(567, 21);
                this.inputUrl.TabIndex = 1;
                // 
                // btnAdd
                // 
                this.btnAdd.Location = new System.Drawing.Point(615, 19);
                this.btnAdd.Name = "btnAdd";
                this.btnAdd.Size = new System.Drawing.Size(59, 21);
                this.btnAdd.TabIndex = 2;
                this.btnAdd.Text = "&Add";
                this.btnAdd.UseVisualStyleBackColor = true;
                this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
                // 
                // btnDel
                // 
                this.btnDel.Location = new System.Drawing.Point(615, 46);
                this.btnDel.Name = "btnDel";
                this.btnDel.Size = new System.Drawing.Size(59, 21);
                this.btnDel.TabIndex = 3;
                this.btnDel.Text = "&Delete";
                this.btnDel.UseVisualStyleBackColor = true;
                this.btnDel.Click += new System.EventHandler(this.btnDel_Click);
                // 
                // btnSave
                // 
                this.btnSave.Location = new System.Drawing.Point(611, 20);
                this.btnSave.Name = "btnSave";
                this.btnSave.Size = new System.Drawing.Size(59, 21);
                this.btnSave.TabIndex = 4;
                this.btnSave.Text = "&Save";
                this.btnSave.UseVisualStyleBackColor = true;
                this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
                // 
                // grpList
                // 
                this.grpList.Controls.Add(this.btnSave);
                this.grpList.Controls.Add(this.listeFlux);
                this.grpList.Location = new System.Drawing.Point(5, 5);
                this.grpList.Name = "grpList";
                this.grpList.Size = new System.Drawing.Size(677, 288);
                this.grpList.TabIndex = 5;
                this.grpList.TabStop = false;
                this.grpList.Text = "List";
                // 
                // grpPodcast
                // 
                this.grpPodcast.Controls.Add(this.inputGenre);
                this.grpPodcast.Controls.Add(this.label4);
                this.grpPodcast.Controls.Add(this.inputType);
                this.grpPodcast.Controls.Add(this.label3);
                this.grpPodcast.Controls.Add(this.label2);
                this.grpPodcast.Controls.Add(this.label1);
                this.grpPodcast.Controls.Add(this.inputName);
                this.grpPodcast.Controls.Add(this.inputUrl);
                this.grpPodcast.Controls.Add(this.btnDel);
                this.grpPodcast.Controls.Add(this.btnAdd);
                this.grpPodcast.Location = new System.Drawing.Point(5, 299);
                this.grpPodcast.Name = "grpPodcast";
                this.grpPodcast.Size = new System.Drawing.Size(676, 113);
                this.grpPodcast.TabIndex = 6;
                this.grpPodcast.TabStop = false;
                this.grpPodcast.Text = "Podcast";
                // 
                // inputGenre
                // 
                this.inputGenre.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
                this.inputGenre.FormattingEnabled = true;
                this.inputGenre.Items.AddRange(new object[] {
                "(N/A)",
                "Bues",
                "Country",
                "Disco",
                "Dance",
                "Funk",
                "Hip Hop",
                "Jazz",
                "Pop",
                "R&B",
                "Reggae",
                "Rock",
                "Techno"});
                this.inputGenre.Location = new System.Drawing.Point(53, 74);
                this.inputGenre.MaxDropDownItems = 13;
                this.inputGenre.Name = "inputGenre";
                this.inputGenre.Size = new System.Drawing.Size(96, 21);
                this.inputGenre.TabIndex = 9;
                // 
                // label4
                // 
                this.label4.AutoSize = true;
                this.label4.Location = new System.Drawing.Point(7, 77);
                this.label4.Name = "label4";
                this.label4.Size = new System.Drawing.Size(40, 13);
                this.label4.TabIndex = 8;
                this.label4.Text = "Genre:";
                // 
                // inputType
                // 
                this.inputType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
                this.inputType.FormattingEnabled = true;
                this.inputType.Items.AddRange(new object[] {
                "Music",
                "Talk",
                "Video"});
                this.inputType.Location = new System.Drawing.Point(510, 47);
                this.inputType.MaxDropDownItems = 3;
                this.inputType.Name = "inputType";
                this.inputType.Size = new System.Drawing.Size(99, 21);
                this.inputType.TabIndex = 7;
                this.inputType.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.inputType_TextChanged);
                this.inputType.TextChanged += new System.EventHandler(this.inputType_TextChanged);
                // 
                // label3
                // 
                this.label3.AutoSize = true;
                this.label3.Location = new System.Drawing.Point(469, 50);
                this.label3.Name = "label3";
                this.label3.Size = new System.Drawing.Size(35, 13);
                this.label3.TabIndex = 6;
                this.label3.Text = "Type:";
                // 
                // label2
                // 
                this.label2.AutoSize = true;
                this.label2.Location = new System.Drawing.Point(7, 50);
                this.label2.Name = "label2";
                this.label2.Size = new System.Drawing.Size(38, 13);
                this.label2.TabIndex = 5;
                this.label2.Text = "Name:";
                // 
                // label1
                // 
                this.label1.AutoSize = true;
                this.label1.Location = new System.Drawing.Point(6, 24);
                this.label1.Name = "label1";
                this.label1.Size = new System.Drawing.Size(30, 13);
                this.label1.TabIndex = 4;
                this.label1.Text = "URL:";
                // 
                // inputName
                // 
                this.inputName.Location = new System.Drawing.Point(50, 47);
                this.inputName.Name = "inputName";
                this.inputName.Size = new System.Drawing.Size(413, 21);
                this.inputName.TabIndex = 1;
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.AutoSize = true;
                this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
                this.ClientSize = new System.Drawing.Size(685, 418);
                this.Controls.Add(this.grpPodcast);
                this.Controls.Add(this.grpList);
                this.MaximizeBox = false;
                this.Name = "Form1";
                this.Text = "Podcast Project";
                this.Load += new System.EventHandler(this.Form1_Load);
                this.grpList.ResumeLayout(false);
                this.grpPodcast.ResumeLayout(false);
                this.grpPodcast.PerformLayout();
                this.ResumeLayout(false);
     
            }
     
            #endregion
     
            private System.Windows.Forms.ListBox listeFlux;
            private System.Windows.Forms.Button btnAdd;
            private System.Windows.Forms.Button btnDel;
            private System.Windows.Forms.Button btnSave;
            private System.Windows.Forms.GroupBox grpList;
            private System.Windows.Forms.GroupBox grpPodcast;
            private System.Windows.Forms.Label label2;
            private System.Windows.Forms.Label label1;
            private System.Windows.Forms.Label label3;
            private System.Windows.Forms.Label label4;
            public System.Windows.Forms.TextBox inputUrl;
            public System.Windows.Forms.ComboBox inputType;
            public System.Windows.Forms.TextBox inputName;
            public System.Windows.Forms.ComboBox inputGenre;
        }
    }
    Les quatre dernières lignes sont a priori OK.

    Dans Form1.cs, j'ai tapé:


    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
     
     
     
        /// <summary>
        ///  Basic class for any kind of feed
        /// </summary>
        public abstract class Feed
        {
            protected string url;
            protected string name;
     
            public string Name
            {
                get
                {
                    return name;
                }
     
                set
                {
                    name = value;
                }
            }
            public string Url
            {
                get
                {
                    return url;
                }
     
                set
                {
                    url = value;
                }
            }
            public abstract void Load();
        }
     
        public class MusicFeed : Feed
        {
            string genre;
     
            public string Genre
            {
                get
                {
                    return genre;
                }
     
                set
                {
                    genre = value;
                }
            }
     
            public MusicFeed(string inName, string inUrl, string inGenre)
            {
                name = inName;
                url = inUrl;
                genre = inGenre;
            }
     
            public override void Load()
            {
                Form1.inputName.Text = name;
            }
     
        }
    Sauf que non, il semble que VS ne voit pas de memebre inputName à Form1: Intellisense ne le propose pas et le compilateur génère une erreur "Erreur 2 Une référence d'objet est requise pour la propriété, la méthode ou le champ non statique 'Podcast1.Form1.inputName' " (Podcast1 est le namespace qui contient tout).

    Quelqu'un ici aurait une piste ?

  2. #2
    Nouveau membre du Club
    Inscrit en
    Novembre 2006
    Messages
    22
    Détails du profil
    Informations forums :
    Inscription : Novembre 2006
    Messages : 22
    Points : 26
    Points
    26
    Par défaut
    Je sais pas si c'est une erreur de frappe mais tu as écris
    Form1.inputName.Text = name;

    Or pour moi, Form1 avec un F majuscule représente le nom de la classe.
    Puisque la variable inputName n'est pas "static" ce n'est pas une variable de classe. Tu dois assigner cette variable via l'object crée à partir de la classe Form1.

  3. #3
    Futur Membre du Club
    Inscrit en
    Novembre 2006
    Messages
    9
    Détails du profil
    Informations forums :
    Inscription : Novembre 2006
    Messages : 9
    Points : 6
    Points
    6
    Par défaut
    Ah mon Dieu mais c'est une excellente remarque ça.
    Sauf que dans program.cs j'ai "Application.Run(new Form1());" (c'est généré automatiquement par VS2005 ce bordel)
    Comment je retrouve mon objet dans ce cas ? :-/

  4. #4
    Nouveau membre du Club
    Inscrit en
    Novembre 2006
    Messages
    22
    Détails du profil
    Informations forums :
    Inscription : Novembre 2006
    Messages : 22
    Points : 26
    Points
    26
    Par défaut
    C'est simple tu modifie le fichier comme cela:

    Form1 myForm=new Form1();
    Application.Run(myForm);

    et hop ! tu utilises la variable myForm.

  5. #5
    Futur Membre du Club
    Inscrit en
    Novembre 2006
    Messages
    9
    Détails du profil
    Informations forums :
    Inscription : Novembre 2006
    Messages : 9
    Points : 6
    Points
    6
    Par défaut
    Trouvé apparement: http://www.ai.uga.edu/mc/CSharpNotes.html#APPRUN

    Je tente ça, merci, je suis un peu con sur ce coup, pourtant j'avais réfléchi...

  6. #6
    Nouveau membre du Club
    Inscrit en
    Novembre 2006
    Messages
    22
    Détails du profil
    Informations forums :
    Inscription : Novembre 2006
    Messages : 22
    Points : 26
    Points
    26
    Par défaut
    Une petite précision car j'ai répondu un peu vite.
    Pour que la variable myForm soit accessible à l'exterieur de la classe Program tu dois la déclarer dans la classe et non pas dans la fonction Main()

    ex
    static class Program
    {
    public static Form1 myForm;

    [STAThread]
    static void Main()
    {
    myForm = new Form1();
    Application.Run(myForm);
    }
    }

    tu pourras ensuite acceder à la variable par Program.myForm

  7. #7
    Futur Membre du Club
    Inscrit en
    Novembre 2006
    Messages
    9
    Détails du profil
    Informations forums :
    Inscription : Novembre 2006
    Messages : 9
    Points : 6
    Points
    6
    Par défaut
    Ça a l'air de marcher, merci beaucoup

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

Discussions similaires

  1. besoin d'aide pour un debugage (sans doute trivial)
    Par zaboug dans le forum Langage
    Réponses: 1
    Dernier message: 07/07/2009, 15h35
  2. Recherche de winform avec accés à des données.
    Par jolzeviking dans le forum Windows Forms
    Réponses: 1
    Dernier message: 23/12/2008, 23h05
  3. Réponses: 2
    Dernier message: 01/07/2007, 12h50
  4. Temps d'accès à des données dans un fichier
    Par TONIAPEL dans le forum Assembleur
    Réponses: 5
    Dernier message: 28/09/2003, 15h21
  5. [PostgresSQL]Pb accés des utilisateurs sans mot de passe
    Par woodwai dans le forum PostgreSQL
    Réponses: 2
    Dernier message: 22/05/2003, 16h06

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