Créer des CheckBox dans le code

Résolu/Fermé
Dead5her Messages postés 15 Date d'inscription mardi 18 novembre 2014 Statut Membre Dernière intervention 8 décembre 2014 - 1 déc. 2014 à 11:37
Whismeril Messages postés 19026 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 20 avril 2024 - 1 déc. 2014 à 17:43
Bonjour,
Je souhaiterais créer des Checkbox dans une boucle "FOR" et les stockes dans un tableau de Checkbox.
Je ne parviens pas à créer les checkbox dans le code..
Comment faire ?

1 réponse

Whismeril Messages postés 19026 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 20 avril 2024 931
1 déc. 2014 à 12:26
Bonjour,

ce que tu ne dis pas c'est si tu veux afficher ces checkbox sur un formulaire.
Je vais supposer que oui.

Commençons par regarder ce que fais Visual Studio quand tu en poses un sur le formulaire dans le mode design.


Double cliquons sur la form et allons chercher la définition de InitializeCompoment


Voici le code généré automatiquement:
        private void InitializeComponent()
        {
            this.checkBox1 = new System.Windows.Forms.CheckBox();
            this.SuspendLayout();
            // 
            // checkBox1
            // 
            this.checkBox1.AutoSize = true;
            this.checkBox1.Location = new System.Drawing.Point(39, 51);
            this.checkBox1.Name = "checkBox1";
            this.checkBox1.Size = new System.Drawing.Size(80, 17);
            this.checkBox1.TabIndex = 0;
            this.checkBox1.Text = "checkBox1";
            this.checkBox1.UseVisualStyleBackColor = true;
            // 
            // Form5
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Controls.Add(this.checkBox1);
            this.Name = "Form5";
            this.Text = "Form5";
            this.Load += new System.EventHandler(this.Form5_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.CheckBox checkBox1;



Que voit on?
D'abord on crée un chekbox (ligne 32), puis dans initializeCompoment, on l'initialise (ligne 3), on lui donne les paramètres nécessaires (lignes 8 à 14) et enfin on le place sur le formulaire (ligne 21).

Dans ta boucle tu auras tout ça à faire:
            List<CheckBox> mesCheckBox = new List<CheckBox>();
            for (int i = 0; i < 3; i++)
            {
                CheckBox chk = new CheckBox();

                chk.AutoSize = true;
                chk.Location = new System.Drawing.Point(20, 40 + i * 20);
                chk.Name = string.Format("checkBox{0}",i+1);
                chk.Size = new System.Drawing.Size(80, 17);
                chk.Text = chk.Name;
                chk.UseVisualStyleBackColor = true;

                this.Controls.Add(chk);
                mesCheckBox.Add(chk);

            }

1
Dead5her Messages postés 15 Date d'inscription mardi 18 novembre 2014 Statut Membre Dernière intervention 8 décembre 2014 7
1 déc. 2014 à 15:54
Merci pour ta réponse très complète. C'est exactement ce que je souhaitais faire.
Je vais tester. Au fond c'était pas faux ce que je faisais. J'oubliais simplement de l'ajouter au FORM.
Merci encore
0
Whismeril Messages postés 19026 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 20 avril 2024 931
1 déc. 2014 à 17:43
J'oubliais simplement de l'ajouter au FORM.

Je m'en doutais.
La prochaine fois, poste ton code en utilisant la coloration syntaxique, ça sera plus simple de répondre au plus juste.
0