Katherina
Katherina

Reputation: 2223

C# closing another form problem, Close(); does not work

I have this code on form1

TimerMode f2 = new TimerMode();
f2.show();

now I'm trying to use this code in some point in time, but nothing happens? Cmd = Closing

public void DoActions(string Cmd)
{
  switch(Cmd){

  case"Open":
      TimerMode f2 = new TimerMode();
      f2.show()
      break;
  case"Closing":
       f2.Close();
       break;
}
}

do you have any idea why its not closing?.

what I really want it to close it.

in vb6 I use this

unload form2

Upvotes: 3

Views: 1683

Answers (2)

Xan-Kun Clark-Davis
Xan-Kun Clark-Davis

Reputation: 2843

I just spent hours wondering why my form wont close. Turns out, I forgot to check everything under

Debug->Exceptions

so one of the eventhandlers silently threw a NullPointerException that got caught by the Framework otherwise. I guess that sets the Cancel property of the event arguments to true and setting it to false afterwards does obviously not do the trick (When I fixed the NullPointer, everything worked normal again.).

Upvotes: 0

Teoman Soygul
Teoman Soygul

Reputation: 25742

Most probably a threading issue. Try this:

f2.Invoke((MethodInvoker)(() => f2.Close()));

If that doesn't work, use below modification:

public TimerMode f2 = new TimerMode();
public void DoActions(string Cmd)
{
  switch(Cmd){    
  case"Open":          
      f2.show()
      break;
  case"Closing":
       f2.Close();
       break;
  }
}

Upvotes: 2

Related Questions