473,407 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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

Jun 5 '07 #1
6 1607
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

<RF******@NationalSteel.comwrote in message
news:11**********************@h2g2000hsg.googlegro ups.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

Jun 5 '07 #2
Thanks!

On Jun 5, 9:06 am, "AlexS" <salexru200...@SPAMrogers.comPLEASEwrote:
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
Jun 5 '07 #3
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]
- mv*@spam.guard.caspershouse.com

<RF******@NationalSteel.comwrote in message
news:11**********************@q69g2000hsb.googlegr oups.com...
Thanks!

On Jun 5, 9:06 am, "AlexS" <salexru200...@SPAMrogers.comPLEASEwrote:
>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
Jun 5 '07 #4
Nicholas,

I did not ask the question, just answered it.

Cheers
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:A3**********************************@microsof t.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]
- mv*@spam.guard.caspershouse.com

<RF******@NationalSteel.comwrote in message
news:11**********************@q69g2000hsb.googlegr oups.com...
>Thanks!

On Jun 5, 9:06 am, "AlexS" <salexru200...@SPAMrogers.comPLEASEwrote:
>>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


Jun 5 '07 #5
On Jun 5, 7:46 pm, "AlexS" <salexru200...@SPAMrogers.comPLEASEwrote:
Nicholas,

I did not ask the question, just answered it.

Cheers
"Nicholas Paldino [.NET/C# MVP]" <m...@spam.guard.caspershouse.comwrote in
messagenews:A3**********************************@m icrosoft.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.comwrote in message
news:11**********************@q69g2000hsb.googlegr oups.com...
Thanks!
On Jun 5, 9:06 am, "AlexS" <salexru200...@SPAMrogers.comPLEASEwrote:
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 -

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

Jun 5 '07 #6
Which is why I responded to the OP, not your response. =)
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"AlexS" <sa***********@SPAMrogers.comPLEASEwrote in message
news:uE**************@TK2MSFTNGP02.phx.gbl...
Nicholas,

I did not ask the question, just answered it.

Cheers
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:A3**********************************@microsof t.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]
- mv*@spam.guard.caspershouse.com

<RF******@NationalSteel.comwrote in message
news:11**********************@q69g2000hsb.googleg roups.com...
>>Thanks!

On Jun 5, 9:06 am, "AlexS" <salexru200...@SPAMrogers.comPLEASEwrote:
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



Jun 5 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Andras Gilicz | last post by:
Hi VB fans I'm working on a relatively large project in VB6 with about a dozen forms, including graphs, labels, text boxes, etc. The software itself is actually a flow simulator with more or...
4
by: Carolina | last post by:
Hi. I have a MDIchild form called Form1. In Form1_Load code, the form populates a Listview control based on a datareader, but I want to close the form inmediately and return to the parent form,...
6
by: Edwinah63 | last post by:
Hi everyone, could someone give me some thoughts on the best way to manage mdi parent and child forms? in vb6 i could scroll through the forms collection and determine which forms were...
10
by: jaYPee | last post by:
I have a function that call a stored procedure which performs an insert command. now i want to refresh the dataset so that the newly inserted data will be available to my datagrid I have tried...
8
by: PAPutzback | last post by:
How do I keep the form up.
1
by: Matt Jensen | last post by:
Howdy I've got a ASP.NET webform page that pops up a window for a user to make a selection. Once they make a selection in this popup window, the form in the popup is submitted an update to the...
10
by: morangolds | last post by:
Hi, I've been having a problem with C++ Windows Forms apps not "ending" when you close the form window. I've searched about this problem all over the place and most searches have lead me to...
1
by: RLN | last post by:
RE: Access 2003 After the user puts in the password to log in, (in frmLogin, I am displaying the main data entry form. That all works great, but now I cannot close the Login form (behind the...
32
by: Andy | last post by:
To further follow up on my last post regarding the docmd.quit vs. Application.quit using access 2007, I noticed that docmd.quit will correctly compact the database (program file) if you have the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.