FormWindowState.Normal qui ne fonctionne pas

Résolu
Claude Azoulai Messages postés 26 Date d'inscription mercredi 11 août 2004 Statut Membre Dernière intervention 13 avril 2013 - 12 avril 2013 à 15:51
Claude Azoulai Messages postés 26 Date d'inscription mercredi 11 août 2004 Statut Membre Dernière intervention 13 avril 2013 - 13 avril 2013 à 18:12
Bonjour à tous

J'ai deux formulaires, Form1 et Form2.
Quand Form1 est affiché, j'ouvre Form2 et je minimise Form1
Sur Form2, j'ai un bouton button1.
Quand je clique sur button1, j'appelle une routine public « Ouvre_Form1 »qui se trouve sur Form1.
Dans cette routine, j'ai écrit :
this.WindowState = FormWindowState.Normal;

Bien que le programme exécute cette ligne, il ne déclenche pas d'erreur, mais mon formulaire reste toujours minimisé dans la barre des tâches

J'ai fait une application identique en VB.Net, et là ça fonctionne

S'agit-il d'un Bug ? Ou bien, en tant que débutant, je me débrouille très mal.

Voici Form1.cs
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }
        private void Form1_Shown(object sender, EventArgs e)
        {
            Form2 fr2  = new Form2();
            fr2.Show();
            fr2.Top = this.Top;
            fr2.Left = this.Left + this.Width;
            this.WindowState = FormWindowState.Minimized;
        }
        public void Ouvre_Form1()
        {
            this.WindowState = FormWindowState.Normal;
        }
    }
}

Voici Form2.cs
namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        Form1 frm1 = new Form1();
    
        public Form2()
        {
            InitializeComponent();
        }
        public void button1_Click(object sender, EventArgs e)
        {
            frm1.Ouvre_Form1();
        }
    }
}



Claude l'ancien

2 réponses

yann_lo_san Messages postés 1137 Date d'inscription lundi 17 novembre 2003 Statut Membre Dernière intervention 23 janvier 2016 26
12 avril 2013 à 21:35
Salut,

La Form1 que tu utilises dans Form2 est une nouvelle Form et pas du tout la même que celle que tu crois réouvrir !
il faut faire ceci :

dans Form 1

private void Form1_Shown(object sender, EventArgs e)
{
    Form2 fr2 = new Form2( this ); //<------------ ICI
    fr2.Show();
    fr2.Top = this.Top;
    fr2.Left = this.Left + this.Width;
    this.WindowState = FormWindowState.Minimized;
}


Puis dans Form2

public partial class Form2 : Form
{
        ///Form1 frm1 = new Form1(); //<-------- NON !!
        From1 _refFrm1 = null;    

        public Form2(Form1 frm) // <-------- REF EXISTANTE OUI !
        {
            InitializeComponent();
            _refFrm1 = frm; //<-------- Affecte référence !
        }
        public void button1_Click(object sender, EventArgs e)
        {
            /*frm1*/_refFrm1.Ouvre_Form1(); // <------- utilise ref !
        }
}


bye...
3
Claude Azoulai Messages postés 26 Date d'inscription mercredi 11 août 2004 Statut Membre Dernière intervention 13 avril 2013
13 avril 2013 à 18:12
Merci à yann_lo_san

Enfin une modification qui est compréhensible par mes très faibles connaissances sur C#.

En tous les cas, ça fonctionne très bien

Claude l'ancien
0
Rejoignez-nous