0

I am working on a UserControl for selecting special files, in this control there is a TreeView which gets populated with nodes when user selects some file. Also user can remove the files from this treeview!

I am using this control in a wizard form. In this wizard form there is a button named buttonNext and this button is disabled by default.

How can I create an event for the treeview in the usercontrol that when it gets populated it notify the next button in wizard form to get enabled and if user removes all files from that treeview it notify the button to get disabled again.

P.S: Selecting files (browser dialog and stuff like that) are all done within this usercontrol, so in my wizard form I have no access to the things that is going on in this component, but only I set the TreeView itself as public so I can read its nodes in my wizard form.

I know how to subscribe to events but never created any event myself :(

1
  • 1
    Events Tutorial Commented Jul 11, 2012 at 9:27

6 Answers 6

1

Declare events on your CustomControl:

public event EventHandler DataPopulated; public event EventHandler DataRemoved; 

Common practice is creating protected virtual methods (for possible overriding them in descendant classes), named On<EventName> which will verify that event has attached handlers and raise event, passing required arguments:

protected virtual void OnDataPopulated() { if (DataPopulated != null) DataPopulated(this, EventArgs.Empty); } 

NOTE: If you need to pass some data to event handlers, then use generic EventHandler<DataPopulatedEventArgs> delegate as event type, where DataPopulatedEventArgs is a class, inherited from EventArgs.

Then just call this method just after your data was populated:

treeView.Nodes = GetNodes(); OnDataPopulated(); 

Then just subscribe to this event and enable your next button:

private void CustomControl_DataPopulated(object sender, EventArgs e) { buttonNext.Enabled = true; } 
Sign up to request clarification or add additional context in comments.

Comments

1

Who is the one populating the TreeView? The one loading the data on it could enable the Next button when it has finished the loading. Am I missing something?

By the way, you create an event like this:

public event EventHandler<EventArgs> YouEventName; 

And you call it like a method:

this.YourEventName(this,EventArgs.Emtpy); 

Best practices say that you should create a method to call it like this:

protected virtual void OnYourEventName() { if (this.YourEventName != null) { this.YourEventName(this, EventArgs.Empty); } } 

1 Comment

The you're right with your design. You should name your event something like OperationDone. Do you need to pass any parameter with your event?
1

Check out this MSDN article for a complete tutorial on how to create and fire events.
http://msdn.microsoft.com/en-us/library/aa645739(v=vs.71).aspx

Comments

1

You can just propogate the event of the Treeview.

You can add this to your custom control, and it will have a SelectedNodeChanged event.

public event EventHandler SelectedNodeChanged { add { tree.SelectedNodeChanged += value; } remove { tree.SelectedNodeChanged-= value; } } 

Comments

0

Creating a new event

public event EventHandler<EventArgs> myEvent; 

You then invoke it from some method

this.myEvent(sender, e); 

The actual event would look something like this:

protected void MyEvent(object sender, EventArgs e) { //Your code here } 

Comments

0

Your code can be like this:

public delegate void ChangedEventHandler(object sender, EventArgs e); class TreeViewEx : TreeView { ... public event ChangedEventHandler Changed; protected virtual void OnChanged(EventArgs e) { if (Changed != null) Changed(this, e); } } 

and it usage

 TreeViewEx tree = ... tree.Changed += new EventHandler(TreeChanged); // This will be called whenever the tree changes: private void TreeChanged(object sender, EventArgs e) { Console.WriteLine("This is called when the event fires."); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.