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.
Related
Here i am trying to show the last selected node after reload of treeview but the selected node is always null. I am able to find the tree node from the treeview node collection but not able to assign it to tree view. Please suggeest:
public void LoadTreeViewData()
{
treeView.Nodes.Clear();// Clear any existing items
treeView.BeginUpdate(); // prevent overhead and flicker
LoadBaseNodes();//Populate all nodes
treeView.EndUpdate();
treeView.Refresh();
treeView.ExpandAll();
if (!string.IsNullOrEmpty(m_oSelectedNode_NAME))
{
TreeNode[] treeNodes = treeView.FlattenTree()
.Cast<TreeNode>()
.Where(r => Convert.ToDecimal(r.Tag) == Convert.ToDecimal(m_oSelectedNode_NAME))
.ToArray();
treeView.SelectedNode = treeNodes[0];
treeView.Focus();
}
}
This problem is happening because i am trying to Update the selected node inside the Tree view after label edit event. I put a timer inside After label edit to resolve the issue and set the Selected node it works for me.
var tnm = new Timer() { Enabled = true, Interval = 30 };
tnm.Tick += delegate {
SetSelectioNode();
tnm.Dispose();
};
I am having trouble type casting and using the right interfaces. I have a function that loops through all text elements with a specific property set. Once a match is found, I want to be able to select this text element or make it active or highlighted or whatever on the map. Code below.
protected override void OnClick(Item item)
{
IMxDocument pDoc = ValidateInterface.GetMxDocument();
IActiveView pLayout = (IActiveView)pDoc.PageLayout;
IGraphicsContainer pGraphicsCont = (IGraphicsContainer)pDoc.PageLayout;
pGraphicsCont.Reset();
IElementProperties _ElemProps = null;
while ((_ElemProps = (IElementProperties)pGraphicsCont.Next()) != null)
{
if (_ElemProps.CustomProperty is IPropertySet2)
{
ITextElement _textElement = (ITextElement)_ElemProps;
IPropertySet2 _propertySet = (IPropertySet2)_ElemProps.CustomProperty;
MessageBox.Show("Before I compare item to string");
if (item.Caption == Convert.ToString(_propertySet.GetProperty(NAME_STRING)))
{
//Problems start here
MessageBox.Show("Inside the IF statement");
IGraphicsContainerSelect _SelectMyElement = null; // = (IGraphicsContainerSelect)pGraphicsCont;
ITextElement _newTextElement = (ITextElement)pGraphicsCont;
IElement TestElement = _newTextElement as IElement;
_SelectMyElement.SelectElement(TestElement);
}
}
}
}
Nothing is being selected though on my map. I'm looping through each graphic element (IGraphicContainer) on the map. Once I find a match, I want to select that Graphic Element. I am trying to utilize IGraphicContainerSelect to do this. It takes an IElement variable type as a parameter, thus why I'm trying to cast it. But again, noting is being selected. This all happens when I click a button.
Any help on this would be greatly appreciated. Thanks in advance.
Try to refresh your map.
I think the better way using pLayout.PartialRefresh or pScreenDisplay.Invalidate methods.
pLayout.PartialRefresh(esriViewDrawPhase.esriViewGraphicSelection,null,pLayout.Extent)
OR
IScreenDisplay pScreenDisplay = pLayout.ScreenDisplay;
pScreenDisplay.Invalidate(null,false,esriViewGraphicSelection);
pScreenDisplay.UpdateWindow();
See help :http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#//000100000242000000
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;
When I tried using the imagelist in treeview, the image index changes when treenode is clicked. I have no idea why it is happening. Can anyone help me?
Thanks in advance
You need to set both the ImageIndex and the SelectedImageIndex on the tree node.
'SelectedImageIndex's intent is to allow displaying a different image upon selection than what is set by the 'ImageIndex' for a particular node. To keep these two consistent it is necessary to set them to the same value. This can be done at design time or programmatically depending on your needs.
For example, if the images never change then it is as simple as setting them concurrently when a new node is added to the TreeView:
int myCurrentImageIndex = 0;
TreeNode node = myTreeView.Nodes.Add("new node!");
node.ImageIndex = node.SelectedImageIndex = myCurrentImageIndex;
However, if you do change the ImageIndex value for any reason after its initial creation (such as a response to some kind of user action), then you must also change the SelectedImageIndex as well. Otherwise, they will become inconsistent.
int myNewImageIndex = 1;
node.ImageIndex = node.SelectedImageIndex = myNewImageIndex;
(Note it is not enough to set them to be the same in the event handler of the 'AfterSelect' event. It must be done anywhere in your code where ImageIndex changes.)
you can directly do it in the constructor :
TreeNode node = new TreeNode("My treenode", 1, 1);
TreeNode tn = new TreeNode();
tn.Text = "NewRecord";
tn.ImageIndex = 1;
treeView.SelectedNode.Nodes.Add(tn);
treeView.SelectedNode = tn;
treeView.SelectedNode.SelectedImageIndex = tn.ImageIndex; // <--- Problem solved
tn.BeginEdit();
Just Add this line:
Node.SelectedIndex:=Node.ImageIndex;
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.