Answer the question
In order to leave comments, you need to log in
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:
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question