Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / June 2006

Tip: Looking for answers? Try searching our database.

Update main UI from another form

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
lucaoscar@gmail.com - 15 Jun 2006 10:39 GMT
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
Charles Law - 15 Jun 2006 11:01 GMT
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

> Hi all,
>
[quoted text clipped - 12 lines]
>
> Luca
Mini-Tools Timm - 15 Jun 2006 15:09 GMT
> 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).
[quoted text clipped - 3 lines]
> 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.

Signature

Timm Martin
Mini-Tools
.NET Components and Windows Software
http://www.mini-tools.com

GhostInAK - 16 Jun 2006 01:50 GMT
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

>> 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).
[quoted text clipped - 16 lines]
> 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.
Mini-Tools Timm - 16 Jun 2006 05:21 GMT
> #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.

Signature

Timm Martin
Mini-Tools
.NET Components and Windows Software
http://www.mini-tools.com


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.