Click here to Skip to main content
15,910,981 members
Articles / Programming Languages / Visual Basic
Article

Observer Design Pattern for Windows Controls

Rate me:
Please Sign up or sign in to vote.
1.24/5 (12 votes)
3 Feb 20062 min read 38.9K   414   17   10
The puropse of this article to implement an Observer pattern for Windows controls, with the change in data of one MDI child, How to get the other registered forms to intimate the changes.

Introduction

I was working on a health care project, and in that project I found that information about same thing can e entered from multiple MDI child forms, but if any two such forms that can add same information are opened at the same time, then if the user adds some data ion control on one form it is not updated on the related control on the other opened form.

Thats why I found a simple but really useful solution. It implements two main design Patterns, 1) Singelton 2) Observer, both patterns helped me to accomplish my task, and I think it might be useful for you people as well.

Problem Statement

Now, in the demo application I have datagrid controls on three form. Now I want if the data is added or deleted from a grid on one form it should be deleted from the other two grids as well (if the forms are created).

Solution

I came up with a simple solution, what I did was, I made a new MDI Application. Then I made three Child forms, each containing a datagrid and a Toolbar to add/delete rows. All the three child forms have been Inherited from FrmParent, that in turns has been inherited from windows.Forms.Form, FrmParent has a Public Overridable method "Update", that is called by the Ovserver class to update the changes.

Observer class is a combination os singleton and Observer design patterns. It has four methods:

1) Public Shared Function Instance() As Observer
 This is a shared method, and returns the singleton object of the class.
2) Public Sub Add(ByVal form As FrmParent)
 This method add the form tho the ArrayList.
3) Public Sub Notify(ByVal row As DataRow, ByVal op As FrmParent.Operation)
 This method intimates the registered forms to get the updates.
4) Public Sub Release(ByVal form As FrmParent)
 On form close this method is called to release the form from the ArrayList of Observer class.

Note: Open all the three forms in the application,and then start data entry, otherwise the newly adds will take effect but the previous data will not come on the new form.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Pakistan Pakistan
hello

Comments and Discussions

 
GeneralThank you. Pin
Marc Leger24-Feb-06 6:38
Marc Leger24-Feb-06 6:38 
GeneralScream Pin
NormDroid3-Feb-06 2:53
professionalNormDroid3-Feb-06 2:53 
GeneralRe: Scream Pin
Jason McBurney3-Feb-06 11:57
Jason McBurney3-Feb-06 11:57 
Was that nessecary? Why not assist your peers and our community by suggesting constructive critisim rather than trashing other people's work. It takes a lot of courage to write an article which 100,000 of your peers and many potinal employers will read! I see that you have written quite a few articles yourself; therefore, you must have a shared understanding of the anexity. Moreover, your comment is quite vauge and offers no development stragies. I would suggest, on a go foward basis, focused suggestion would be appricated.

Marc Clifton wrote an exelent article on writing articles, we would be of value, but I can not seem to find.


do you need to investigate an online backup[^] company
GeneralRe: Scream Pin
NormDroid6-Feb-06 20:49
professionalNormDroid6-Feb-06 20:49 
GeneralRe: Scream Pin
Jason McBurney7-Feb-06 5:25
Jason McBurney7-Feb-06 5:25 
GeneralRe: Scream Pin
NormDroid7-Feb-06 20:42
professionalNormDroid7-Feb-06 20:42 
GeneralRe: Scream Pin
Secrets6-Feb-06 9:31
Secrets6-Feb-06 9:31 
GeneralRe: Scream Pin
NormDroid6-Feb-06 20:42
professionalNormDroid6-Feb-06 20:42 
GeneralRe: Scream Pin
Secrets8-Feb-06 8:21
Secrets8-Feb-06 8:21 
GeneralRe: Scream Pin
NormDroid8-Feb-06 20:37
professionalNormDroid8-Feb-06 20:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.