|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace TreeIterator.Example
{
public partial class MainForm : Form
{
private const string _TextLabel = "Other surnames starting with {0} :";
public MainForm() {
InitializeComponent();
// Initialize all ToolTips
foreach(TreeNode node in Enumerate()) node.ToolTipText = node.Name;
// Initialize the ComboBox
FillComboBox();
// Initialize the text label
this.label4.Text = string.Format(_TextLabel, "");
}
/// <summary>Fill the combo box with all the surnames the tree contains.</summary>
private void FillComboBox() {
this.comboBox1.Items.AddRange(Enumerate().OrderBy(node => node.Text).Select(node=>node.Text).ToArray());
this.comboBox1.SelectedIndex = 0;
}
/// <summary>Returns an enumeration of the tree nodes, with a depth traversal.</summary>
/// <returns>An enumeration on the nodes.</returns>
private IEnumerable<TreeNode> Enumerate() {
DepthTreeEnumerator<TreeNode> enumerator = new DepthTreeEnumerator<TreeNode>(new SurnameTreeNode(this.treeView1.Nodes[0]));
foreach (SurnameTreeNode node in enumerator) yield return node.Node;
}
/// <summary>The user clicked the search button.</summary>
private void ButtonSearch_Click(object sender, EventArgs e) {
// Get the selected name
string selectedname = this.comboBox1.SelectedItem.ToString();
// Get the matching node
TreeNode resultnode = Enumerate().Single(node => node.Text == selectedname);
// Set the reult node text into the text box
this.textBox1.Text = resultnode.FullPath;
// Make the matching node visible
this.treeView1.CollapseAll();
Enumerate().Single(node => node.Text == selectedname).EnsureVisible();
// Initialize the text label
string letter = selectedname[0].ToString();
this.label4.Text = string.Format(_TextLabel, letter);
// Search all surnames starting with the same letter than the selected surname
StringBuilder sb = new StringBuilder();
foreach (TreeNode node in Enumerate().Where(node => node.Text.StartsWith(letter))) {
sb.Append(node.Text).Append("; ");
}
// Set the result string into the text box
this.textBox2.Text = sb.ToString();
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.