在我的Windows窗体中,我有以下Treeview
控件,其中有四个节点代表大学课程的学年。
正如您所看到的,我已经向节点添加了小图像(对它们进行颜色编码)图像的大小为8 X 8像素png图像。我已使用ImageList
集合将它们内联存储。
我对此有两个问题
第二个问题请参考下图,了解单击节点时发生的情况
2.你可以看到绿色突然变成橙色
有没有办法解决这些问题?
在treeview
控件的当前配置中,我已将showlines
设置为false
,将hideselection
设置为false
所有其他属性均为默认设置。
这是代码
在表单的load()中我将此myImageList
集合变量全局声明
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;
}
我没有提到的组合框但是它在这里做了什么,它有1,2,3,4个值分别意味着第1,2,3,4个,所以在我添加的组合框的SelectedIndexChanged()事件中这段代码所以它将以下列方式添加节点。如果用户选择1,如果用户选择2,则仅将年份#1添加到TreeView,将年份#1和年份#2添加到树视图中
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];
}
}
感谢
答案 0 :(得分:3)
SelectedImageIndex
属性(以及设置StateImageIndex
属性的StateImage
)设置为与{相同的值} {1}} property。