How to add a button next to each node TreeView?

How to add a button next to each node TreeView?

+8
source share
3 answers

Adding a button next to each node in the tree structure is difficult. You will have to process the drawing of the tree structure yourself and either press the buttons yourself, or emulate their functionality, or create child button controls and display them in the right places in the control tree, and then process their repositioning when scrolling the controls, etc. In any case, it will be a nightmare.

Fortunately, there is a simple way out: you do not need to do any of these complex things, BECAUSE YOU SHOULD NOT MAKE A CHILD!

Have you ever seen a control tree with buttons in it? Not. Therefore, if your tree control has buttons in it, it will be seen by your end users as fancy.

What you need to do is to consider how other applications have solved the problem you are trying to solve without using the tree controls with buttons in them and doing the same thing as them.

+12
source

Here is a site where I found the project source code on codeproject, where someone really did what you are trying to do .. hope this helps you

How to place buttons inside a tree image This is a CodeProject link in which the project actually has a source for executing the working project. good luck

+3
source

The easiest way to do this is to draw a tree yourself. Here is a small example (note that PushButtonState is inside the System.Windows.Forms.VisualStyles namespace):

public class CustomTreeView : TreeView { private Rectangle buttonRect = new Rectangle(80, 2, 50, 26); private StringFormat stringFormat; public CustomTreeView() { SetStyle(ControlStyles.OptimizedDoubleBuffer, true); DrawMode = TreeViewDrawMode.OwnerDrawText; ShowLines = false; FullRowSelect = true; ItemHeight = 30; stringFormat = new StringFormat(); stringFormat.Alignment = StringAlignment.Near; stringFormat.LineAlignment = StringAlignment.Center; } protected override void OnDrawNode(DrawTreeNodeEventArgs e) { e.Graphics.DrawString(e.Node.Text, this.Font, new SolidBrush(this.ForeColor), e.Bounds, stringFormat); ButtonRenderer.DrawButton(e.Graphics, new Rectangle(e.Node.Bounds.Location + new Size(buttonRect.Location), buttonRect.Size), "btn", this.Font, true, (e.Node.Tag != null) ? (PushButtonState)e.Node.Tag : PushButtonState.Normal); } protected override void OnNodeMouseClick(TreeNodeMouseClickEventArgs e) { if (e.Node.Tag != null && (PushButtonState)e.Node.Tag == PushButtonState.Pressed) { e.Node.Tag = PushButtonState.Normal; MessageBox.Show(e.Node.Text + " clicked"); // force redraw e.Node.Text = e.Node.Text; } } protected override void OnMouseDown(MouseEventArgs e) { TreeNode tnode = GetNodeAt(e.Location); if (tnode == null) return; Rectangle btnRectAbsolute = new Rectangle(tnode.Bounds.Location + new Size(buttonRect.Location), buttonRect.Size); if (btnRectAbsolute.Contains(e.Location)) { tnode.Tag = PushButtonState.Pressed; tnode.Text = tnode.Text; } } } 

In addition, you can achieve this even without creating a custom control - just add these event handlers to the standard TreeView

+2
source

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


All Articles