|
|
 |
 |
 |
 |
Best Way To Close A 'Loaded' Form
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
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 <RFlem @NationalSteel.com> wrote in message news:1181048451.444901.179510@h2g2000hsg.googlegroups.com...
>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
Thanks! On Jun 5, 9:06 am, "AlexS" <salexru200@SPAMrogers.comPLEASE> wrote:
> 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
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] - m@spam.guard.caspershouse.com <RFlem @NationalSteel.com> wrote in message news:1181049274.669998.245430@q69g2000hsb.googlegroups.com...
> Thanks! > On Jun 5, 9:06 am, "AlexS" <salexru200@SPAMrogers.comPLEASE> wrote: >> 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
Nicholas, I did not ask the question, just answered it. Cheers "Nicholas Paldino [.NET/C# MVP]" <m@spam.guard.caspershouse.com> wrote in message news:A3BDB1E2-F3F4-4457-B814-9EEC92F4F3AC@microsoft.com...
> 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] > - m@spam.guard.caspershouse.com > <RFlem@NationalSteel.com> wrote in message > news:1181049274.669998.245430@q69g2000hsb.googlegroups.com... >> Thanks! >> On Jun 5, 9:06 am, "AlexS" <salexru200@SPAMrogers.comPLEASE> wrote: >>> 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
On Jun 5, 7:46 pm, "AlexS" <salexru200 @SPAMrogers.comPLEASE> wrote:
> Nicholas, > I did not ask the question, just answered it. > Cheers > "Nicholas Paldino [.NET/C# MVP]" <m@spam.guard.caspershouse.com> wrote in > messagenews:A3BDB1E2-F3F4-4457-B814-9EEC92F4F3AC@microsoft.com... > > 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] > > - m@spam.guard.caspershouse.com > > <RFlem@NationalSteel.com> wrote in message > >news:1181049274.669998.245430@q69g2000hsb.googlegroups.com... > >> Thanks! > >> On Jun 5, 9:06 am, "AlexS" <salexru200@SPAMrogers.comPLEASE> wrote: > >>> 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- Hide quoted text - >
Use the Close() method for form. If you want to close on some event of same form close this.Close(). -----------------------------------------------Reply-----------------------------------------------
Which is why I responded to the OP, not your response. =) -- - Nicholas Paldino [.NET/C# MVP] - m@spam.guard.caspershouse.com "AlexS" <salexru200 @SPAMrogers.comPLEASE> wrote in message news:uExsgA4pHHA.2652@TK2MSFTNGP02.phx.gbl...
> Nicholas, > I did not ask the question, just answered it. > Cheers > "Nicholas Paldino [.NET/C# MVP]" <m@spam.guard.caspershouse.com> wrote > in message news:A3BDB1E2-F3F4-4457-B814-9EEC92F4F3AC@microsoft.com... >> 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] >> - m@spam.guard.caspershouse.com >> <RFlem@NationalSteel.com> wrote in message >> news:1181049274.669998.245430@q69g2000hsb.googlegroups.com... >>> Thanks! >>> On Jun 5, 9:06 am, "AlexS" <salexru200@SPAMrogers.comPLEASE> wrote: >>>> 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
|
 |
 |
 |
 |
|