Treeview for dashboards

I have a project with a user interface consisting of two panels (left and right).

In the left pane is a tree structure. Depending on the selected node, a different form is required in the right pane.

So far, I have defined a bunch of different “user controls” for the right panel, and I will create them and show them how it is required to select the node from the tree correctly.

Is there a "template" to control this process, since my code (too long to include here) is very fragile and not at all extensible. Anyone received any suggestions or even knew about an open source project that is reaching the same level.

+3
source share
1 answer

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.

+5
source

Source: https://habr.com/ru/post/1767566/


All Articles