some kind of Application Variable with Application Events???

P

Pieter

Hi,

For a VB.NET 2005 application I need some kind Application Variable that
holds a value that can be accessed everywhere in the application. It should
also be available for a usercontrol in a seperate dll. Even more: I should
be able to trigger everywhere in the application when this variable changes.

I need it for some kind of Copy Paste operation of Documents in the
application. Not only the ID of what I want to copy must be available
everywhere (I could actually do this via the clipboard), but I should be
able to disable everywhere the "paste"-buttons (which are on every form)
when there isn't any copied, and they must be enabled when the user copied
something. So I kind of need a way to handle on different locations the
event.

Any idea how I should do this? I searched for some kind of
application-events, but didn't find a solution.

Any help our hints would be really appreciated!


Thanks a lot in advance!


Pieter
 
B

Bajoo

Dear Pieter,
You can make a Singleton Class which extend Hashtable,
so it will accessable to you through out the application.
Q . I should be able to trigger everywhere in the application when this variable changes?
You can override the Add method of the Hashtable and do what you want
in that.

I hope it helps.
Regards,
Naveed Ahmad Bajwa
http://bajoo.blogspot.com/
 
P

Pieter

Thanks, but I dson't think it will help me any further:

Let's say that I create such a class, and I make an instance of it when the
application loads: MyClass.
How will I handle in every form/class the MyClass.Changed-event?
 
B

Branco Medeiros

Pieter said:
For a VB.NET 2005 application I need some kind Application Variable that
holds a value that can be accessed everywhere in the application. It should
also be available for a usercontrol in a seperate dll. Even more: I should
be able to trigger everywhere in the application when this variable changes.
<snip>

As Bajoo suggested, singleton is your friend.

You declare a class to provide the functionality you need, say DocList,
with appropriate events for when the list changes:

Public Class DocList
Public Event Change(ByVal Sender As Object, ByVal E As EventArgs)
' ...
' ...
' ...

Then, in the class body, you declare both a shared variable to
represent your singleton, as well as a shared method to give access to
it:

' In the DocList class body
Private Shared mGlobalDocList As New DocList

Shared ReadOnly Property GlobalDocList As DocList
Return mGlobalDocList
End Property

In every form you declare a withevents reference to a DocList class:

Private WithEvents GlobalDocList As DocList

Then upon form intialization, you set the reference to the actual
GlobalDocList:

Sub Form_Load(...) Handles MyBase.Load
'...
GlobalDocList = DocList.GlobalDocList
End Sub

Of course, every form must handle the events generated by the doclist:

Sub GlobalDocList_Change(...) Handles GlobalDocList.Change
'...
End Sub

And that's all there is to it.

Now, for the custom control in the DLL, I suggest you add a custom
property, say,

Public Property DocumentsAvailable As Boolean
'...
End Property

which you can set from inside the form that holds the control. This way
you keep your control independent from (and unaware of) the DocList.

HTH.

Regards,

Branco.

Disclaimer: the snippets above are pure Air Code (TM), and give no
warranties. ;-)
 
P

Pieter

Thanks! It seems that this is exactly what I want! I'll try it as fast as
possible!

Thanks a lot for the help!

Pieter
 

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