benabdessamed
Messages postés52Date d'inscriptionjeudi 29 novembre 2007StatutMembreDernière intervention26 septembre 2012
-
13 mars 2011 à 09:29
benabdessamed
Messages postés52Date d'inscriptionjeudi 29 novembre 2007StatutMembreDernière intervention26 septembre 2012
-
13 mars 2011 à 12:46
bonjour
j'ai message d'erreur suivant : Object reference not set to an instance of an object
lorsue je veut modifie un composant d' un form1 à partir de form1;
avec ce code :
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();
form.ShowDialog();
}
public string LabelText
{
get { return Lbl.Text; }
set { Lbl.Text = value; }
}
et pour la deuxiem form
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();
}
cs_jopop
Messages postés1540Date d'inscriptionlundi 26 mai 2003StatutMembreDernière intervention 1 août 201312 13 mars 2011 à 10:15
Salut,
Dans ton event button1_Click, lorsque tu instancies un objet de type Form2, tu fais appel au constructeur par défaut (new Form2()). Ton objet de type Form2 n'a donc pas référencer la Form appelante dans sa variable mainForm. Du coup mainForm.LabelText = [..] lève une exception.