-1

it was work before but now not working why i dont know what is the issue?

in example have 2 forms form1 have 1 button form2 have 1 textbox when start the program and click the button form1 should close, form2 open and delegate variable should write in textbox but not work. Error is "System.NullReferenceException occurred"

public partial class Form1 : Form
{
    public delegate void kapatici(string al);
    public static event kapatici kapat;
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        kapat("deneme");
        Form2 f = new Form2();
        f.ShowDialog();
    }
}

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        Form1.kapat += Form1_kapat;
    }

    private void Form1_kapat(string al)
    {
        textBox1.Text = al;
        //throw new NotImplementedException();
    }
}

I've tried different types like

private void button1_Click(object sender, EventArgs e)
{
    Form2 f = new Form2();
    f.ShowDialog();
    kapat("deneme");
}

but still not working. thank you for your answers.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
iekara
  • 1
  • 2
  • We do not add "solved" to a title on Stack Overflow. If you have in fact solved the problem, post an _answer_ to the question and click the "accept" button on that answer. – Peter Duniho Oct 17 '16 at 02:18
  • We also do not answer questions that have already been asked and answered before. If after following the extensive advice in the marked duplicate, you still cannot solve the problem, post a new question explaining what you've done already to diagnose the problem, and express your question in a manner that goes beyond simply stating that your code throws `NullReferenceException`. – Peter Duniho Oct 17 '16 at 02:19
  • Hi Peter, actualy i saw your linked subject before wrote that question. But i coudnt find right solition for me. Sorry if my entry is duplicate. 2nd point is didnt click the accept button because i didnt try answer i dont want to manipulate who search answers. 3rd point is i found a solition different way before answer to my question. The problem is algorithmic The delegate stack on opening new form and i changed way delegate position between forms and it works. I know this is not best solition but its work and i couldnt write here because this is not professionel solition. Thank you. – iekara Oct 18 '16 at 06:42

1 Answers1

0

I'm not sure what you want to achieve with the code - but I think / hope it's just a minimal example. So here is a solution:

Events are null until they are suscribed. You have to check the event before you are going to invoke it:

if (kapat =! null)
{
  kapat.Invoke("deneme");
}

A compact way to do this is by using the null-operator:

kapat?.Invoke("deneme");

Second mistake is to show your second form as dialog, because you are blocking your method until the dialog is closed. If your click-method looks like this, it will work:

private void button1_Click(object sender, EventArgs e)
{
    Form2 f = new Form2();
    f.Show();
    kapat?.Invoke("deneme");
}

If you only want to show a text on your second form, you should not use a event. The simplest way is to pass the string throught the constructor of the second form like:

public Form2(string al)
{
    InitializeComponent();
    textBox1.Text = al;
}

Now you can open the form with:

private void button1_Click(object sender, EventArgs e)
{
    Form2 f = new Form2("deneme");
    f.ShowDialog();
}
Fruchtzwerg
  • 10,999
  • 12
  • 40
  • 49