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();
}
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.