Archive Browser
Download CustomTreeViewLabelEdit.zip, last updated 28/10/2013 (16.32 KB)
Download- md5: 80ba715b3ff9cc64851d7914f54af397
using System;
using System.ComponentModel;
using System.Windows.Forms;
namespace CustomTreeViewLabelEdit
{
internal class TreeViewEx : TreeView
{
// Specifying custom text when using the LabelEdit functionality of a TreeView
// http://cyotek.com/blog/specifying-custom-text-when-using-the-labeledit-functionality-of-a-treeview
#region Events
[Category("Behavior")]
public event EventHandler<NodeRequestTextEventArgs> RequestDisplayText;
[Category("Behavior")]
public event EventHandler<NodeRequestTextEventArgs> RequestEditText;
#endregion
#region Overridden Members
protected override void OnAfterLabelEdit(NodeLabelEditEventArgs e)
{
if (e.Label != null) // if the user cancelled the edit this event is still raised, just with a null label
{
NodeRequestTextEventArgs displayTextArgs;
displayTextArgs = new NodeRequestTextEventArgs(e.Node, e.Label);
this.OnRequestDisplayText(displayTextArgs);
e.CancelEdit = true; // cancel the built in operation so we can substitute our own
if (!displayTextArgs.Cancel)
e.Node.Text = displayTextArgs.Label;
}
base.OnAfterLabelEdit(e);
}
protected override void OnBeforeLabelEdit(NodeLabelEditEventArgs e)
{
NodeRequestTextEventArgs editTextArgs;
// get the text to apply to the label
editTextArgs = new NodeRequestTextEventArgs(e.Node, e.Node.Text);
this.OnRequestEditText(editTextArgs);
// cancel the edit if required
if (editTextArgs.Cancel)
e.CancelEdit = true;
// apply the text to the EDIT control
if (!e.CancelEdit)
{
IntPtr editHandle;
editHandle = NativeMethods.SendMessage(this.Handle, NativeMethods.TVM_GETEDITCONTROL, IntPtr.Zero, IntPtr.Zero); // Get the handle of the EDIT control
if (editHandle != IntPtr.Zero)
NativeMethods.SendMessage(editHandle, NativeMethods.WM_SETTEXT, IntPtr.Zero, editTextArgs.Label); // And apply the text. Simples.
}
base.OnBeforeLabelEdit(e);
}
#endregion
#region Members
/// <summary>
/// Raises the <see cref="RequestDisplayText" /> event.
/// </summary>
/// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
protected virtual void OnRequestDisplayText(NodeRequestTextEventArgs e)
{
EventHandler<NodeRequestTextEventArgs> handler;
handler = this.RequestDisplayText;
if (handler != null)
handler(this, e);
}
protected virtual void OnRequestEditText(NodeRequestTextEventArgs e)
{
EventHandler<NodeRequestTextEventArgs> handler;
handler = this.RequestEditText;
if (handler != null)
handler(this, e);
}
#endregion
}
}
Donate
This software may be used free of charge, but as with all free software there are costs involved to develop and maintain.
If this site or its services have saved you time, please consider a donation to help with running costs and timely updates.
Donate