Reputation: 271
In my windows form I have the following Treeview
control with four nodes to represent the academic years of a university program.
As you can see I have added small images to the nodes (to color code them) image are of size 8 X 8 pixels png images. I have used the ImageList
collection to store them inline.
there are two issues I have with this
the second problem refer to the following image to see what happened when I click a node
2.As you can see the green one suddenly got orange
is there a way to fix those problems?
in the current configuration of the treeview
control, I have set the showlines
to false
and hideselection
to false
all the rest of properties are their default settings.
here is the code
in the form's load() I have this myImageList
collection variable is globally declared
private void FrmNewProgram_Load(object sender, EventArgs e)
{
myImageList = new ImageList();
myImageList.Images.Add(new Bitmap(SMSV100.Properties.Resources.year1tagx16));
myImageList.Images.Add(new Bitmap(SMSV100.Properties.Resources.year12tagx16));
myImageList.Images.Add(new Bitmap(SMSV100.Properties.Resources.year3x16));
myImageList.Images.Add(new Bitmap(SMSV100.Properties.Resources.year4x16));
tvcAcdYears.ImageList = myImageList;
}
There is combobox which I did not mentioned but here what it does, it has 1, 2, 3, 4 values respectively mean year 1, 2, 3, 4 so in the SelectedIndexChanged() event of the combobox I have added this code so it will add the nodes in the following way. if the user selects 1, adds only Year #1 to the TreeView if user selects 2, adds Year #1 and Year #2 to the treeview so forth
private void cmbProgDuration_SelectedIndexChanged(object sender, EventArgs e)
{
if (cmbProgDuration.SelectedIndex > -1)
{
if (cmbProgDuration.SelectedIndex == 0)
{
tvcAcdYears.Nodes.Clear();
TreeNode tnOneYear = new TreeNode("Year #1");
tnOneYear.ImageIndex = 0;
tvcAcdYears.Nodes.Add(tnOneYear);
}
else if (cmbProgDuration.SelectedIndex == 1)
{
tvcAcdYears.Nodes.Clear();
TreeNode tnOneYear = new TreeNode("Year #1");
tnOneYear.ImageIndex = 0;
TreeNode tn2ndYear = new TreeNode("Year #2");
tn2ndYear.ImageIndex = 1;
tvcAcdYears.Nodes.Add(tnOneYear);
tvcAcdYears.Nodes.Add(tn2ndYear);
}
else if (cmbProgDuration.SelectedIndex == 2)
{
tvcAcdYears.Nodes.Clear();
TreeNode tnOneYear = new TreeNode("Year #1");
tnOneYear.ImageIndex = 0;
TreeNode tn2ndYear = new TreeNode("Year #2");
tn2ndYear.ImageIndex = 1;
TreeNode tn3rdYear = new TreeNode("Year #3");
tn3rdYear.ImageIndex = 2;
tvcAcdYears.Nodes.Add(tnOneYear);
tvcAcdYears.Nodes.Add(tn2ndYear);
tvcAcdYears.Nodes.Add(tn3rdYear);
}
else if (cmbProgDuration.SelectedIndex == 3)
{
tvcAcdYears.Nodes.Clear();
TreeNode tnOneYear = new TreeNode("Year #1");
tnOneYear.ImageIndex = 0;
TreeNode tn2ndYear = new TreeNode("Year #2");
tn2ndYear.ImageIndex = 1;
TreeNode tn3rdYear = new TreeNode("Year #3");
tn3rdYear.ImageIndex = 2;
TreeNode tn4thYear = new TreeNode("Year #4");
tn4thYear.ImageIndex = 3;
tvcAcdYears.Nodes.Add(tnOneYear);
tvcAcdYears.Nodes.Add(tn2ndYear);
tvcAcdYears.Nodes.Add(tn3rdYear);
tvcAcdYears.Nodes.Add(tn4thYear);
}
tvcAcdYears.SelectedNode = tvcAcdYears.Nodes[0];
}
}
thanks
Upvotes: 0
Views: 1041
Reputation: 14059
SelectedImageIndex
property (and the StateImageIndex
if the StateImage
property is set) of each treeview's node is set to the same value as for the ImageIndex
property.Upvotes: 3