So in my project i would like have a nice treeview that has images.
but when i run the program the image was not shown. Any idea?
here is my code:
while (thisreader.Read()) // i read from the database.
{
TreeNode tn = new TreeNode();
tn.Text = thisreader["channel_name"].ToString();
tn.ImageIndex = 1;
tn.SelectedImageIndex = 1; // i have my imagelist1 in the corresponding winform
treeView1.Nodes.Add(tn);
}
The ImageList must be assigned to the ImageList property of the TreeView as well. Simply dropping it on the form is not enough.
Related
I have a menu structure as seen in picture below:
I want to add images to all submenu-items under the controllerToolStripMenuItem, ie openToolStripMenuItem, openInNewWindowToolStripMenuItem... ImageList contains my images to use and menuStrip1 gets a reference to the list of images :
menuStrip1.ImageList = ImageList;
Test code:
private void LoadSubMenuIcons()
{
menuStrip1.ImageList = ImageList;
//Just for test:
controllerToolStripMenuItem.ImageIndex = 2;
ToolStrip firstLevelParent = controllerToolStripMenuItem.GetCurrentParent();
//firstLevelParent.ImageList is of type MenuStrip
//firstLevelParent.ImageList equals ImageList set above
foreach (ToolStripItem subMenuItem in controllerToolStripMenuItem.DropDownItems)
{
if (subMenuItem is ToolStripMenuItem)
{
subMenuItem.ImageIndex = 2;
ToolStrip secondLevelParent = subMenuItem.GetCurrentParent();
//secondLevelParent is of type ToolStripDropDownMenu
//secondLevelParent.ImageList is null
}
}
}
In the example code above I try to add the image with index 2 to all submenu-items. This doesn't work! It might have to do with the fact that their parent is of type ToolStripDropDownMenu and its ImageList-property is null? The middle level item, controllerToolStripMenuItem, on the other hand shows the image correctly. Its parent is menuStrip1 which has a valid refrence to my imagelist. How do I solve the problem with the submenu-items?
Hi I have a datagrid and would like to insert a treeview in each cell to view and select items per hierarchy level- Screenshot attached .
I tried to create through the following code but it shows in a wierd way and does not expand to view the child nodes:
List<string> treeList = new List<string>();
TreeView tree = new TreeView();
tree.Nodes.Add(new TreeNode("Root"));
if (treeList.Count() == 1)
{
tree.Nodes.Add(new TreeNode(treeList[0]));
}
else if (treeList.Count() > 1)
{
tree.Nodes.Add(new TreeNode(treeList[0]));
tree.Nodes[0].Nodes.Add(new TreeNode(treeList[1]));
}
tree.ExpandAll();
dgPreview.Rows.Add(tree,"");
The result is also attached as screenshot.
Can anybody guide on the issue!!
What i have: Big PictureBox (lets call it Pic_Map) on the form. A class Ore.cs, A List<Ore> ores; and a database that pulls the data and places it into the ores list.
Functionality: So, The functionality of this is i have a TextBox/Combobox and a Button. When i press the Button, it will loop through the ores list and Dynamically add PictureBoxes ontop of the Pic_Map based on TexBox/ComboBox being equal to the data (in this case Ore_Name).
Problem: This all works fine, but the problem is that when i add the PictureBoxes Dynamically, it only seems to add the last value on the ores list (Red circles on Pic_Map). so, it ends up showing only 1 PictureBox instead of lets say 3, since i have 3 value Names that match with the TextBox/ComboBox.
Question: How to get it to work like when i write/choose "Flame Stone" it looks on all the data that has "Flame Stone" in its name and add it (Instead of it adding only the last value from the list).
Code:
private void PopulateComboBoxByName()
{
PictureBox ore_Area = new PictureBox();
db.GetOre(); //Getting data and putting it into "ores" list
foreach (Ore ore in db.ores)
{
if (CBOX_Filter.SelectedItem.ToString() == ore.Ore_Name)
{
int area_Width = Convert.ToInt32(ore.Area_Width);
int area_Height = Convert.ToInt32(ore.Area_Height);
int ore_Width = Convert.ToInt32(ore.Ore_Width);
int ore_Height = Convert.ToInt32(ore.Ore_Height) - area_Height / 2;
ore_Area.Name = "ore_Area";
ore_Area.ImageLocation = #"Data\Images\Circle.png";
ore_Area.SizeMode = PictureBoxSizeMode.StretchImage;
ore_Area.Size = new Size(Convert.ToInt32(area_Width), Convert.ToInt32(area_Height));
ore_Area.Location = new Point(Convert.ToInt32(ore_Width), Convert.ToInt32(ore_Height));
ore_Area.BackColor = Color.Transparent;
this.Controls.Add(ore_Area);
}
}
ore_Area.Parent = PIC_Map;
}
Images:
Data:
Map:
Thanks to LarsTech, it has been fixed.
Making the picturebox is moved inside the loop, this.controls.add is changed to PIC_Map.controls.add and Ore_Area.Parent = PIC_Map; is deleted.
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.
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;