Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a TreeView From Custom Class Data?

Status
Not open for further replies.

Insider1984

Technical User
Feb 15, 2002
132
US
Okay so I have a class that consists of four variables:

int a
int b
string c
string d

I have created an array of this class and loaded it up with data.


first[arraycount] = new Test(a,b,c,d);

now for each unique variable in my test array I would like to create a treeview of it.

Example:

a1
=b1
==c1
==c2
==c3
=b2
==c1
==c2
==c3

basically a changes rarely, b changes frequently and c changes all the time.

I'm trying to move to C# from C++ and I'm in a pickle on how to even get started here or what to use.

I don't need the answer, just some guidance on how to get started.

Thanks for your help.



=====================
Insider
4 year 'on the fly' programmer
C++ Basic Java
 
I think I understand what I have to do, it's just that I have never done a treeview before.

here is my thoughts in sudo code:

Create Root Node:

Code:
			TreeNode rootNode = new TreeNode();
			rootNode.Text = "Group4";
			 Add a main root treenode.
			treeview_ini.Nodes.Add(rootNode);


then create my first node based on my variable 'a'

*BUT* I want to ensure the value of 'a' isn't already a node. If it is, I would want to select it and continue my node creation under it.

So my questions are:

How do I "navigate" in the code between different nodes in my treeview?
How can I search my nodes for a specific displayed value?
I really don't understand C++ or C# nodes so a basic help on that and knowing how to search/navigate nodes, would get me started.

Thank you very much for your help.




=====================
Insider
4 year 'on the fly' programmer
C++ Basic Java
 
The simple way to do it is by a for loop:
Code:
bool found = false
foreach ( TreeNode tmpNode in treeView1.Nodes)
{
    if (tmpNode.Text == "123")
    {
        found = true;
        break;
    }
    if (!found)
    {
	// Add the node
    }
}
Don't try to call treeView1.Nodes.Contains because you need to give a node to this function but you didn't find it yet.
If you write:
Code:
treeView1.Nodes.Add("123");
bool found = treeView1.Nodes.Contains(new TreeNode("123"));
found is false.
you can create a new class that inherit from treeview and do something else with the contain function, but it is more simple to do the loop above.
 
Okay I got it!

Code:
			TreeNode Tfov = null;
			TreeNode Timage = null;
			
			int count=0;
			for (int i = 0; i < arraycount; i++)    
			{
				if (i==0)
					Tfov = treeview_ini.Nodes.Add(first[i].fov);
				else
				{	//if our fov does not equal the one before it, create a new node
					if(first[i].fov != first[i-1].fov)
						Tfov = treeview_ini.Nodes.Add(first[i].fov);
				}
				//TreeNode Timage;
				if (i==0)
				{
					Timage = Tfov.Nodes.Add(first[i].image);
					Timage.Nodes.Add(first[i].test);
				}
				else
				{	//if our fov does not equal the one before it, create a new node
					if(first[i].image != first[i-1].image)
						Timage = Tfov.Nodes.Add(first[i].image);
					Timage.Nodes.Add(first[i].test);
				}
				count++;
			}

now my question is,

when someone double clicks on my Timage.Nodes in the treeview I'd like to send first.Test_ID to a function. I know how to send the variable to the function but do not know how to assign a certain Timage node to the specific ID.

Thanks again for your help.

=====================
Insider
4 year 'on the fly' programmer
C++ Basic Java
 
I thought you want to check if a node you want to add already exist, but you check if it is equal only to the previous node, not all nodes.
About your question, if you want to send the parent node that contains the image, you can call Node.Parent.
If you want to send some data, You can store the data in the Tag property in a node. The Tag can store everything.
 
Okay, last part! I set the tag to my "Test" object. Now when I double click on a Node I want to get information out of that tag.
Code:
		private void treeview_ini_DoubleClick(object sender, System.EventArgs e)
		{
			TreeNode tn = treeview_ini.SelectedNode;
			showdata(tn);
		}

		public void showdata(TreeNode tn)
		{
                  //I want to access tn.Tag and the "Test" object that it contains
               
		}

=====================
Insider
4 year 'on the fly' programmer
C++ Basic Java
 
Because the Tag is and object you need to cast it to your own type
Code:
private void treeview_ini_DoubleClick(object sender, System.EventArgs e)
        {
            TreeNode tn = treeview_ini.SelectedNode;
            showdata(tn.Tag);
        }

        public void showdata(Test tmpTest)
        {
                  tmpTest.anything
               
        }
Or
Code:
        public void showdata(TreeNode tn)
        {
                  ((Test)tn.Tag).anything
        }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top