Update main UI from another form

L

lucaoscar

Hi all,

as you can see from the level of my question, i am pretty new to c# and
programming in general.

situation: i would like to update the UI of a form (main form of my
application) from a second form (which was called from the first).
Ideally when a button on the second form is pressed, the first one
(main form) should update/refresh its UI.

But it would be also fine to have the first form refreshing when the
second form is closed/disposed.

Can you help me please? Thanks in advance.

Luca
 
C

Charles Law

Hi Luca

The Observer pattern would be worth a look here. Have your second form raise
an event when the UI needs to be updated, and then handle this event on your
main form. The event can either pass the data to use for the update, in an
EventArgs derived class, or the main form can simply use the event to
trigger a request for the latest information.

HTH

Charles
 
G

Guest

situation: i would like to update the UI of a form (main form of my
application) from a second form (which was called from the first).
Ideally when a button on the second form is pressed, the first one
(main form) should update/refresh its UI.

But it would be also fine to have the first form refreshing when the
second form is closed/disposed.

There are two ways to approach this:

1. Your main form can have a public UpdateUI() method that your second form
calls when it wants the main form to update its UI.
2. Your second form can have an UpdateNow event that is raised when it
wants other forms to update. Then your main form can subscribe to that event
and update its UI when the event is raised.

If you only have two forms, then #1 is probably easier. If you have
multiple forms that you need to update, then #2 is preferred.
 
G

GhostInAK

Hello Mini-Tools Timm info at mini-tools dot com,

#1 sucks in any case (2 forms or more). I would suggest steering clear of
it.

You could make your second form act like a dialog box. Assign the DialogResult
property of your OK and Cancel buttons.. then from the first form you can
do something like:

Dim tDialog as Form2 = New Form2
' Set tDialog properties here..
If tDialog.ShowDialog(Me) = DialogResult.OK Then
' The user is always wrong, but what they hey, let em suffer.
End If

tDialog.Dispose
 
G

Guest

GhostInAK said:
#1 sucks in any case (2 forms or more). I would suggest steering clear of
it.

Why is that? If Form2 is always launched from Form1, a reference to Form1
can be passed into Form2's constructor. When the button in Form2 is clicked,
it can call the public UpdateUI() method in Form1 to update it. It's not a
bad way especially if the forms are loosely coupled.
You could make your second form act like a dialog box. Assign the DialogResult
property of your OK and Cancel buttons..

Yes, this is the preferred way if Form2 is a dialog of Form1. But from his
question, I gathered that he wanted the two forms to remain loosely coupled.
It's tough to be sure without understanding his exact problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top