Gérer form1 à partir de form 3

Résolu
benabdessamed Messages postés 52 Date d'inscription jeudi 29 novembre 2007 Statut Membre Dernière intervention 26 septembre 2012 - 21 mars 2011 à 08:56
benabdessamed Messages postés 52 Date d'inscription jeudi 29 novembre 2007 Statut Membre Dernière intervention 26 septembre 2012 - 27 mars 2011 à 14:22
bonjour,

j'ai form1 qui fait appel à form2, et form2 appel à form3, puis form3 appel form1

je veut gérer form1(textbox) à partir de form3, qq peut m'aider

mon code est :
form1 :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

}
// private Label label;

// Construction etc as normal


private void button1_Click(object sender, EventArgs e)
{
Form2 form = new Form2(this);
form.ShowDialog();


}
//[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public string LabelText
{
get { return label1.Text; }
set { label1.Text = value; }
}

private void Form1_Load(object sender, EventArgs e)
{

}



}
}

form2 :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();

}

private Form1 mainForm = null;

public Form2(Form callingForm)
{
mainForm = callingForm as Form1;
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

this.mainForm.LabelText = txtMessage.Text;

//Form1 form = new Form1();
// form.Activate.;
//Close();
}

private void Form2_Load(object sender, EventArgs e)
{

}

private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
mainForm.Close();

}

private void button3_Click(object sender, EventArgs e)
{
Form3 form = new Form3(this);
form.ShowDialog();

}
}
}
form3 :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
public partial class Form3 : Form
{
private Form1 mainForm = null;
public Form3(Form callingForm)
{
mainForm = callingForm as Form1;
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
this.mainForm.LabelText = textBox1.Text;

}
}
}

j'ai le message d'erreur :
Object reference not set to an instance of an object. au niveau :
this.mainForm.LabelText = textBox1.Text;

merci


abdessamed

4 réponses

cs_jopop Messages postés 1540 Date d'inscription lundi 26 mai 2003 Statut Membre Dernière intervention 1 août 2013 12
21 mars 2011 à 15:56
Salut,

Lorsque tu instancies ta Form3 tu passes this au constructeur. Or tu es dans la Form2, du coup le frm3.mainForm pointe sur frm2.
Remplace dans button3_Click :
	Form3 form = new Form3(this); 

par :
	Form3 form = new Form3(this.mainForm); 
3
Renfield Messages postés 17287 Date d'inscription mercredi 2 janvier 2002 Statut Modérateur Dernière intervention 27 septembre 2021 74
21 mars 2011 à 10:43
où est définit ce textBox1 ??

même a supposer que la ligne correcte soit :

mainForm.textBox1.Text

Aucun de tes 3 form ne déclarent de textbox...

Renfield - Admin CodeS-SourceS - MVP Visual Basic & Spécialiste des RegExp
0
benabdessamed Messages postés 52 Date d'inscription jeudi 29 novembre 2007 Statut Membre Dernière intervention 26 septembre 2012 3
21 mars 2011 à 15:40
textBox1 est définit dans form3
et LabelText (this.mainForm.LabelText = textBox1.Text;) est définit dans form1

merci


abdessamed
0
benabdessamed Messages postés 52 Date d'inscription jeudi 29 novembre 2007 Statut Membre Dernière intervention 26 septembre 2012 3
27 mars 2011 à 14:22
merci

abdessamed
0
Rejoignez-nous