Click here to Skip to main content
15,898,374 members
Home / Discussions / C#
   

C#

 
GeneralRe: Progmatically Creating TreeNodes and Child Nodes Pin
musefan27-Mar-09 6:34
musefan27-Mar-09 6:34 
GeneralRe: Progmatically Creating TreeNodes and Child Nodes Pin
maxatlis27-Mar-09 6:44
maxatlis27-Mar-09 6:44 
GeneralRe: Progmatically Creating TreeNodes and Child Nodes Pin
musefan27-Mar-09 6:49
musefan27-Mar-09 6:49 
GeneralRe: Progmatically Creating TreeNodes and Child Nodes Pin
maxatlis27-Mar-09 7:10
maxatlis27-Mar-09 7:10 
GeneralRe: Progmatically Creating TreeNodes and Child Nodes Pin
fly90427-Mar-09 7:54
fly90427-Mar-09 7:54 
AnswerRe: Progmatically Creating TreeNodes and Child Nodes Pin
Ian Shlasko27-Mar-09 9:58
Ian Shlasko27-Mar-09 9:58 
GeneralRe: Progmatically Creating TreeNodes and Child Nodes Pin
maxatlis27-Mar-09 13:20
maxatlis27-Mar-09 13:20 
AnswerHow To: - Progmatically Creating TreeNodes and Child Nodes [Answer] Pin
maxatlis27-Mar-09 14:37
maxatlis27-Mar-09 14:37 
Big, big thank you musefan and Ian! I’ve now got it working brilliantly.

Based on the pseudo-code Ian posted (which worked great after tweeking!) I’ve decided to post the working code incase anyone else out there is experiencing TreeNode’s from hell lol.


"ps, i'm not great with codeproject, so if you know of a better place for me to post this answer, please let me know."


Well here’s the working code… Big Grin | :-D Thumbs Up | :thumbsup:

Add a TreeView control to Form1 and name it treeView1. and then add this code..

<code>
private void Form1_Load(object sender, EventArgs e)
{
TreeNode tn = new TreeNode("Root");

// set PathSeparator to url paths
treeView1.PathSeparator = "/";

// Create any old folders and process
InsertURL(tn, @"mywebsite.com/home/about_us.aspx");
InsertURL(tn, @"mywebsite.com/home/contact_us.aspx");
InsertURL(tn, @"mywebsite.com/home/main.aspx");
InsertURL(tn, @"mywebsite.com/blogs/seo.aspx");
InsertURL(tn, @"mywebsite.com/blogs/linkchecks.aspx");
InsertURL(tn, @"mywebsite.com/blogs/howto/gettheresults.aspx");

//add nodes to treeView
treeView1.Nodes.Add(tn);
}

private void InsertURL(TreeNode root, string url)
{
InsertURL(root, SplitNodes(url), 0);
}

private string[] SplitNodes(string nodeString)
{
char[] delimiter = treeView1.PathSeparator.ToCharArray();
return nodeString.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
}

private void InsertURL(TreeNode parent, string[] urlParts, int index)
{
// Create the node if it doesn't exist.
CreateNode(parent, urlParts[index]);

// get the next urlPart
index++;
if (index < urlParts.Length)
{
InsertURL(parent.Nodes[urlParts[index-1]], urlParts, index);
}
}

private static void CreateNode(TreeNode treeNode, string name)
{
// Add the node if it don't exist
if (treeNode.Nodes[name] == null)
{
treeNode.Nodes.Add(name, name);
}
}</code>

<div class="ForumMod">modified on Saturday, March 28, 2009 5:57 AM</div>
QuestionOracle + #C DataBase Picture Pin
E_Gold27-Mar-09 5:17
E_Gold27-Mar-09 5:17 
QuestionNew Line VS2005 Installer's BodyText Pin
jp2code27-Mar-09 5:12
professionaljp2code27-Mar-09 5:12 
Questiondatabase in sql Pin
Mangesh Tomar27-Mar-09 4:39
Mangesh Tomar27-Mar-09 4:39 
AnswerRe: database in sql Pin
musefan27-Mar-09 4:47
musefan27-Mar-09 4:47 
GeneralRe: database in sql Pin
Mangesh Tomar27-Mar-09 5:41
Mangesh Tomar27-Mar-09 5:41 
GeneralRe: database in sql Pin
musefan27-Mar-09 5:48
musefan27-Mar-09 5:48 
QuestionCreating an .msg file Pin
MxIsMe27-Mar-09 2:56
MxIsMe27-Mar-09 2:56 
QuestionReceive "Now Playing" Song Information Pin
give.away27-Mar-09 2:52
give.away27-Mar-09 2:52 
AnswerRe: Receive "Now Playing" Song Information Pin
musefan27-Mar-09 4:52
musefan27-Mar-09 4:52 
GeneralRe: Receive "Now Playing" Song Information Pin
give.away27-Mar-09 5:01
give.away27-Mar-09 5:01 
GeneralRe: Receive "Now Playing" Song Information Pin
musefan27-Mar-09 5:09
musefan27-Mar-09 5:09 
GeneralRe: Receive "Now Playing" Song Information Pin
give.away27-Mar-09 5:39
give.away27-Mar-09 5:39 
GeneralRe: Receive "Now Playing" Song Information Pin
Eddy Vluggen27-Mar-09 5:16
professionalEddy Vluggen27-Mar-09 5:16 
GeneralRe: Receive "Now Playing" Song Information Pin
give.away27-Mar-09 5:33
give.away27-Mar-09 5:33 
GeneralRe: Receive "Now Playing" Song Information Pin
0x3c027-Mar-09 6:09
0x3c027-Mar-09 6:09 
QuestionRe: Receive "Now Playing" Song Information Pin
Eddy Vluggen27-Mar-09 8:19
professionalEddy Vluggen27-Mar-09 8:19 
GeneralRe: Receive "Now Playing" Song Information [modified] Pin
give.away27-Mar-09 14:29
give.away27-Mar-09 14:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.