So i have this TreeView with 3 parent nodes, each one with some childs. There's a specific child that depends on others two childs in the same parent node. Here's the code i've done:
private void tvMorgan_AfterCheck(object sender, TreeViewEventArgs e)
{
if ((e.Node.Text.Contains("BRL/EUR")) && (e.Node.Parent.Text.Contains("FWD")))
{
TreeNode tnParent = e.Node.Parent;
tnParent.Nodes["BRL/USD"].Checked = true;
tnParent.Nodes["EUR/USD"].Checked = true;
}
}
When i run it, it can't find those two nodes i want to check.
Thanks in advance
As commentors pointed out, your nodes are most likely missing a name, which is the key used in the string index lookup. Easy to happen since most nodes get created at runtime.
Simple way to add the name:
myParent.Nodes.Add(new TreeNode("BRL/USD") { Name = "BRL/USD" });
myParent.Nodes.Add(new TreeNode("EUR/USD") { Name = "EUR/USD" });
Then your code should work:
if (tnParent.Nodes.ContainsKey("EUR/USD"))
tnParent.Nodes["EUR/USD"].Checked = true;
You should probably use the same logic on your if condition so that you are less reliant on the text property, which really should be only used for display purposes:
if ((e.Node.Name == "BRL/EUR") ...
If not setting the names, then you would have to search the node tree yourself to find the "text":
TreeNode found = tnParent.Nodes.OfType<TreeNode>().Where(x => x.Text.Contains("EUR/USD")).First();
if (found != null)
found.Checked = true;
Related
I am implementing a function to add some data to a tree view - MVP (application consists of Word ribbon (VSTO) + C# code base). Below is my code.
private ElementTreeNode LoadElement(Element element, ElementTreeNode parent = null)
{
ElementTreeNode loadElement;
ElementTreeNode node;
//Add a node to the TreeView that represents the element.
loadElement = Add(text: GetElementNodeText(element.Name), parent: parent);
loadElement.Element = element;
//Add a node to the TreeView for conditions
if (element.TSS.Conditions.Count > 0)
{
Add(text: "Conditions", parent: loadElement).Conditions = element.TSS.Conditions;
}
//Add a node to the TreeView control for each image within the element.
foreach (Image image in element.FrameSets.Active.Images)
{
Add(text: GetImageNodeText, parent: loadElement).Image = image;
}
//Call this procedure recursively to add any nested elements.
foreach (Element objElement in element.FrameSets.Active.Elements)
{
LoadElement(element: objElement, parent: loadElement);
}
return loadElement;
}
Add method :
private ElementTreeNode Add(string text, string key = "", ElementTreeNode parent = null)
{
try
{
TreeNode node = null;
//Create a random key if a key is not specified.
if (key.Length == 0)
{
key = "ElementTreeNode_" + GetRandomNumber();
}
//Add a node to the TreeView control.
if (parent == null) //..............point 1
{
node = treeView.Nodes.Add(key: key, text: text);
}
else // ..............point 2
{
node = parent.Node.Nodes.Add(key: key, text: text);
}
//Add a new ElementTreeNode object to this collection. It will have the same key as objNode.
ElementTreeNode objNode = new ElementTreeNode(node);
elementTreeNodesCollection.Add(key: key, value: objNode);
return objNode;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + Environment.NewLine + "ElementTreeNodes.Add","MyApp,
MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
}
Create the random number :
public long GetRandomNumber()
{
Random rnd = new Random();
return rnd.Next();
}
The issue is, in my test run
this code first add the parent element to the treeview and added into the collection (Dictionary<string, ElementTreeNode> elementTreeNodesCollection). In the next step, a 'conditions' tree node will be added as a sub node under parent element to the tree node. When I try to add it to the elementTreeNodesCollection, it gives below error.
An item with same key has already been added
The biggest issue is, if I put debug points at point 1 or before (no other debug points in whole codebase) within Add method, I am not receiving any errors and items have been added to the tree view and collection well.
But if I add the debug point at or after point 2, I receive the error. Also when there are no break points, then also I got this error.
I tried setting the key as an incrementing number and then the code ran fine. But I need to use a random number here, as the LoadElement method involves with recursion.
Is this issue is related with the random number generating function? or other case?
I am really stuck in here.
Does anybody have an idea what I am doing wrong in here?
Thank you in advance.
I strongly suspect that the problem is your GetRandomNumber() method, which depends on the current time. If you call it multiple times in quick succession, you'll end up getting the same number out of it.
If you wait a bit between calls - e.g. due to being paused at a breakpoint - you'll get different random numbers.
I'd suggest that the simplest option would be to make the number not random at all - keep a counter that you increment each time you need to create a new key. Or you could use Guid.NewGuid() and convert that to a string, or something similar.
If you really want to use Random, create a single instance of Random that you use for all the calls - but be aware that Random isn't thread-safe; if you need to do work across multiple threads, it all becomes a lot more complicated.
I have a TreeView and an associated ImageList. What are the steps to add images to the Parent and child nodes ?
All the nodes are being added from the code. Nothing is done from the Design.
public void fill_tree()
{
host_listbox_new.Items.Clear();
foreach (KeyValuePair<string, host_config> hlitem in host_list)
{
string sitem = hlitem.Key;
if (host_list[sitem].sessionOptions == null)
host_list[sitem].sessionOptions = new SessionOptions();
host_list[sitem].sessionOptions.Protocol = Protocol.Sftp;
host_list[sitem].sessionOptions.HostName = host_list[sitem].ip;
host_list[sitem].sessionOptions.UserName = host_list[sitem].username;
host_list[sitem].sessionOptions.Password = host_list[sitem].password;
host_list[sitem].sessionOptions.PortNumber = Convert.ToInt32(host_list[sitem].port);
//host_list[sitem].sessionOptions.SshHostKeyFingerprint = host_list[sitem].rsa;
if (treeView1.SelectedNode != null)
{
treeView1.SelectedNode.Nodes.Add(hlitem.Key.ToString());
}
else
{
treeView1.Nodes[0].Nodes.Add(hlitem.Key.ToString());
}
}
}
private void Parent_Load(object sender, EventArgs e)
{
read_process_config();
read_host_config();
host_listbox.Items.Clear();
treeView1.BeginUpdate();
treeView1.Nodes.Add("Servers");
fill_tree();
treeView1.EndUpdate();
treeView1.ExpandAll();
connect_server_bttn.Enabled = false;
}
i want to add items i.e child nodes to Server Parent node each of them having one image before them ( green image if hlitem.Value.connected is true. red image if hlitem.Value.connected is false)
But i have no idea about treeview or imagelist.
Can anyone help me about the whole thing?
The Add command returns a reference to the new Node. You can use it to style the Node.
Change your code to this:
if (treeView1.SelectedNode != null)
{
TreeNode tn =treeView1.SelectedNode.Nodes.Add(hlitem.Key.ToString());
tn.ImageIndex = yourIndex;
}
else
{
TreeNode tn =treeView1.Nodes[0].Nodes.Add(hlitem.Key.ToString());
tn.ImageIndex = yourIndex;
}
Or whatever logic you need to set the index.
If you need the parent node's index you could write:
tn.ImageIndex = tn.Parent.ImageIndex;
You may also want ot check out the other formats of the Add method. Some let you include the ImageIndex directly. You can also include the SelectedIndex; especially if you don't want that you should include it to prevent the Tree using its default SelectedIndex!
This will set the node to show the 2nd image, whether selected or not:
TreeNode tn =treeView1.Nodes[0].Nodes.Add(sitem, sitem, 1,1 );
Since you can't set a property of an object before you have created it, you can't set the Child nodes when you create the parent node. Instead you can use a simple function to do the changes:
void copyImgIndexToChildren(TreeNode tn)
{
if (tn.Nodes.Count > 0)
foreach (TreeNode cn in tn.Nodes) cn.ImageIndex = tn.ImageIndex;
}
void copyImgIndexToAllChildren(TreeNode tn)
{
if (tn.Nodes.Count > 0)
foreach (TreeNode cn in tn.Nodes)
{
cn.ImageIndex = tn.ImageIndex;
copyImgIndexToAllChildren(cn);
}
}
The first method changes the direct ChildNodes only , the 2nd recursively changes all levels below the starting node.
BTW: Is there a reason to use hlitem.Key.ToString() in your code instead of sitem?
At the moment I have a hardcoded treeview in a winform set up with the following code:
private void Form1_Load_1(object sender, EventArgs e)
{
TreeNode _leftCameraNode = new TreeNode("LeftCamera");
TreeNode _rightCameraNode = new TreeNode("RightCamera");
TreeNode[] stereoCameraArray = new TreeNode[] { _leftCameraNode, _rightCameraNode };
TreeNode _screenNode = new TreeNode("Screen");
TreeNode _cameraNode = new TreeNode("Camera", stereoCameraArray);
TreeNode[] headNodeArray = new TreeNode[] { _cameraNode };
TreeNode _headNode = new TreeNode("HeadNode", headNodeArray);
TreeNode[] centreNodeArray = new TreeNode[] { _screenNode, _headNode };
TreeNode _centreNode = new TreeNode("CentreNode", centreNodeArray);
// root node
TreeNode[] inmoRootNodeArray = new TreeNode[] { _centreNode };
TreeNode treeNode = new TreeNode("Main Node", inmoRootNodeArray);
treeView1.Nodes.Add(treeNode);
}
This works fine, however it is very limiting in its functionality. What I really want this to do is be populated from a list of nodes that I've set up and filled out with information that I'm getting from a bunch of text boxes on my form.
For example, I create a new list of screens (child of my node class) like so:
public List<HV_Screen> _screenList = new List<HV_Screen>();
Then I fill out my list with the data entered from my text box like this:
if (_selectedNode > -1)
{
Node n = _nodeList[_nodeList.Count - 1];
n.Name = _screenName;
}
I've been looking around on the net and on stackoverflow but I can't find anything to help me out with dynamically creating my treeview, so I was wondering if anyone would be able to point me in the right direction and help me out in how to do this?
The reason I want to do this is because, say I have 3 different instances of my screen, I want my treeview to show this, Then if I change the name in screen1 to MYSCREEN, I want my treeview to show this. But, should I click screen2, the treeview will still show the default name. Then, when I click back on screen1, its new name MYSCREEN will still be there on display.
I hope that makes sence.
Edit
Following on from a comment here is a screenshot of my current GUI:
The tree structure there has all be hard coded in with the code above. This takes in no information from my class screen class.
Now, my HV_Screen class looks as follows:
private string WIDTH;
private string HEIGHT;
public string Width
{
get { return WIDTH; }
set { WIDTH = value; }
}
public string Height
{
get { return HEIGHT; }
set { HEIGHT = value; }
}
public override List<XmlElement> GenerateXML(XmlDocument _xmlDoc)
{
List<XmlElement> elementList = new List<XmlElement>();
XmlElement _screenName = _xmlDoc.CreateElement("ScreenName");
_screenName.SetAttribute("Name", name.ToString());
_screenName.InnerText = name.ToString();
elementList.Add(_screenName);
XmlElement _screenTag = _xmlDoc.CreateElement("ScreenTag");
_screenTag.SetAttribute("Tag", tag.ToString());
_screenTag.InnerText = tag.ToString();
elementList.Add(_screenTag);
XmlElement _localPosition = _xmlDoc.CreateElement("LocalPosition");
_localPosition.SetAttribute("X", XPOS.ToString());
_localPosition.SetAttribute("Y", YPOS.ToString());
_localPosition.SetAttribute("Z", ZPOS.ToString());
_localPosition.InnerText = WorldPos.ToString();
elementList.Add(_localPosition);
XmlElement _orientation = _xmlDoc.CreateElement("Orientation");
_orientation.SetAttribute("Yaw", YAW.ToString());
_orientation.SetAttribute("Pitch", PITCH.ToString());
_orientation.SetAttribute("Roll", ROLL.ToString());
_orientation.InnerText = Orientation.ToString();
elementList.Add(_orientation);
XmlElement _width = _xmlDoc.CreateElement("Width");
_width.SetAttribute("Width", WIDTH.ToString());
_width.InnerText = WIDTH.ToString();
elementList.Add(_width);
XmlElement _height = _xmlDoc.CreateElement("Height");
_height.SetAttribute("Height", HEIGHT.ToString());
_height.InnerText = HEIGHT.ToString();
elementList.Add(_height);
return elementList;
}
Nothing too major. This simply takes in the information and writes it to an xml file. I plan on doing this for each of my Node children. So, if I was to have a camera class, it would have its own generate XML function and write it out from there. My base class (node) simply has a bunch of getters and setters for things like name, tag etc.
With the way the code is set up at the moment, I can add in new nodes. First I must select the root node I want to branch from, then I fill out the information associated with it. So in that screen shot, if I was to select Main Node and then hit the Add button, Sample Name would now be a child of Main Node. However, if I was to select Sample and Name and change it, nothing would happen.
This way is also hard coded in to only relate to my screen class. In theory, I could simply turn a lot of different text boxes on and off but this would be really bad practise. And I'm pretty sure it will have an impact on my programs performance.
So again, what I want is one Name text box that will be in charge of filling out the name for every object that requires a name. Then, I wish to take the name I entered and have it fill out the treeview. Once I select it, I want my selected node to fill out the textboxes with all of the associated text I put in for it. But, should I change the name of my selected node, it will also change the name in the tree structure.
Again, sorry if that is confusing. I'm pretty beat at the moment.
I need to construct a huge treeview from a composite database table with Grouping.
Grouping is, what we see in SQL Server Management Studio Express. After a Database node, some fixed folders are shown (like, Database Diagrams, Tables, Views, Synonyms, Programmability and Security) and children are grouped in those folders.
Up to this point I have used AfterSelect event and handler to achieve this.
But the problem with AfterSelect is, before selecting the node, the viewer is not able to know whether there is any child available. This is because, the expandable plus sign is not visible.
I want to use BeforeExpand. But the problem with BeforeExpand is, it works if the children are already populated. In that case, when I click groups, nothing happens.
How to solve this?
So codes/web-link will be appreciated.
What I usually do is to add a "dummy child node" wherever there may be children that should be loaded in a lazy manner. This will make the parent have the plus sign, and then you can add code to the AfterExpand event where you do the following:
Check if there are are exactly one child, and if that child is the dummy node (you can use the Tag property to identify the dummy node)
If the dummy node is found, launch a search to get the children and add them to the parent node, finish it off by removing the dummy node.
I typically give the dummy node a text like "Loading data. Please wait..." or so, so that the user gets some info on what is going on.
Update
I put together a simple example:
public class TreeViewSample : Form
{
private TreeView _treeView;
public TreeViewSample()
{
this._treeView = new System.Windows.Forms.TreeView();
this._treeView.Location = new System.Drawing.Point(12, 12);
this._treeView.Size = new System.Drawing.Size(200, 400);
this._treeView.AfterExpand +=
new TreeViewEventHandler(TreeView_AfterExpand);
this.ClientSize = new System.Drawing.Size(224, 424);
this.Controls.Add(this._treeView);
this.Text = "TreeView Lazy Load Sample";
InitializeTreeView();
}
void TreeView_AfterExpand(object sender, TreeViewEventArgs e)
{
if (e.Node.Nodes.Count == 1 && e.Node.Nodes[0].Tag == "dummy")
{
// this node has not yet been populated, launch a thread
// to get the data
ThreadPool.QueueUserWorkItem(state =>
{
IEnumerable<SomeClass> childItems = GetData();
// load the data into the tree view (on the UI thread)
_treeView.BeginInvoke((Action)delegate
{
PopulateChildren(e.Node, childItems);
});
});
}
}
private void PopulateChildren(TreeNode parent, IEnumerable<SomeClass> childItems)
{
TreeNode child;
TreeNode dummy;
TreeNode originalDummyItem = parent.Nodes[0];
foreach (var item in childItems)
{
child = new TreeNode(item.Text);
dummy = new TreeNode("Loading. Please wait...");
dummy.Tag = "dummy";
child.Nodes.Add(dummy);
parent.Nodes.Add(child);
}
originalDummyItem.Remove();
}
private IEnumerable<SomeClass> GetData()
{
// simulate that this takes some time
Thread.Sleep(500);
return new List<SomeClass>
{
new SomeClass{Text = "One"},
new SomeClass{Text = "Two"},
new SomeClass{Text = "Three"}
};
}
private void InitializeTreeView()
{
TreeNode rootNode = new TreeNode("Root");
TreeNode dummyNode = new TreeNode("Loading. Please wait...");
dummyNode.Tag = "dummy";
rootNode.Nodes.Add(dummyNode);
_treeView.Nodes.Add(rootNode);
}
}
public class SomeClass
{
public string Text { get; set; }
}
It's standard behaviour for a tree to show a "+" in front of every folder/group, and the plus dissapears when clicked on if it's found to have no children, this saves the expensive "do you have children" check.
Alternatively you can provide this information if you have a cheap way of determining if a node has children. This question provides more information.
I'm busy with a simple application. It reads xml and puts the information in a treeview.
I do this by creating TreeNodes and nest them, and finaly, return the root treenode. Because I want to show some extra information when a treenode is selected, I put the information in the tag property of the TreeNode. In this way, I should be able to retrieve the information when the node is selected.
But when I try to retrieve the information in the Tag property, it says the value = null.
Here is the code where I fill the tag. This is in a function which is recursively used to read the XML dom. treeNode is a paramater given to this function.
if (treeNode.Tag == null)
{
treeNode.Tag = new List<AttributePair>();
}
(treeNode.Tag as List<AttributePair>).Add(new AttributePair(currentNode.Name, currentNode.Value));
This is the event where a treenode is selected
private void tvXML_AfterSelect(object sender, TreeViewEventArgs e)
{
if (tvXML.SelectedNode.Tag != null)
{
}
if (e.Node.Tag != null)
{
}
}
Both values evaluate to null. How can I solve this problem?
The code you posted should work as-is. Something else in your code, code that you didn't post here, is causing this to break. It could be clearing the Tag, it could be a data binding set on the tag, etc.
Without seeing all your code, the best I can do is guess and help you isolate the problem.
Here's what I'd do: setup Visual Studio to allow stepping into the .NET framework source code with the debugger. Then, set a breakpoint on the setter for the TreeNode.Tag property. After you set the tag in your code to your AttributePair List, see when it gets set again. The breakpoint will hit, you'll look at the stack trace and see what exactly is clearing your Tag property.
If using Tag property isn't in principle, I'm recommend inherit TreeItem:
public class MyTreeNode : TreeNode
{
public List<AttributePair> list;
public MyTreeNode (string text,List<AttributePair> list) : base(text)
{
this.list = list;
}
//or
public MyTreeNode (string text) : base(text)
{
this.list = new List<AttributePair>();
}
}
And use it:
private void tvXML_AfterSelect(object sender, TreeViewEventArgs e)
{
if (tvXML.SelectedNode is MyTreeNode)
{
MyTreeNode selectedNode = tvXML.SelectedNode as MyTreeNode;
selectedNode.list.Add(.., ..);
}
if (e.Node is MyTreeNode)
{
MyTreeNode node = e.Node as MyTreeNode;
node.list.Add(.., ..);
}
}
Maybe you are assigning the values after Select event. Otherwise you can maintain a dictionary of TreeNode and tag values as workaround.
Try declaring/initialising your List object somewhere above (outside of the inner scope you are in) and when you assign to the .tag property - don't create a new list but rather assign previously created List object.
private TreeViewItem _subsender;
private object _senderTag;
public TreeViewItem _sender
{
get {
return _subsender;
}
set
{
_senderTag = value.Tag;
_subsender = value;
}
}
Got the same problem this the solution that i found
Just don't use the .tag but _senderTag
(don't change the lines in the set for some reason :D )
(You cant just reset the tag (maybe new TreeViewItem ))