It should not be difficult. Connect the TreeView on the left, add a panel and set the Dock to fill. Then use this code to select a user control in it:
private UserControl currentView;
public void SelectView(UserControl ctl) {
if (currentView != null) {
panel1.Controls.Remove(currentView);
currentView.Dispose();
}
if (ctl != null) {
ctl.Dock = DockStyle.Fill;
panel1.Controls.Add(ctl);
}
currentView = ctl;
}
You can get fancy in TreeView using reflection. In the constructor, set the node Name property to the name of the user control (for example, "UserControl1"). And fire the BeforeSelect event like this:
private void treeView1_BeforeSelect(object sender, TreeViewCancelEventArgs e) {
string name = e.Node.Name;
name = this.GetType().Namespace + "." + name;
Type ctlType = System.Reflection.Assembly.GetExecutingAssembly().GetType(name);
if (ctlType == null) e.Cancel = true;
else {
var ctor = ctlType.GetConstructor(new Type[] { });
var ctl = ctor.Invoke(null) as UserControl;
SelectView(ctl);
}
}
It's all. Change the code above if the user controls are in a different namespace or in a different assembly.
source
share