473,386 Members | 1,673 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,386 software developers and data experts.

WinForms opening and closing forms sequentially

I am (still) relatively new to Windows applications, most of my experience
has been Web based, and I am confused about what exactly happens when the
Main() method is called and how to manipulate forms opening & closing.

An example of this issue is as follows. I have a logon form open as the
first thing. The main functional form opens when a user has successfully
logged on. From the main form, a user should be able to logout which will
reshow the logon form and the state should be essentially identical to if
they have never logged on in the first place. What I find is that I can't
close any form in this string, or everything shuts down, so I have to hide
forms. But if I do that, then they still lurk in the background and do
strange things.

I have been thinking that I should make an orchestrator class with a method
something like this

public void ReplaceCurrentlyShownForm(Form toOpen, Form currentlyOpen)
{
currentlyOpen.Close();
currentlyOpen.Dispose();
toOpen.Show();
}
and have Main instantiate this class which then runs until the whole app is
closed down.
Would that make sense?

I have been looking for examples of code in which different windows forms
are show at different times in the program, but I have not yet found
anything. If you could point me to something, that would be helpful.
Thanks!
Ethan

Sep 18 '08 #1
2 4208
On Thu, 18 Sep 2008 12:59:01 -0700, Ethan Strauss
<Et**********@discussions.microsoft.comwrote:
I am (still) relatively new to Windows applications, most of my
experience
has been Web based, and I am confused about what exactly happens when
the
Main() method is called and how to manipulate forms opening & closing.
I recommend looking at the Main() method then. It's in, by default, the
Program.cs file. Also by default, it only has three things it does: two
calls to initialize some of the GUI stuff, and then a statement that calls
Application.Run() with a new form instance. Application.Run() will return
once the form passed to it is closed, closing all other open forms owned
by that thread (which is usually all other open forms without exception)
at the same time.

One way to change the behavior of your program is to change the Main()
method so that it calls Application.Run() differently. For example, if
you don't pass it a form instance, then closing any form won't cause
Application.Run() to exit. Only a call to Application.ExitThread()
would. That gives you more control over the lifetime of your application,
regardless of which forms are shown or closed.
An example of this issue is as follows. I have a logon form open as the
first thing. The main functional form opens when a user has successfully
logged on. From the main form, a user should be able to logout which
will
reshow the logon form and the state should be essentially identical to if
they have never logged on in the first place. What I find is that I can't
close any form in this string, or everything shuts down, so I have to
hide
forms. But if I do that, then they still lurk in the background and do
strange things.
"Do strange things"? Like what? If you forms are doing strange things
when hidden, then that's something you should be concerned about. Not
because it's an impediment to the basic functionality, but because you've
made a mistake in authoring your forms somehow. Ideally, your forms
should be strictly UI, but in any case there's no reason that they should
do anything "strange" while hidden.

Also, it should not be trued that you "can't close any form in this
string". By default, there's only one form that's critical: the one
passed to Application.Run(). You can show and close arbitrarily many
other forms without problem; it's only closing that first form that would
cause the application to end.

So, another option is to show/close other forms as you'd like, just taking
care to never close the logon form. Hide that when it's not needed, show
it when it is. Close it when you want the application to exit. If it
does "strange things" when it's hidden, fix it so that it doesn't.
I have been thinking that I should make an orchestrator class with a
method
something like this

public void ReplaceCurrentlyShownForm(Form toOpen, Form
currentlyOpen)
{
currentlyOpen.Close();
currentlyOpen.Dispose();
toOpen.Show();
}
and have Main instantiate this class which then runs until the whole app
is
closed down.
Would that make sense?
How would that help? At some point, you still need to call
Application.Run(). And with or without a method such as above, the same
rules apply.

By the way, Form.Close() does a Dispose() implicitly, unless you used
Form.ShowDialog() to show the form. There's no need to call Dispose()
after closing a non-modal form.

Pete
Sep 18 '08 #2
Thanks Peter.
That's helpful. I think I was making things way more complex than they
should be by using .ShowDialog() when that was not really what I wanted,
showing, hiding, and reshowing forms without making sure they were properly
refreshed and so forth. I think I can probably go back through and simply
everything by making sure that the initial form is not closed and making sure
I reset everything when a user ends up back at a form that had existed
before.
Ethan

Sep 19 '08 #3

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

Similar topics

3
by: Jean-Fran?ois Lacrampe | last post by:
Hello, I want to write a _very_ simple text parser that would replace a string like: "This is text with /italics/, *bold* and _underline_." and generate automatically something like this: ...
2
by: Toonman | last post by:
I have a single .asp page that opens a connection and then sequentially opens and closes 14 recordsets from stored procedures to obtain various product information before closing the connection. ...
3
by: ksedran | last post by:
Hi all, I am running into an issue trying to close a form from another form. I have a main form called MainForm. A child form is opened in the MainForm as follows: Dim Form2 As New Form2...
1
by: N. Graves | last post by:
Hi, I want to have a Search Dialog box that has several text box and fields to build a search and display the results in a form. I can do everything that I need to if I us a report but I would...
3
by: Greg | last post by:
On my report I want to have an opening balance signifying all transactions up to the month selected and detailed transactions for the month selected and then a closing blance. I'm perpelexed...
16
by: iwdu15 | last post by:
how can i open a file i saved and place the info into different text boxes?
3
by: Arne Beruldsen | last post by:
The migration from VB6 is anything but easy. Ok...I have an introductory form (start-up is via sub main) and then several succeeding forms which gather some info. As soon as the info is gathered...
23
by: raylopez99 | last post by:
Here I am learning WinForms and two months into it I learn there's a WPF API that is coming out. Is this WPF out yet, and is it a threat to WinForms, in the sense that all the library routines I...
0
tlhintoq
by: tlhintoq | last post by:
My project involves reading magstripe cards, capturing an ID number, then spinning off to do some other things including taking a photograph. 99% of this is done and good. All early phases of work...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...

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.