473,324 Members | 2,313 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Where to hook/unhook events with Forms/Controls

JS
I have an application with many user controls and forms which display
measurements in whatever the current unit system is. The current unit
system is kept in my 'Units' static class as Units.CurrentSystem. I
also have an event in the Units class called
Units.CurrentSystemChanged. My controls hook into this event (usually
in their OnLoad() method). However, I am not quite sure where to
unhook the event. I could do it in Dispose(), OnHandleDestroyed() or
somewhere else. I'd like to establish a pattern for doing this type
of thing in my application so if anyone has a comment I would
appreciate hearing it.

Mar 30 '07 #1
4 5332
On Mar 30, 1:52 pm, "JS" <jnospams...@gmail.comwrote:
I have an application with many user controls and forms which display
measurements in whatever the current unit system is. The current unit
system is kept in my 'Units' static class as Units.CurrentSystem. I
also have an event in the Units class called
Units.CurrentSystemChanged. My controls hook into this event (usually
in their OnLoad() method). However, I am not quite sure where to
unhook the event. I could do it in Dispose(), OnHandleDestroyed() or
somewhere else. I'd like to establish a pattern for doing this type
of thing in my application so if anyone has a comment I would
appreciate hearing it.
You have two choices.

1. The low-tech solution is to un-subscribe from the even in the
Form's Dispose method. As all Forms run with .Show() are Disposed when
the user closes them, and all code that uses .ShowDialog() is supposed
to Dispose the form afterward, this will work.

2. The high-tech solution is to subscribe to the event via a weak
reference delegate. You have to build this yourself (unfortunately),
but it allows your form to subscribe to the event and say, in effect,
"This subscription doesn't count where garbage collection is
concerned."

In your case, door #1 is probably the best. However, if you find your
forms subscribing to more and more static events (as I did) then the
second option becomes the simpler of the two.

Mar 30 '07 #2
JS
Thanks for the answer. I've searched your 'high-tech' suggestion of
weak references, but I still would like to try to do this in the 'low
tech' way, partly because I'm setting the pattern for future
developers and I'd like to keep the pattern simple, and also because I
don't fully understand the implications of the weak reference scheme
yet.

I am testing with a Form right now and am currently unhooking the
event in the Dispose() method. However, I notice that when the X
(close) button is pressed with the mouse, Dispose() is not called --
at least with ShowDialog(). It is called if I call Close() in my
code. That was not what I expected, and I will need to find the right
way to do this. Still, for forms, it looks like I can use OnLoad and
OnClosed, however I'm not sure what to do yet for controls.

For controls I was considering using OnHandleDestroyed, although I'm
concerned that this may be too late -- I would prefer that whenever my
user control gets the event, it can assume that its control and child
controls are created and all normal methods and properties (Text,
Invalidate() for example) will work. So, specifically for controls,
do you have any suggestions about when to hook/unhook events in the
'low-tech' way?

Thanks.

Mar 31 '07 #3
JS
Correction, Dispose() is not called when I call Close() in the code,
at least not right away. I suspect that Close(), as well as clicking
the close box, eventually lead to a Dispose() call.

Mar 31 '07 #4
On Mar 31, 10:36 am, "JS" <jnospams...@gmail.comwrote:
Thanks for the answer. I've searched your 'high-tech' suggestion of
weak references, but I still would like to try to do this in the 'low
tech' way, partly because I'm setting the pattern for future
developers and I'd like to keep the pattern simple, and also because I
don't fully understand the implications of the weak reference scheme
yet.

I am testing with a Form right now and am currently unhooking the
event in the Dispose() method. However, I notice that when the X
(close) button is pressed with the mouse, Dispose() is not called --
at least with ShowDialog().
If you use ShowDialog then you must always call Dispose() on the form
after you're finished with it. That is, the function that creates the
dialog and shows it must also dispose of it. The easiest way to do
this is via the using statement:

using (MyDialog dlg = new MyDialog())
{
DialogResult result = dlg.ShowDialog();
if (result == DialogResult.OK)
{
... get information from dlg and do something with it ...
}
}

dlg.Dispose() is automatically called when control leaves the using
statement.

If, on the other hand, you show your form using .Show(), Windows
should automatically call Dispose() on it when the user closes it.

Mar 31 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Luke | last post by:
I am trying to capture an event when the screensaver starts. I have code working on Windows XP etc when the Password/Welcome Screen is enabled . Using the Fast User Switching Notifications. ...
3
by: April | last post by:
Hi, I have a function that needs to run whenever a user clicks on any textbox, listbox or combobox on any input forms in my application (there are many forms). Background (in case someone has...
10
by: Drakier Dominaeus | last post by:
This is my first time posting here, so please forgive me if I do anything incorrectly. I've been learning C# and working with different things and decided I wanted to get into Multi-Threading....
8
by: CJack | last post by:
hy, I have an mdi application, i create a child form and I want to know when a button is pressed while that child form is loaded. I have this code: private void frmTestBaby_KeyUp(object sender,...
3
by: june | last post by:
Hi, I am coding for an application with dialog window. I need intercept mouse input. I need catch raw input, pretty much everything for WM_INPUT: such as Left/Middle/Right button down/up,...
4
by: Colin McGuire | last post by:
Hi again, thanks everyone for your previous help. But having resolved past problems, I'm moving on to new problems :( This one is a simple winforms application with two buttons, named Button1...
3
by: Lance | last post by:
I've noticed that controls that are contained in MDI child forms fail to raise MouseLeave events if the MDI child form's MdiParent property is set to Nothing (after it was set to an existing MDI...
0
by: zeng.hui.stephen | last post by:
I download the demo http://msdn.microsoft.com/msdnmag/issues/02/10/cuttingedge/. I inherite the demo, and write my code. I want to use Hook to monitor C++ Edit change. I use a C# form...
22
by: schneider | last post by:
I need to hook the system mouse down event. I'm trying to replicate how a context menu hides when the mouse clicks outside of the control. Thanks, Schneider
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.