Classe , controls et formulaire. Comment faire ?

Résolu
Woslike Messages postés 46 Date d'inscription mercredi 25 juillet 2012 Statut Membre Dernière intervention 12 septembre 2013 - 16 janv. 2013 à 11:27
Whismeril Messages postés 19026 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 20 avril 2024 - 16 janv. 2013 à 13:13
Bonjour tout le monde, je viens vers vous pour une question qui m'aiderai ENORMEMENT.

Je vous explique, j'aimerai créer une classe qui me permettrai de mettre en place automatiquement 2 boutons sur le formulaire.

Exemple :

J'ai créer un fomulaire, et j'aimerai que lorsque j'initialiser une variable de type 'MettreBoutton", deux bouttons 'Retour' et 'Valider' ce mettent automatiquement au lancement du programme.

Alors voici mon code que j'ai essayé, mais qui ne fonctionne pas :

--- Formulaire : Form1 ---

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;


namespace NewMarche2013
{
/// <summary>
/// Description résumée de Form1.
/// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        MettreBoutton  mesBouttons = new MettreBoutton();

        public FormPasswd()
        {
    InitializeComponent();			
        }
    }
}


--- Classe : MettreBoutton ---


using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace NewMarche2013
{
    class MettreBoutton 
    {
        private Button btnAnnuler = new Button();
        private Button btnValider = new Button();

        private void InitialiseComponent()
        {
            // 
            // btnAnnuler
            // 
            this.btnAnnuler.BackColor = System.Drawing.Color.Red;
            this.btnAnnuler.ForeColor = System.Drawing.Color.White;
            this.btnAnnuler.Location = new System.Drawing.Point(3, 134);
            this.btnAnnuler.Name = "btnAnnuler";
            this.btnAnnuler.Size = new System.Drawing.Size(103, 35);
            this.btnAnnuler.TabIndex = 3;
            this.btnAnnuler.Text = "Annuler";
            this.btnAnnuler.Click += new System.EventHandler(this.btnAnnul_Click);
            // 
            // btnValider
            // 
            this.btnValider.BackColor = System.Drawing.Color.Lime;
            this.btnValider.Location = new System.Drawing.Point(133, 134);
            this.btnValider.Name = "btnValider";
            this.btnValider.Size = new System.Drawing.Size(103, 35);
            this.btnValider.TabIndex = 2;
            this.btnValider.Text = "Valider";
            this.btnValider.Click += new System.EventHandler(this.btnValid_Click);
        }

        public MettreBoutton ()
        {
            InitialiseComponent();
        }

        private void btnAnnul_Click(object sender, System.EventArgs e)
        {
           //Code d'annuler
        }

         private void btnValid_Click(object sender, System.EventArgs e)
        {
           //Code de la validation
        }
     }
}

5 réponses

Woslike Messages postés 46 Date d'inscription mercredi 25 juillet 2012 Statut Membre Dernière intervention 12 septembre 2013 1
16 janv. 2013 à 12:42
Problème résolu.
En fouillant un peu, j'ai trouvé la solution.
Pour voir apparaître les deux boutons, il faut affecter le parent sur chaque control de la classe.

Voici le programme modifié et FONCTIONNEL


--- Formulaire : Form1 ---

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;


namespace NewMarche2013
{
/// <summary>
/// Description résumée de Form1.
/// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        MettreBoutton  mesBouttons

        public FormPasswd()
        {
    InitializeComponent();
    mesBouttons  = new MettreBoutton(this);	
        }
    }
}



--- Classe : MettreBoutton ---

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace Boutton
{
    class MettreBoutton 
    {
        private Button btnAnnuler = new Button();
        private Button btnValider = new Button();

        private void InitialiseComponent(Control Parent)
        {
            // 
            // btnAnnuler
            // 
            this.btnAnnuler.BackColor = System.Drawing.Color.Red;
            this.btnAnnuler.ForeColor = System.Drawing.Color.White;
            this.btnAnnuler.Location = new System.Drawing.Point(3, 134);
            this.btnAnnuler.Name = "btnAnnuler";
            this.btnAnnuler.Size = new System.Drawing.Size(103, 35);
            this.btnAnnuler.TabIndex = 3;
            this.btnAnnuler.Text = "Annuler";
            this.btnAnnuler.Click += new System.EventHandler(this.btnAnnul_Click);
            this.btnAnnuler.Parent = Parent;
            // 
            // btnValider
            // 
            this.btnValider.BackColor = System.Drawing.Color.Lime;
            this.btnValider.Location = new System.Drawing.Point(133, 134);
            this.btnValider.Name = "btnValider";
            this.btnValider.Size = new System.Drawing.Size(103, 35);
            this.btnValider.TabIndex = 2;
            this.btnValider.Text = "Valider";
            this.btnValider.Click += new System.EventHandler(this.btnValid_Click);
            this.btnValider.Parent = Parent;
        }

        public MettreBoutton (Control Parent)
        {
            InitialiseComponent(Parent);
        }

        private void btnAnnul_Click(object sender, System.EventArgs e)
        {
           //Code d'annuler
        }

         private void btnValid_Click(object sender, System.EventArgs e)
        {
           //Code de la validation
        }
     }
    
}
3
Woslike Messages postés 46 Date d'inscription mercredi 25 juillet 2012 Statut Membre Dernière intervention 12 septembre 2013 1
16 janv. 2013 à 11:29
Désolé du double poste...

En écrivant ce code, aucun bouton n’apparaît. Ni aucune erreur. Comment faire ?
Je vous remercie d'avance pour vos réponse !
0
salut,
est-ce que tu as supprimé le "NewMarche2013.designer?" si ok alors
        private System.ComponentModel.IContainer components = null;


public NewMarche2013()
{
//
// Requis pour la prise en charge du Concepteur Windows Forms
//
InitializeComponent();

//
// TODO : ajoutez le code du constructeur après l'appel à InitializeComponent
//
}


                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>


car je ne pense pas que ca doit se retrouver dans votre NewMarche2013.cs
0
d'acord
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
Whismeril Messages postés 19026 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 20 avril 2024 656
16 janv. 2013 à 13:13
Bonjour, si ton probléme est résolu, merci de cliquer sur le bouton "Réponse Acceptée". Le post apparaitra résolu dans la vur du forum


Whismeril
0
Rejoignez-nous