Cannot make a new form active

J

jjeffers1

Hello - I am an experienced web programmer who is new to windows forms
programming.
The problem I am facing is passing control in a winforms app from one
form to another. Essentially I have form1, which has some text fields,
and upon submittal of those fields using a button click, I want to save
those and open a new form called form2. I instantiate form2 in the
button click event handler of form1 by using:

Form Form2 = new Form();
Form2.Activate();
Form2.Visible = true;

This works fine as it displays form2. However if I close form1 manually
or using:

this.Close();

in the same button click event handler after the previous code snippet
it closes the whole application. That leads me to believe that form1
remains the "ActiveForm". How do I make form2 the active form after the
submit event?

Thanks for any help.
 
G

Guest

This works fine as it displays form2. However if I close form1 manually
or using:
this.Close();
in the same button click event handler after the previous code snippet
it closes the whole application. That leads me to believe that form1
remains the "ActiveForm". How do I make form2 the active form after the
submit event?

In a windows app, you typically have a startup object which is a form. When
the form is closed, the app ends, and all of this is by design. In your
case, it sounds like you want to start a new form and end the old form, and
do this perhaps several times. There are two ways I would approach this
problem.

The first is to use ShowDialog rather than Show. The idea is that modal
dialogs would transition one to the next rather than nonmodal forms. If the
transition logic is simple (ie always form1 to form2 to form3, then program
end), then this would be simple to do. If the transition logic is
complicated (ie if formx may transit to formy or formz depending on what the
user does), then this would be a more complicated design. Sub main (for
example) would have to capture and interpret the outputs of each dialog in
order to decide what to do next. It can be done, but it requires some glue,
if you follow the analogy.

The second is to create a new form, form0, and make it be your .net startup
object. In form0's load event, have it make itself invisible and have it
show your form1. Have your form1 and form2 (and others if you need) do what
they do now. When it is time to end the program, do form0.close.
 
S

Stoitcho Goutsev \(100\)

jjeffers1,

It has nothing to do with what form is active. Form1 is your main form that
you pass to the Application.Run method, so when you close that form the
application will exit the message loop and finish. In order to solve this
you need to use Application.Run override that excepts ApplicationContext
object. This way you can control when to close the application. Look at the
MSDN regarding this overload of Application.Run. They have an example of how
to create application with two forms. Look also at the ApplicationContext
class docs.
 

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

Top