Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I execute code after my form starts?

Tags:

c#

winforms

I'm really new to Windows Forms programming and not quite sure what's the right way to go about programming.

This is my confusion.

I have a single form:

    public partial class ReconcilerConsoleWindow : Form
    {
        public ReconcilerConsoleWindow()
        {
            InitializeComponent();
            SetLogText("Started");

        }

        public void SetLogText(String text)
        {
            string logInfo = DateTime.Now.TimeOfDay.ToString() + ": " + text + Environment.NewLine;
            tbx_Log.AppendText(logInfo);
        }


    }

And in my Program.cs class I have the following code:

    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            ReconcilerConsoleWindow window = new ReconcilerConsoleWindow();
            Application.Run(window);

            if (CallSomeMethod() == true)
            {
                 window.SetLogText("True");
            }                     

        }


    }

Now, once the window has been displayed by the Application.Run command, the program halts at that point. How can I do further processing while the window is up?

The above is just an example. My purpose is to read an XMl file and display a datagridview. Subsequently, I watch the XMl file for changes and everytime a change is made, I want to refresh the datagridview. However, once the console pops up, how can i continue with my program and make changes to the information displayed on the form on the fly?

like image 738
xbonez Avatar asked Nov 04 '10 19:11

xbonez


People also ask

What is form load event?

The Form Load Event in VB . NET. An important event you'll want to write code for is the Form Load event. You might want to, for example, set the Enabled property of a control to False when a form loads. Or maybe blank out an item on your menu.

Which function is called for loading of a window form?

Load Event (System. Windows. Forms) | Microsoft Learn.

How do you call a load event in C#?

You have to call Form_load. Form_Load(this, null);


3 Answers

Processing that occurs after Application.Run is usually triggered in the form's Load event handler. You can easily create a Load method in Visual Studio by double clicking any open space on the form.

This would look something like this.

    private void ReconcilerConsoleWindow_Load(object sender, EventArgs e)
    {
        if (CallSomeMethod())
        {
            this.SetLogText("True");
        }
    }

The reason this is (as stated in several other answers) is that the main thread (the one that called Application.Run(window)) is now taken up with operating the Message Pump for the form. You can continue running things on that thread through messaging, using the form's or forms' events. Or you can start a new thread. This could be done in the main method, before you call Application.Run(window), but most people would do this in Form_Load or the form constructor, to ensure the form is set up, etc. Once Application.Run returns, all forms are now closed.

like image 133
C. Ross Avatar answered Nov 10 '22 20:11

C. Ross


Application.Run starts the Windows event handling loop. That loop won't finish til your form closes, at which time anything you do to it won't matter anyway.

If you want to do something with your form, do it in the form's Load event handler.

like image 27
cHao Avatar answered Nov 10 '22 19:11

cHao


Program.cs is not meant to have business rules, it should only call your Form and display it. All datagrid loading/refreshing/editing should be done at your Forms. You should be using the Events defined on Forms class, like: OnLoad, OnUnload, OnClose and many others etc.

like image 31
goenning Avatar answered Nov 10 '22 20:11

goenning