0

So I have the template C# windows form, and made the debugInstructionsLabel label public so that I can edit it from outside the form, then added a few lines to main:

namespace test {
    static class Program {
        [STAThread]
        static void Main() {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Form1 form = new Form1();
            Application.Run(form);
            form.debugInstructionsLabel.Text += "Aaaaaaa";
            form.Refresh();
            form.Update();
            form.Invalidate();
            Application.DoEvents();
        }
    }
}

However, this doesn't actually change anything in the form, how would I update the text?

EDIT: It seems like Application.Run doesn't return, should I create another thread or handle everything inside the form class?

Jeffmagma
  • 452
  • 2
  • 8
  • 20
  • 2
    `Application.Run()` starts a windows message pump that doesn't terminate until the application terminates. At that time, that function will return. If you want to modify the form, modify its properties before calling `Application.Run` See https://stackoverflow.com/questions/2222365/what-is-a-message-pump –  Mar 11 '19 at 19:48
  • @Amy or just display the form using ```form.Show()```, don't using ```Application.Run``` at all. – Lucca Ferri Mar 11 '19 at 19:51
  • 1
    @LuccaFerri Not calling `Application.Run` doesn't really answer the question or solve anything in this instance. You're right that it is an option. –  Mar 11 '19 at 19:53
  • @Amy sorry for my ignorance, but as I can understand from his code, he is working with a console application, and will use it to update the form as his console applications runs through. I don't see the point of using Application.Run there, as it should hang the thread. – Lucca Ferri Mar 11 '19 at 19:55
  • 1
    He's not working with a console application. Every winforms application has a `main` method. `Application.Run` and `form.Show` are basically equivalent. –  Mar 11 '19 at 19:56
  • I think I have greatly misunderstood the question then. Sorry. My answer will be deleted. I was sure I read somewhere he was working with a Console application. Perhaps from another question I was reading about. Then it should be Application.Run. – Lucca Ferri Mar 11 '19 at 19:57
  • @Amy They're not. `Form.Show()` is the same as `Form.Visible = true`. You can use the latter to show a Form. `Application.Run()` it's all another beast: it starts the application message loop on the thread. Without it, it this case, after `Form.Show()` the application will simply terminate. – Jimi Mar 12 '19 at 02:52
  • @Jimi You're right, I was thinking of `ShowDialog`: https://stackoverflow.com/questions/2314514/whats-the-difference-between-application-run-and-form-showdialog –  Mar 12 '19 at 03:41
  • @Amy Yes, that is because `Form.ShowDialog()`, after `Control.CreateControl()` calls `Application.RunDialog()`, which, of course, calls `ThreadContext.FromCurrent().RunMessageLoop(...)`. Thus, you always call `Application.Run()` in a way or another. – Jimi Mar 12 '19 at 13:07
  • @Jimi Yes, I understand that. I don't need it explained. –  Mar 12 '19 at 13:08

1 Answers1

1

You should put the line that changes the text of the label in the onLoad event of the form. Even on the constructor after initializecomponents is run would be fine. With that minor change it will work.

Hope this helps!

Matt
  • 319
  • 2
  • 7