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 / March 2008

Tip: Looking for answers? Try searching our database.

subscribing to events in controls in a separate window in another window ?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bill Woodruff - 02 Mar 2008 05:08 GMT
< .NET 2.0, VS Studio 2005 Ent., Win XP Pro SPII, all .NET Frameworks from
1.0 to 3.5 installed >

... note : normally I do not cross-post, but I felt this scenario justified
sending to both the controls and windowsforms groups ...

Hi,

1. Main form creates other top-level winform windows at run-time (new window
parent == null) from a pre-defined form template as :

secondaryFormTemplate newSForm = new secondaryFormTemplate();
// set formborderstyle, set to topmost, toplevel, etc.
newSForm.Show;

2. On each secondary winform you create there is treeview (which has public
accessor); you subscribe to the After_Select event of each Treeview IN THE
MAIN FORM with the same event handler in the main form like so :

newSForm.myTreeView.After_Select += new
System.Windows.Forms.TreeViewEventHandler(secondaryTVHitHandler);

3. so the handler for all secondary treeview selection in the main form
looks something like this :

private void secondaryTVHitHandler(object sender TreeViewEventArgs e)
{
// do whatever, get the node from e.Node, etc.
}

This compiles okay, seems to work okay most of the time, but once in a while
it appears that node selections in some of the secondary windows are not
reported to the subscriber in the main form.

So, I switched back to the more "classic" paradigm of having the secondary
form template implement an After_Select event which raises a public event,
which I then subscribe to in the main Form. Intermittent problems of some
selection events went away.

My question is : were the glitches I observed using the first technique
possibly a result of threading problems involving separate independent
top-level winforms ? Another hypothesis :  the .net TreeView (now, like me,
so long in the tooth :) is, as we all know, a wrapper around a COM control
... issues related to COM ?

My sense is that it is "best practice" to have objects raise public events
from their own internal private events, rather than have "alien objects"
subscribe directly to events of objects, but in the case where the secondary
objects (winforms in this case) are being created at run-time by a "master
object" it is "tempting" to just subscribe to their events there.

Appreciate any comments, recommendations, insights !

best, Bill Woodruff
dotScience
Chiang Mai, Thailand
Bob Powell [MVP] - 02 Mar 2008 13:21 GMT
Hi Bill,
This shouldn't be a threading issue because UI will all be on the same
thread unless you've done something out of the ordinary. To verify that it
isn't (or is) the handler should check InvokeRequired before modifying any
properties or calling any methods on the object that handles the event call.

private void secondaryTVHitHandler(object sender TreeViewEventArgs e)
{
if(((Control)sender).InvokeRequired)
   //access won't be threadsafe... invoke your own methods.
else
   // do whatever, get the node from e.Node, etc.
}

Signature

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

>< .NET 2.0, VS Studio 2005 Ent., Win XP Pro SPII, all .NET Frameworks from
>1.0 to 3.5 installed >
[quoted text clipped - 53 lines]
> dotScience
> Chiang Mai, Thailand

Rate this thread:







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.