This is how I did it when using MDI. I used a Tree View control populated with A-Z subnodes would be names organized last to first with a comma and middle initial, and I would store the id of the record containing the information in the tag element of the Tree View like so.
private void updateTreeview()
{
//add letters sort nodes
for (int i = System.Convert.ToInt32('A'); i <= System.Convert.ToInt32('Z'); i++)
{
treeView1.Nodes.Add(Convert.ToString((char)(i)), Convert.ToString((char)(i))); //set key and text
}
//add customers
TreeNode childnode = null;
string sContactsName = null;
foreach (DataRow rowContacts in dtContacts.Rows)
{
sContactsName = string.Concat(rowContacts\["LastName"\].ToString(), ", ", rowContacts\["FirstName"\].ToString());
if (!(System.Convert.IsDBNull(rowContacts\["MiddleInitial"\].ToString())))
{
sContactsName = string.Concat(sContactsName, " ", rowContacts\["MiddleInitial"\].ToString(), ".");
}
//---------------------Key lines here --------------
childnode = new TreeNode(sContactsName, 0, 1);
childnode.Name = sContactsName; //set text as key
childnode.Tag = rowContacts\["ContactID"\].ToString();
//--------------------------------------------------
string sortnode = sContactsName.Substring(0, 1).ToUpper(); //gets alphabet sort node by first letter
treeView1.Nodes\[sortnode\].Nodes.Add(childnode);
}
//Update TreeView
treeView1.Visible = true;
treeView1.Enabled = true;
treeView1.ExpandAll();
}
After the node containing the name was clicked I handled it like this:
private void treeView1\_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
string nodeName = e.Node.Name;
if (nodeName.Length == 1)
{
return; //is alphabet sort node, exit
}
//see if child form already exists and show it
foreach (Form f in this.MdiChildren)
{
if (f.Name == nodeName)
{
f.Activate();
return;
}
}
str