C# treeView實作 階層複選(checkbox)紀錄/還原 改變顏色等功能
C# treeView實作 階層複選(checkbox)紀錄/還原 改變顏色等功能
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace CS_treeView_checkbox
{
public partial class Form1 : Form
{
//this.treeView1.CheckBoxes = true;//Form1.Designer.cs~這樣treeView1元件才支援CheckBox
private bool[] m_blnClasscheck;//紀錄每個根節點狀態[是否選取]
private int[] m_intClassCount;//紀錄每個根節下的子節點數量
private bool m_blnOpenLog;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
m_blnOpenLog = false;
TreeNode newNode = null;
string strbuf;
m_blnClasscheck = new bool[1000];
m_intClassCount = new int[1000];
for (int i = 0; i < 1000; i++)
{
strbuf = “Class” + i;
newNode = new TreeNode(strbuf);
treeView1.Nodes.Add(newNode);
m_blnClasscheck[i] = false;
}
for (int j = 0; j < treeView1.Nodes.Count; j++)
{
m_intClassCount[j] = 70;
for (int k = 0; k < 70; k++)
{
strbuf = “s” + j+”~”+k;
newNode = new TreeNode(strbuf);
treeView1.Nodes[j].Nodes.Add(newNode);
}
}
}
private void setUI()
{
//全選紅色
//沒選白色
//有選黃色
int countIndex = 00;
String selectedNode = “”;
int intC = 0;
foreach (TreeNode myNode in treeView1.Nodes)
{
if ((myNode.Checked == true) && (m_blnClasscheck[intC] == false))
{
m_blnClasscheck[intC] = myNode.Checked;
myNode.BackColor = Color.Red;
foreach (TreeNode chNode in myNode.Nodes)
{
chNode.Checked = true;
chNode.BackColor = Color.Yellow;
}
}
if ((myNode.Checked == false) && (m_blnClasscheck[intC] == true))
{
m_blnClasscheck[intC] = myNode.Checked;
myNode.BackColor = Color.White;
foreach (TreeNode chNode in myNode.Nodes)
{
chNode.Checked = false;
chNode.BackColor = Color.White;
}
}
intC++;
}
for (int i = 0; i < treeView1.Nodes.Count; i++)
{
countIndex = 0;
foreach (TreeNode myNode in treeView1.Nodes[i].Nodes)
{
// Check whether the tree node is checked.
if (myNode.Checked)
{
// Set the node’s backColor.
myNode.BackColor = Color.Yellow;
selectedNode += myNode.Text + ” “;
countIndex++;
}
else
{
myNode.BackColor = Color.White;
}
if (countIndex > 0)//
{
treeView1.Nodes[i].BackColor = Color.Yellow;
}
else
{
treeView1.Nodes[i].BackColor = Color.White;
}
if (m_intClassCount[i] == countIndex)//子選項已經全選
{
if (treeView1.Nodes[i].Checked == false)//根還未選
{
treeView1.Nodes[i].Checked = true;
m_blnClasscheck[i] = true;
treeView1.Nodes[i].BackColor = Color.Red;
}
}
else//還未全選
{
if (treeView1.Nodes[i].Checked == true)
{
treeView1.Nodes[i].Checked = false;
m_blnClasscheck[i] = false;
}
}
}
}
richTextBox1.Text = selectedNode;
}
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
setUI();
}
private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)//沒用
{
}
private void treeView1_Click(object sender, EventArgs e)//沒用
{
//*
//*/
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Enabled=false;
if (System.IO.File.Exists(“AutoSave.dat”) && (!m_blnOpenLog))
{
//有
StreamReader sr = new StreamReader(“AutoSave.dat”);
while (!sr.EndOfStream)// 每次讀取一行,直到檔尾
{
string line = sr.ReadLine();// 讀取文字到 line 變數
string[] strs = line.Split(‘;’);
if ((strs.Length == 2) && (strs[1].Length > 0))
{
int root = Int32.Parse(strs[0]);
string[] strs_1 = strs[1].Split(‘,’);
for (int i = 0; i < (strs_1.Length – 1); i++)
{
int j = Int32.Parse(strs_1[i]);
int k = 0;
foreach (TreeNode myNode in treeView1.Nodes[root].Nodes)
{
if (j == k)
{
myNode.Checked = true;
}
k++;
}
}
treeView1.SelectedNode = treeView1.Nodes[root];//展開特定節點
treeView1.SelectedNode.Expand();
}
}
//treeView1.ExpandAll();//打開所有節點
setUI();
sr.Close();// 關閉串流
}
else
{
timer1.Interval = 1000;//1S
int countIndex = 0;
String selectedNode = “”;
StreamWriter sw = new StreamWriter(“AutoSave.dat”);
for (int i = 0; i < treeView1.Nodes.Count; i++)
{
countIndex = 0;
selectedNode = “”;
foreach (TreeNode myNode in treeView1.Nodes[i].Nodes)
{
// Check whether the tree node is checked.
if (myNode.Checked)
{
selectedNode += countIndex + “,”;
}
countIndex++;
}
selectedNode = i + “;” + selectedNode;
sw.WriteLine(selectedNode);// 寫入文字
}
sw.Close();// 關閉串流
}
m_blnOpenLog = true;
timer1.Enabled=true;
}
}
}