?
Solved

I have a C# project that I have already set the main form, I want to add another form so that new form appears before this main form. How?

Posted on 2011-04-18
10
Medium Priority
?
278 Views
Last Modified: 2012-05-11
Hi there;

I have a C# project that I have already set the main form, I want to add another form so that new form appears before this main form. How can I do this elegantly?

Kind regards.
0
Comment
Question by:jazzIIIlove
  • 5
  • 3
  • 2
10 Comments
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 35419439
Why don't just reset the main form? It would be easiest solution.

Options - start the main form, keep it not visible and display another form, then make the main form visible. Something like that.
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 35419457
Hi there;

Thanks for your very interest for my question.

but the main form is the place where application starts right? That's the reason I got stuck. I really appreciate if you could provide me a snippet for your case. My main form is really complicated, lots of controls. :(

Kind regards.
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 35419480
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 12

Author Comment

by:jazzIIIlove
ID: 35419600
ok, I did so, I just change my code as Application.Run(new firstForm())I and create a new form and add a button to that new form, but from this point I don't know what to write to new form 's button click as the old form takes some parameters to its constructor as Application.Run(new oldForm(a,b,c)) and naturally, those a.b and c are not recognized within the new first form. What should I do? How can I pass the params of default constructor params of old form in the new first form?

Kind regards.
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 35420210
You could just display it right before the Application.Run() line in program.cs:
static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Form2 f2 = new Form2();
            f2.Show();
            Application.Run(new Form1());
        }
    }

Open in new window


Do you need to interact with that other form from the main form?  Should the user close it?...or should it be closed from the main form?  etc....
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 35420602
The scenario should be:

after initiation of the new form, a user should click a button on the new form and the old form should appear. The critical point is the new form should be destroyed completely, not only hidden.

The old form takes some parameters in its constructor whereas new form doesn't have to be. Yet since they are different cs files, I think there should be an interaction between those forms to pass the parameters. Am I right? If so, how to fix?

Note that I managed hiding but I know the new form is hidden though still there.

Kind regards
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 35420622
>>Note that I managed hiding but I know the new form is hidden though still there.

I must not only hide it, but also destroy it.

Kind regards.

0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 2000 total points
ID: 35421685
Well...you can pass the parameters to the main form as you already are.

Just work around something like the below.

Form2 is the "new form" with the button on it.  I set the ControlBox() to false so you have to use the button to close it.  The button handler simply calls "this.Close()".

I've set the Opacity() of Form1, the "old form", to 0 (zero) to make it load invisible.  When Form2 is closed I set the Opacity to 1 (one) to make it appear.

Here is the program.cs:
static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Form1 f1 = new Form1();
            f1.Opacity = 0; // load it hidden
            Form2 f2 = new Form2();
            f2.ControlBox = false;
            f2.FormClosed += delegate(object sender, FormClosedEventArgs e) { f1.Opacity = 1; f2.Dispose(); };
            f2.Show();
            Application.Run(f1);
        }

    }

Open in new window

0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 35425656
ok, this is working nearly perfect, though, the loading of components in the new form is really slow after doing as above. The form is not heavy loaded, 1 button and 1 label, and event handler. What should I do?

Kind regards.
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 2000 total points
ID: 35425709
You can call DoEvents() after f2.Show() to force the new form to refresh itself before continuing:

            f2.Show();
            Application.DoEvents();
            Application.Run(f1);
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question