Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Recently,I am using a open-source control which is named 'MagicLibrary.dll'. there are several questions occured.

I manual added a TreeView control into collections of contents which is belong DockManager, this content object instance is named 'notepad', following codes:

C#
Crownwood.Magic.Docking.DockManager _DockManager=null; //Declare a DockManager object
 
Crownwood.Magic.Docking.Content notepad = null; //Declare a Content object
 
// Function for Add a treeview control and display this treeview control in the content.
 
public void CreateTreeview()
 
{
 
  TreeView newTrview=new TreeView(); // Declare a treeview instance dynamically.
   
  newTrview.AfterSelect += new TreeViewEventHandler(MainFormtreeView_AfterSelect); //register treeview's after_select event
   
  notepad = _DockingManager.Contents.Add(newTrview, projectModel.newProjectName, null, 0); //Add treeview control into the contents
   
  notepad.DisplaySize = new Size(250, 200); //Set up default display size
   
  notepad.AutoHideSize = new System.Drawing.Size(250, 200);//Set up AutoHidePanel size
   
  _DockingManager.AddContentWithState(notepad, State.DockLeft);//

}

questions:

1.when the treeview control display first timen, the after_select event had been registered can be striked. however, after the treeview hide automatically and displayed second time, the after_select events can not be striked normally. so, why did above cases occur?


if you check anyone bug in above codes,let me know pls.
Posted
Updated 6-Feb-15 3:26am
v2

1 solution

From your question, it appears that you may be destroying the instance of the TreeView and then your application creates it again when it reappears. For example there is a problem here:
C#
public void CreateTreeview() 
{ 
    TreeView newTrview=new TreeView();
    ...
}
Your newTrview variable will go out of scope after this method completes and the control will be destroyed. You must be repeatedly creating it.

Declare your newtrview variable outside the CreateTreeview() method and then make a call to it like so:
C#
TreeView newTrview = null; // Declare it here.
public void CreateTreeview() 
{ 
    if(newTrview == null)
    {
        newTrview = new TreeView();
        // Attach your event handlers now...
        // Adjust your newTrview properties now too.
        ...
    }
}
That should fix your problem. Always instantiate a control to a variable defined at a higher scope. The if-statement should then prevent your application from repeated creating the same TreeView control.
 
Share this answer
 
v2
Comments
BillWoodruff 6-Feb-15 2:19am    
+5 A reasonable hypothesis as to the cause, and a well-written answer !
buick_123 10-Feb-15 0:26am    
ok, thank you very much BillWoodruff, your solution is very good, I had resolved that question based on your solution, I think your solution is similar as singleton pattern.
thanks very much again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900