How to close a form in C#


G

Guest

I'm trying to close a form in C#. I have a vb background, so the question
may sound dumb. In vb it is just me.close(). I've tried a bunch of ways to
close the form but C# is acting pretty dumb also. I can't find a way to
close it. Here are some of the things I've tried:

private void button1_Click(object sender, System.EventArgs e)
{
// Form1 frmI = new Form1();
// DialogResult dr = frmI.ShowDialog();

Form1.Quit();
// this.Hide();

}

The new form seems to recreate the form - that is a total nightmare. Hide
doesn't end the program and Close just gets a message that says close is not
defined.
 
Ad

Advertisements

G

Guest

Thanks... tried again with This.close(); and this time it worked. Not sure
what was going on but thanks for the help.
 
H

Herfried K. Wagner [MVP]

thejamie said:
I'm trying to close a form in C#. I have a vb background, so the question
may sound dumb. In vb it is just me.close(). I've tried a bunch of ways
to
close the form but C# is acting pretty dumb also. I can't find a way to
close it. Here are some of the things I've tried:

private void button1_Click(object sender, System.EventArgs e)
{
// Form1 frmI = new Form1();
// DialogResult dr = frmI.ShowDialog();

Form1.Quit();
// this.Hide();

}

The new form seems to recreate the form - that is a total nightmare. Hide
doesn't end the program and Close just gets a message that says close is
not
defined.
Mhm... 'this.Close();' should work.
 
H

Herfried K. Wagner [MVP]

thejamie said:
Thanks... tried again with This.close(); and this time it worked. Not
sure
what was going on
Note that C# is case-sensitive!
 
G

Guest

I'm assuming button1_Click() is an event handler for a button on the form
that you want to close. So if you want to close the form, it would look like
this:

private void button1_Click(object sender, System.EventArgs e)
{
this.close();
}

If this form is the entry point for your application, then the applicaton
will exit when the form is closed. If this form is not the entry point to
your application, but want to close the application, you could use
Application.Exit(); to end your program.
 
G

Guest

Whoa... it isn't self correcting! I'm glad you mentioned this.
Thanks again.
 
Ad

Advertisements

Joined
Sep 15, 2006
Messages
10
Reaction score
0
If using Application.Exit() :

'System.Web.HttpApplicationState' does not contain a definition for 'Exit' and no extension method 'Exit' accepting a first argument of type 'System.Web.HttpApplicationState' could be found (are you missing a using directive or an assembly reference?)

And I have an assembly reference to System.Web (where HttpApplicationState should be) AND an Import tag for System.Web.HttpApplicationState. I get intellisense on "Application" but it does not include ".Exit()".

this.close() gives this:

'ASP.testpage_aspx' does not contain a definition for 'close' and no extension method 'close' accepting a first argument of type 'ASP.testpage_aspx' could be found (are you missing a using directive or an assembly reference?)

So anyone want to tell me how this is supposed to work/be the answer?
 
Ad

Advertisements

Joined
Sep 15, 2006
Messages
10
Reaction score
0
I found that the above might have been dealing with WinForms, whereas I needed a solution for an ASP .NET button on a C#-based web page.

So after looking high and low, I found that either of these would work:

closeButton.Attributes.Add("onclick", "javascript:window.close(); return false;")

<asp:Button ID="CloseButton" runat="server" Text="Close" OnClientClick="javascript:window.close(); return false;" />
 

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

Similar Threads


Top