Click here to Skip to main content
15,920,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello all,

I was asked to develop an extremely simple GUI to front-end what was a console program in order to give the user assurance that the program actually is doing something. I created a Windows application with a single form. I designed the form so that there is a label which displays "Working. Please stand by...". In the Form1() method(?) I do some work & track whether or not the program completed its task successfully. Depending on success or failure, I change the text in the label, alter the image in a picturebox and make an OK button visible that the user can click to dismiss the form.

Here's the problem I'm running into: The form will not actually appear until I drop into the method which alters the text in the label. So, in effect, the user doesn't see the form at all until the program has completed its work. I'm not sure what I am doing wrong. I would expect the form to appear immediately after calling InitializeComponent(). Any advice would be greatly appreciated.

C#
namespace Blah
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            bool result = doStuff();
            if (result) 
                success();
            else
                fail();
        }

        private void success()
        {
            lblStatus.Text = "Process was successful.";
            pbxStatus.Image = global::IACRenew2.Properties.Resources.check;
            btnOK.Visible = true;
        }

        private void fail()
        {
            lblStatus.Text = "An error occurred.  Please contact support.";
            pbxStatus.Image = global::IACRenew2.Properties.Resources.error;
            btnOK.Visible = true;
        }
    }
}
Posted
Comments
Sergey Alexandrovich Kryukov 11-Jul-12 17:31pm    
It totally depends on what doStuff does; and you did not inform us on that -- not going to work for you.
--SA

1 solution

You should not do tasks (doStuff) that affect form initialization, even less in a constructor. Put it in Load event handler.
 
Share this answer
 
Comments
CertNerd 11-Jul-12 16:20pm    
Ok - so I have a Load method written. How do I trigger the Load event?
Zoltán Zörgő 11-Jul-12 16:27pm    
Are you writing your code with notepad? Load is triggered by the framework, if it is added by InitializeComponent() to the queue.
In VisualStudio it is really simple:
- click on the form
- on the right in the properties window choose the events tab
- locate "load", double click in it
- write method code
This way VisualStudio adds the method to the queue.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900