Best Way To Close A 'Loaded' Form

R

RFleming

I am a pretty experienced VB programmer trying to make a jump to C#.
I have created a simple (so I thought) project and seem to be stuck.
I know I can create a form and write the code within the form.
However just for the experience I created a Main class and created an
instance of frmMain. Using the designer I created a button btexit.
When the button is pushed I am trying to dispose the loaded instance
of frmMain. I am getting the following error and Internet searches
don't seem to address my particular issue. The error message is:
Only assignment, call, increment, decrement, and new object
expressions can be used as a statement

Example of my code:

Main.cs Below
*******************************************
using System;
using System.Windows.Forms;
using System.Text;

class MainClass
{
public static void Main()
{
TCPIPServer.frmMain frmMain1 = new TCPIPServer.frmMain();
frmMain1.Show();
Application.Run();
}
}

*********************************************
frmMain.cs below
*********************************************
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TCPIPServer
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}

private void btExit_Click(object sender, EventArgs e)
{
this.Parent.Parent.Dispose;
}
}
}
}
**********************************************

Any help, suggestion as to why or why not I would want to even
approach loading a form that way, etc.... would be greatly
appreciated!

Thanks

Ryan
 
A

AlexS

Dispose is method, so you miss brackets: Dispose().

You don't need to call Dispose in button event handler. Just call

this.Close();

HTH
Alex
 
N

Nicholas Paldino [.NET/C# MVP]

Alex,

You are still doing a few things the wrong way. First, you need to pass
your form to the Run method so that it knows what the main window is while
processing the message loop, like so:

class MainClass
{
public static void Main()
{
Application.Run(new TCPIPServer.frmMain());
}
}
Also, there is no need to call the parent of the parent, if you want to
close your form, then just call Close or Dispose on yourself.

If you want to exit the application, just call the Exit method on the
Application object, and it will send an WM_QUIT message to your message
loop, which should terminate the message pump and subsequently, your
program.
 
A

AlexS

Nicholas,

I did not ask the question, just answered it.

Cheers
Nicholas Paldino said:
Alex,

You are still doing a few things the wrong way. First, you need to
pass your form to the Run method so that it knows what the main window is
while processing the message loop, like so:

class MainClass
{
public static void Main()
{
Application.Run(new TCPIPServer.frmMain());
}
}
Also, there is no need to call the parent of the parent, if you want to
close your form, then just call Close or Dispose on yourself.

If you want to exit the application, just call the Exit method on the
Application object, and it will send an WM_QUIT message to your message
loop, which should terminate the message pump and subsequently, your
program.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

 
A

Aneesh Pulukkul

Nicholas,

I did not ask the question, just answered it.

Cheers
message






- Show quoted text -

Use the Close() method for form. If you want to close on some event of
same form close this.Close().
 
N

Nicholas Paldino [.NET/C# MVP]

Which is why I responded to the OP, not your response. =)


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

AlexS said:
Nicholas,

I did not ask the question, just answered it.

Cheers
Nicholas Paldino said:
Alex,

You are still doing a few things the wrong way. First, you need to
pass your form to the Run method so that it knows what the main window is
while processing the message loop, like so:

class MainClass
{
public static void Main()
{
Application.Run(new TCPIPServer.frmMain());
}
}
Also, there is no need to call the parent of the parent, if you want
to close your form, then just call Close or Dispose on yourself.

If you want to exit the application, just call the Exit method on the
Application object, and it will send an WM_QUIT message to your message
loop, which should terminate the message pump and subsequently, your
program.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Thanks!

Dispose is method, so you miss brackets: Dispose().

You don't need to call Dispose in button event handler. Just call

this.Close();

HTH
Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top