A
A
Alex Gorbunov2020-05-09 00:34:44
C++ / C#
Alex Gorbunov, 2020-05-09 00:34:44

How can I implement TreeView data binding to RichTextBox?

I'm building a dialog editor for a game here and I've got a problem that I can't seem to solve. How can something like this be implemented:

  1. There are two together elements on the form RichTextBox and one TreeView.
  2. Nodes are added to the TreeView using the button.
  3. When you click on a node, the first RichTextBox is activated, when you click on its subnode, the second RichTextBox is activated (the first one is disabled).
  4. You can enter something into this RichTextBox and it will be saved in the selected node/subnode (not in two at once).
  5. If the user clicks on another node or their subnode, the RichTextBox will be cleared, but when the user clicks on that previous node again, the previously entered data will reappear in the RichTextBox.
  6. And so you can bind data to any selected node.
  7. I will be able to get the data of each node through the code in the future.

I am most interested in points 4,5,6 and 7 . I have already implemented the rest, but with these I have no idea how. I know about the existence of Tag in nodes, but this does not give me any idea of ​​​​the implementation of such. Thanks in advance for your reply.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Korotenko, 2020-05-09
@Naratzull

The first 2 classes store data, load it from the database, well, you can think of it yourself. See LoadData
The last one is the form handles the NodeMouseClick and TextChanged events on text boxes.
The logic is simple, but this is generally an example, in reality, complicate to taste.

using System.Collections.Generic;

namespace TreeViewBinding.Code
{
    public class TreeSource
    {
        public TreeSource()
        {
            Leaves = new List<TreeLeaf>();
        }
        public List<TreeLeaf> Leaves { get; set; }

        /// <summary>
        /// Load data stub. In this example use mock data. In real application load data from database or web service
        /// </summary>
        public void LoadData()
        {
            var first = new TreeLeaf { Description = "First description", Title = "First" };
            var firstSubs = new[]
            {
                new TreeLeaf(first){Description = "First desc",Title = "First"},
                new TreeLeaf(first){Description = "Second desc",Title = "Second"},
                new TreeLeaf(first){Description = "Third desc",Title = "Third"}
            };
            first.Leaves.AddRange(firstSubs);
            var second = new TreeLeaf { Description = "Second description", Title = "Second" };
            var third = new TreeLeaf { Description = "Third description", Title = "Third" };
            Leaves.AddRange(new[]
            {
                first,
                second,
                third,
            });
        }
    }
}

using System.Collections.Generic;

namespace TreeViewBinding.Code
{
    public class TreeLeaf
    {
        public TreeLeaf(TreeLeaf parent = null)
        {
            Leaves = new List<TreeLeaf>();
            Parent = parent;
        }
        public int Id { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public string Icon { get; set; }
        public List<TreeLeaf> Leaves { get; private set; }
        public TreeLeaf Parent { get; private set; }
    }
}

#region License
// // Разработано: Коротенко Владимиром Николаевичем (Vladimir N. Korotenko)
// // email: [email protected]
// // skype:vladimir-korotenko
// // https://vkorotenko.ru
// // Создано:  09.05.2020 8:26
#endregion

using System.Collections.Generic;
using System.Windows.Forms;
using TreeViewBinding.Code;

namespace TreeViewBinding
{
    public partial class TreeViewBindingForm : Form
    {
        private TreeSource _treeSource;
        private TreeLeaf _currentLeaf;
        private TreeNode _node;
        public TreeViewBindingForm()
        {
            InitializeComponent();
            _treeSource = new TreeSource();
            _treeSource.LoadData();
            FillInTree(treeView.Nodes, _treeSource.Leaves);
        }

        private static void FillInTree(TreeNodeCollection  nodes, IEnumerable<TreeLeaf> leafs)
        {
            foreach (var leaf in leafs)
            {
                var treeNode = new TreeNode(leaf.Title) {Tag = leaf};
                nodes.Add(treeNode);
                if (leaf.Leaves.Count > 0) FillInTree(treeNode.Nodes, leaf.Leaves);
            }
        }

        private void treeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node.Tag is TreeLeaf item)
            {
                _node = e.Node;
                _currentLeaf = item;
                titleTextBox.Text = item.Title;
                descriptionTexBox.Text = item.Description;
            }
        }

        private void titleTextBox_TextChanged(object sender, System.EventArgs e)
        {
            if (_node != null)
            {
                _currentLeaf.Title = _node.Text = titleTextBox.Text;
            }
        }

        private void descriptionTexBox_TextChanged(object sender, System.EventArgs e)
        {
            if (_node != null)
            {
                _currentLeaf.Description = descriptionTexBox.Text;
            }
        }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question