wiring events

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Last month Bruce Wood responded to my question as below. Can some one give
me a little more detail how to "connect up" as mentioned here.

Bruce, are you there? Help again, please.

1. If the purpose of MyClass is to interact tightly with a tree view
and nothing but a tree view, and that's really all it does, it might be
appropriate to pass the tree view instance into MyClass and have it
connect up its own event handlers to the events of interest from the
tree view. This is a very tight coupling, and you should use it only
when MyClass and the tree view are tightly related, one-to-one, and
MyClass really doesn't mean anything without a companion tree view. Or,
preferably,
 
OK. Got it.

This is what I was trying to do. Please advise if there is any risk. Seems
to be working.

theTv is a reference to actual treeview that will exist at runtime and this
code wil be in a user control.

ttheTv is a reference to actual treeview that will exist at runtime.

this.theTV.AfterSelect +=
new
System.Windows.Forms.TreeViewEventHandler(this.tabPage1_theTVAfterSelect);

and then have the following....

private void tabPage1_theTVAfterSelect(object sender,
System.Windows.Forms.TreeViewEventArgs e)
{
MessageBox.Show(e.Node.Text + " TreeView after select trigerred");
}
 
Yes, that's the idea.

I'm assuming that you're passing the tree view into your user control
either on the constructor (if the tree view to which it's connected
will never change) or via a property or something:

public class MyUserControl...
{
private TreeView theTV;
public TreeView AssociatedTreeView
{
get { return this.theTV; }
set
{
if (this.theTV != null)
{
this.theTV.AfterSelect -=
new
System.Windows.Forms.TreeViewEventHandler(this.tabPage1_theTVAfterSelect);
}
this.theTV = value;
if (this.theTV != null)
{
this.theTV.AfterSelect +=
new
System.Windows.Forms.TreeViewEventHandler(this.tabPage1_theTVAfterSelect);
}
}
}
}

Sorry about the long delay. Vacation and all that. :)
 
Back
Top