Challenges making a simple UI in a C# program... guidance much appreciated

In brief: made a C# program that fills a TreeView control with info parsed from a file and displays it with PanelManager.DisplayControl(treeView... - works wonderfully (IMHO) - and employs treeView.DrawMode = TreeViewDrawMode.OwnerDrawText and DrawNode += new DrawTreeNodeEventHandler(DrawNode) to make it render nicely.

Now I decide that I need to add buttons so that I can reload the file and redisplay it ... as well as do other things. So I make some buttons using PanelManager.StackWpfElement(loadButton...... and when I click the button, it makes another Panel with a TreeView control...

Try as I might, I can't seem how to figure out how to re-use or delete/recreate a TreeView on its own panel so that there is a single "TreeView panel" after subsequent requests to reload. I am hesitant to recode the TreeView using a wpf control due the the time invested so far, and the challenges that I have overcome with that .. especially if the outcome is going to be bleak.

Many thanks in advance.
(Yet another fan on LINQPad)


  • edited July 31

    The StackWpfElement method is a convenient shortcut that when called repeatedly, adds WPF controls to a stack panel. It works only with WPF controls, so if you need to include a WinForms control, you will need to first wrap it in a WindowsFormsHost.

    Note that you don't have to use StackWpfElement - you can instead create your own containing panel and dump it. This can be either WinForms or WPF. For example, the following displays a TreeView and buttons using WinForms:

    var tv = new TreeView { Dock = DockStyle.Fill };
    int fontHeight = SystemFonts.DefaultFont.Height;
    var buttonsPanel = new Panel { Dock = DockStyle.Bottom, Height = fontHeight * 3, Padding = new Padding (fontHeight / 3) };
    var button = new Button { Text = "Update Nodes", AutoSize = true, Dock = DockStyle.Left };
    button.Click += (sender, args) => PopulateNodes();
    buttonsPanel.Controls.Add (button);
    var mainPanel = new Panel();
    mainPanel.Controls.Add (buttonsPanel);
    mainPanel.Controls.Add (tv);
    void PopulateNodes()
        tv.Nodes.Add ("Now").Nodes.Add (DateTime.Now.ToString());

    If you prefer to use WPF buttons instead of WinForms buttons (while using a WinForms TreeView), make buttonsPanel a System.Windows.Forms.Integration.ElementHost instead of a Panel - then you can assign a WPF control to its Child property.

Sign In or Register to comment.