473,725 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.frm Main frmMain1 = new TCPIPServer.frm Main();
frmMain1.Show() ;
Application.Run ();
}
}

*************** *************** ***************
frmMain.cs below
*************** *************** ***************
using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;

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

private void btExit_Click(ob ject sender, EventArgs e)
{
this.Parent.Par ent.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 1616
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******@Natio nalSteel.comwro te in message
news:11******** **************@ h2g2000hsg.goog legroups.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.frm Main frmMain1 = new TCPIPServer.frm Main();
frmMain1.Show() ;
Application.Run ();
}
}

*************** *************** ***************
frmMain.cs below
*************** *************** ***************
using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;

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

private void btExit_Click(ob ject sender, EventArgs e)
{
this.Parent.Par ent.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.comP LEASEwrote:
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.frm Main());
}
}
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.co m

<RF******@Natio nalSteel.comwro te in message
news:11******** **************@ q69g2000hsb.goo glegroups.com.. .
Thanks!

On Jun 5, 9:06 am, "AlexS" <salexru200...@ SPAMrogers.comP LEASEwrote:
>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.c omwrote in
message news:A3******** *************** ***********@mic rosoft.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.frm Main());
}
}
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.co m

<RF******@Natio nalSteel.comwro te in message
news:11******** **************@ q69g2000hsb.goo glegroups.com.. .
>Thanks!

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

I did not ask the question, just answered it.

Cheers
"Nicholas Paldino [.NET/C# MVP]" <m...@spam.guar d.caspershouse. comwrote in
messagenews:A3* *************** *************** ***@microsoft.c om...
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.frm Main());
}
}
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.c om
<RFlem...@Natio nalSteel.comwro te in message
news:11******** **************@ q69g2000hsb.goo glegroups.com.. .
Thanks!
On Jun 5, 9:06 am, "AlexS" <salexru200...@ SPAMrogers.comP LEASEwrote:
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.co m

"AlexS" <sa***********@ SPAMrogers.comP LEASEwrote in message
news:uE******** ******@TK2MSFTN GP02.phx.gbl...
Nicholas,

I did not ask the question, just answered it.

Cheers
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c omwrote
in message news:A3******** *************** ***********@mic rosoft.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.frm Main());
}
}
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.co m

<RF******@Nati onalSteel.comwr ote in message
news:11******* *************** @q69g2000hsb.go oglegroups.com. ..
>>Thanks!

On Jun 5, 9:06 am, "AlexS" <salexru200...@ SPAMrogers.comP LEASEwrote:
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
25028
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 less complex technical calculations, several input variables. I would like to equipp the starting panel with the usual New, Open, Save, Save As, Close etc. menus (like in Excel, or Word, etc.) What is the best way to accomplish Save, or Save As?...
4
2771
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, if there was a problem connecting to the database and populating the Listview control. I've tried something like this:
6
9423
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 open/closed and their current state. now i can't. before i didn't have to declare numerous named instances each time i loaded or unloaded a form.
10
30689
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 to call DsStudentCourse1.Tables("SchYrSemCourseJoin").Clear() SqlDataAdapter3.Fill(DsStudentCourse1) However, the fill method causes a lot of time to process.
1
3526
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 DB is fired, the window closes, and the opener window's (server) form is submitted (via JavaScript) (so that any form changes that were made are retained) and the newly loaded page shows the new selections from the popup window. However, the...
10
10046
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 believe that this has to do with unreleased form component events or event handlers. I'm comparatively new to .net and windows forms, in the sense that though I've been using them for over 2 years now, it's been rather sporadic. I work with...
1
4153
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 scenes) after the main form has loaded. Do I have to shift focus back to frmLogin after the Main form has loaded? What is the proper syntax for shifting focus back to a form
32
2351
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 "Compact on Close" option set for the current database. However, even with Compact on Close set, if you use the Application.Quit acQuitSaveNone, the compacting will not fire. This behavior actually seems desirable, as you can now make a program...
0
8888
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9401
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9113
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3221
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2635
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.