Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update form after it has started?

Is it possible to update the winform after it has started.

for example.

import clr
clr.AddReference("System")
clr.AddReference("System.Windows.Forms")
from System.Windows.Forms import (Application, Form, StatusBar)

form = Form()
form.sb = StatusBar()
form.sb.Parent = form
form.sb.Text = "Demo"
Application.Run(form)
form.sb.Text = "Test"

I am able to see "Demo" but not the "Test".

How to change the status bar after Application.Run?

like image 537
Rahul Avatar asked Jan 20 '26 18:01

Rahul


2 Answers

Just add code into the load event

 private void Form_Load(object sender, EventArgs e)
    {
       sb.Text = "Test";
      Application.DoEvents();
    }

Call do events to force a repaint of the form.

VB

Private Sub Form_Load(Byval Sender As Object, Byval e As EventArgs)
      sb.Text = "Test"
      Application.DoEvents()
End Sub
like image 102
Wheels73 Avatar answered Jan 22 '26 09:01

Wheels73


Application.Run(form) starts a standard application loop on the current thread. That means the line form.sb.Text = "Test" won't actually do anything until the form is closed. You can show the form by using form.Show(), and it would be possible to update it then.

You could do one of two things. First, as Wheels73 suggested, add an event handler on the form load event. You can accomplish that with the following lines.

def form_load(sender, e):
    sender.sb.Text = 'Test'

form.Load += form_load
Application.Run(form)

You could also create a class for the form and add event handlers in there. I would recommend giving this a read: http://www.voidspace.org.uk/ironpython/winforms/

like image 27
David Avatar answered Jan 22 '26 09:01

David