MdiChild et toolBox

ibr4u Messages postés 4 Date d'inscription mardi 16 septembre 2003 Statut Membre Dernière intervention 5 mai 2004 - 4 mai 2004 à 21:21
ibr4u Messages postés 4 Date d'inscription mardi 16 septembre 2003 Statut Membre Dernière intervention 5 mai 2004 - 5 mai 2004 à 12:12
Bonjour, pour faire vite :
J'ai une toolBar dans une Form Parent (formParent).
J'ai deux Form filles rattachées à formParent (formFille1 et formFille2) et qui ne sont pas de le meme classe mais qui possede les deux la meme méthode publique (saveFille).
Dans ma toolBar j'ai un boutton Save, cette toolbar étant rataché à formParent le gestionnaire d'évenement se trouve dans formParent.

Je voudrai executer quand je clique sur le boutton Save recuperer la Form Fille active et executer son saveFille.

Voici le code en simplifié où j'essaye de passer par l'intermédiaire d'un interface mais sans succés.

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

namespace testHeritage
{

public class formParent : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnSave;
private System.ComponentModel.Container components = null;

formFille1 myFormFille1;
formFille2 myFormFille2;

public formParent()
{
InitializeComponent();

this.IsMdiContainer = true;
//fille1
myFormFille1 = new formFille1();
myFormFille1.MdiParent = this;
myFormFille1.Show();
//fille2
myFormFille2 = new formFille2();
myFormFille2.MdiParent = this;

}

private void InitializeComponent()
{
this.btnSave = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// btnSave
//
this.btnSave.Location = new System.Drawing.Point(272, 8);
this.btnSave.Name = "btnSave";
this.btnSave.Size = new System.Drawing.Size(64, 24);
this.btnSave.TabIndex = 0;
this.btnSave.Text = "Save";
this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
//
// formParent
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(608, 485);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.btnSave});
this.IsMdiContainer = true;
this.Text = "formParent";
this.ResumeLayout(false);

}

[STAThread]
static void Main()
{
Application.Run(new formParent());
}

private void btnSave_Click(object sender, System.EventArgs e)
{
Maman filleActive;
filleActive = (Form) this.ActiveMdiChild;
filleActive.saveFille();
}

}

public class formFille1 : System.Windows.Forms.Form,Maman
{

public formFille1()
{
this.ClientSize = new System.Drawing.Size(100, 100);
this.Text = "formFille1";

}

public void saveFille()
{
Console.WriteLine ("save fille 1");
}

}

public class formFille2 : System.Windows.Forms.Form,Maman
{

public formFille2()
{
this.ClientSize = new System.Drawing.Size(100, 100);
this.Text = "formFille2";

}

public void saveFille()
{
Console.WriteLine ("save fille 2");
}

}

interface Maman{
void saveFille();
}

}

Quelqu'un peut il m'aider ?
Je n'ai pas trouvé d'exemples sur le forum qui traite de ce cas (les exemples qui existent utilisent des mdiChild de la meme classe)

Merci

5 réponses

taharban Messages postés 56 Date d'inscription lundi 7 juillet 2003 Statut Membre Dernière intervention 14 novembre 2007
5 mai 2004 à 10:08
et comme ça ?
filleActive = (Maman) this.ActiveMdiChild;

PS: à noter l'emploi de la balise de code qui permet une lecture plus facile
PS2: le message d'erreur est aussi utile dans ce genre de cas
0
ibr4u Messages postés 4 Date d'inscription mardi 16 septembre 2003 Statut Membre Dernière intervention 5 mai 2004
5 mai 2004 à 10:35
Merci pour tes conseils.
L'utilisation de l'interface est une solution que j'ai essayé et qui ne marche pas (sinon je ne posterai pas sur le forum).
L'erreur réside dans le fait que je ne peux pas convertir un objet de ma classe Maman en Form (ce qui parait normal).
voici le msg :
Impossible de convertir implicitement le type 'System.Windows.Forms.Form' en 'testHeritage.Maman'
Une solution serait de faire hériter l'interface d'un Form mais c'est impossible.

L'utilisation d'une interface (ou d'une classe abstraite) sont des pistes que j'ai exploré sans y parvenir.
J'ai aussi essayé de m'en sortir en utilisant la méthode getType :

private void btnSave_Click(object sender, System.EventArgs e)
{
Form filleActive;
filleActive = (Form) this.ActiveMdiChild;
MessageBox.Show( filleActive.GetType().ToString());
//si j'arrive à créer un objet de la classe filleActive.GetType() c'est gagné
}

Voilà j'ai des pistes mais pas le chemin.
0
ibr4u Messages postés 4 Date d'inscription mardi 16 septembre 2003 Statut Membre Dernière intervention 5 mai 2004
5 mai 2004 à 11:43
Ayé finalement j'ai trouvé une solution.
Ce n'est pas la plus courte mais elle est clean car je n'ai plus besoin de toucher au code de btnSave_Click de formParent si je veux lui ajouter une autre fille d'une autre classe.

Donc je passe en paramétre dans le constructeur des filles le bouton Save et on gére l'évenement à l'intérieur des filles.
Bon le hic c'est que ça rajoute pas mal de code dans les classes filles et j'aurai souhaité une solution un peu plus transparente.

voici donc mon nouveau code :

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

namespace testHeritage
{

public class formParent : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnSave;

formFille1 myFormFille1;
formFille2 myFormFille2;

public formParent()
{
InitializeComponent();

this.IsMdiContainer = true;
//créé et ouvre fille1
myFormFille1 = new formFille1(btnSave);
myFormFille1.MdiParent = this;
myFormFille1.Show();

//créé et ouvre fille2
myFormFille2 = new formFille2(btnSave);
myFormFille2.MdiParent = this;
myFormFille2.Show();
}

private void InitializeComponent()
{
this.btnSave = new System.Windows.Forms.Button();
this.SuspendLayout();

//
// btnSave
//
this.btnSave.Location = new System.Drawing.Point(536, 8);
this.btnSave.Name = "btnSave";
this.btnSave.Size = new System.Drawing.Size(64, 24);
this.btnSave.TabIndex = 0;
this.btnSave.Text = "Save";
//on ne gere plus l'évenement dans le parent mais dans les filles
//this.btnSave.Click += new System.EventHandler(this.btnSave_Click);

//
// formParent
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(608, 485);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.btnSave});
this.IsMdiContainer = true;
this.Name = "formParent";
this.Text = "formParent";
this.ResumeLayout(false);

}

[STAThread]
static void Main()
{
Application.Run(new formParent());
}
}

//la classe fille1
public class formFille1 : System.Windows.Forms.Form
{

Button mBtnSave;

//constructeur où l'on passe en paramétre le boutton du parent
public formFille1(Button pBtnSave)
{
this.ClientSize = new System.Drawing.Size(400, 400);
this.Text = "formFille1";
mBtnSave = pBtnSave ;
this.mBtnSave.Click += new System.EventHandler(this.mBtnSave_Click);
}

//on gere l'évenement directement dans la classe fille
private void mBtnSave_Click(object sender, System.EventArgs e)
{
Form parrentForm = Form.ActiveForm; //recupere le parent
Form currentChild =parrentForm.ActiveMdiChild; //recupere la mdi Actif
if (currentChild != null)
{
if (this == currentChild) //si le mdi actif est celui ci alors on éxecute le code
{
MessageBox.Show("Save de Fille1");
}
}

}
}

//idem que fille1 mais pas pareil
public class formFille2 : System.Windows.Forms.Form
{

Button mBtnSave;

public formFille2(Button pBtnSave)
{
this.ClientSize = new System.Drawing.Size(200, 200);
this.Text = "formFille2";
mBtnSave = pBtnSave ;
this.mBtnSave.Click += new System.EventHandler(this.mBtnSave_Click);
}

public void saveFille()
{
Console.WriteLine ("save fille 2");
}

private void mBtnSave_Click(object sender, System.EventArgs e)
{
Form parrentForm = Form.ActiveForm;
Form currentChild =parrentForm.ActiveMdiChild;
if (currentChild != null)
{
if (this == currentChild)
{
MessageBox.Show("Save de Fille2");
}
}

}

}

}

Voilà si quelqu'un à une solution plus courte je suis preneur
Merci à tous ceux qui y ont participé, c'était pourtant un sujet interressant.
0
taharban Messages postés 56 Date d'inscription lundi 7 juillet 2003 Statut Membre Dernière intervention 14 novembre 2007
5 mai 2004 à 11:53
J'ai compilé et lancer ce code puis j'ai appuyé sur save et il a écrit "save fille 1"
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;

namespace testHeritage
{
public class formParent : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnSave;
formFille1 myFormFille1;
formFille2 myFormFille2;
public formParent()
{
InitializeComponent();
this.IsMdiContainer = true;
//fille1
myFormFille1 = new formFille1();
myFormFille1.MdiParent = this;
myFormFille1.Show();
//fille2
myFormFille2 = new formFille2();
myFormFille2.MdiParent = this;
}

private void InitializeComponent()
{
this.btnSave = new System.Windows.Forms.Button();
this.SuspendLayout();
// 
// btnSave
// 
this.btnSave.Location = new System.Drawing.Point(272, 8);
this.btnSave.Name = "btnSave";
this.btnSave.Size = new System.Drawing.Size(64, 24);
this.btnSave.TabIndex = 0;
this.btnSave.Text = "Save";
this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
// 
// formParent
// 
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(608, 485);
this.Controls.AddRange(new System.Windows.Forms.Control[] this.btnSave});
this.IsMdiContainer = true;
this.Text = "formParent";
this.ResumeLayout(false);
}

[STAThread]
static void Main() 
{
Application.Run(new formParent());
}
private void btnSave_Click(object sender, System.EventArgs e)
{
Maman filleActive;
filleActive = (Maman) this.ActiveMdiChild;
filleActive.saveFille();
}
}
public class formFille1 : System.Windows.Forms.Form,Maman
{
public formFille1()
{
this.ClientSize = new System.Drawing.Size(100, 100);
this.Text = "formFille1";
}
public void saveFille()
{
Console.WriteLine ("save fille 1");
}
}

public class formFille2 : System.Windows.Forms.Form,Maman
{
public formFille2()
{
this.ClientSize = new System.Drawing.Size(100, 100);
this.Text = "formFille2";
}
public void saveFille()
{
Console.WriteLine ("save fille 2");
}
}
interface Maman
{
void saveFille();
}
}

Cela ne marche pas sur ta machine ?
0

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

Posez votre question
ibr4u Messages postés 4 Date d'inscription mardi 16 septembre 2003 Statut Membre Dernière intervention 5 mai 2004
5 mai 2004 à 12:12
Ah magnifique.
A force de cast dans tous les sens, il semblerait que celui là m'avait échappé et pourtant je suis persuadé de l'avoir testé.
Merci car c'est la solution que de loin je préfère.
0
Rejoignez-nous