You can easily add as-you-type spell checking to the .NET TreeView control by substituting the standard editing textbox with RapidSpell Desktop's AYTTextBox. 1. Add this basic helper class to the project public class AYTTreeViewHelper { Keyoti.RapidSpell.AYTTextBox subTextBox; Keyoti.RapidSpell.RapidSpellAsYouType ayt; TreeNode editingNode; TreeView tv; public AYTTreeViewHelper(TreeView tv, IContainer components) { ayt = new Keyoti.RapidSpell.RapidSpellAsYouType(components); subTextBox = new Keyoti.RapidSpell.AYTTextBox(); subTextBox.Leave += new EventHandler(subTextBox_Leave); subTextBox.TextChanged += new EventHandler(subTextBox_TextChanged); ayt.TextComponent = subTextBox; subTextBox.Visible = false; tv.Controls.Add(subTextBox); tv.BeforeLabelEdit += new NodeLabelEditEventHandler(tv_BeforeLabelEdit); tv.Disposed += new EventHandler(tv_Disposed); this.tv = tv; } void tv_Disposed(object sender, EventArgs e) { subTextBox.Leave -= new EventHandler(subTextBox_Leave); subTextBox.TextChanged -= new EventHandler(subTextBox_TextChanged); tv.BeforeLabelEdit -= new NodeLabelEditEventHandler(tv_BeforeLabelEdit); } void tv_BeforeLabelEdit(object sender, NodeLabelEditEventArgs e) { e.CancelEdit = true; editingNode = e.Node; Rectangle r = e.Node.Bounds; subTextBox.Font = e.Node.TreeView.Font; subTextBox.Text = e.Node.Text; subTextBox.Location = r.Location; subTextBox.Size = r.Size; if (subTextBox.Width + subTextBox.Left > tv.Width - 10) subTextBox.Width = tv.Width - 10 - subTextBox.Left; else if (r.Size.Width < 50) subTextBox.Width = 52; subTextBox.Visible = true; editingNode.Text = ""; subTextBox.Focus(); }
//To autoexpand the textbox as the user types void subTextBox_TextChanged(object sender, EventArgs e) { Graphics gfx = subTextBox.CreateGraphics(); SizeF sf = gfx.MeasureString(subTextBox.Text, subTextBox.Font); if (sf.Width + subTextBox.Left > tv.Width - 10) subTextBox.Width = tv.Width - 10 - subTextBox.Left; else { if (sf.Width > 50) subTextBox.Width = (int)sf.Width + 12; else subTextBox.Width = 52; } gfx.Dispose(); } //Hide the ayt tb when done void subTextBox_Leave(object sender, EventArgs e) { editingNode.Text = subTextBox.Text; subTextBox.Visible = false; } } 2. Initialize the object and pass the TreeView control, to activate it, eg. in the Form's constructor. public partial class TreeviewForm : Form { public TreeviewForm() { InitializeComponent(); AYTTreeViewHelper helper = new AYTTreeViewHelper(treeView1, components); } } Where, 'treeView1' is the TreeView control to work with. Any problems? Please email support @ keyoti.com. |