📄 list.cs
字号:
using System;
using System.Collections;
using System.Windows.Forms;
namespace EAlbum
{
//显示所有图片的树状图,继承了TreeView,Observer两个类
public class List : TreeView,Observer
{
private Model model;
//构造函数
public List()
{
}
//实现自己的dataUpdate
public void dataUpdate(Model model,string str,int npara)
{
this.model = model;
//如果操作为删除、添加和更新属性则重画树状图
if(str.Equals("3") || str.Equals("4") || str.Equals("5"))
DrawTree();
//如果是添加操作,还要负责将结点设为打开状态
if(str.Equals("4"))
this.Nodes[npara].Expand();
}
//设置视图对应的模型
public void SetModel(Model model)
{
this.model = model;
DrawTree();
}
//创建一个结点(表示照片的一个种类),参数是
private void InsertAlbum(string strName, int nAlbum)
{
TreeNode node = new TreeNode(strName);
node.Tag = new TreeItem(ItemType.Album, nAlbum);
//向树状图中插入一个子结点
this.Nodes.Add(node);
}
//插入照片,需要指定的参数是照片种类、照片名称、照片本身
private void InsertPhoto(int nAlbum, string strName, int nPhoto)
{
TreeNode node = new TreeNode(strName);
node.Tag = new TreeItem(ItemType.Photo, nPhoto);
//向nAlbum结点中插入一个子结点
this.Nodes[nAlbum].Nodes.Add(node);
}
//每次执行完删除、添加和更新属性操作都会调用
private void DrawTree()
{
ArrayList textlist=new ArrayList();
//将当前展开的结点放入textlist中
for(int i=0;i<this.Nodes.Count;i++)
{
if(this.Nodes[i].IsExpanded)
textlist.Add(this.Nodes[i].Text);
}
//把原视图清空(Nodes是TreeNode的一个成员)
this.Nodes.Clear();
//将结点加入TreeNode中
for(int i=0;i<model.albumList.Count;i++)
{
InsertAlbum((string)model.albumList[i], i);
}
//将子结点加入TreeNode中
for(int i=0;i<model.idList.Count;i++)
{
InsertPhoto((int)model.categoryList[i], (string)model.nameList[i], (int)model.idList[i]);
}
//把原来做过标记(已经打开)的结点打开
for(int i=0;i<this.Nodes.Count;i++)
{
if(textlist.Contains(this.Nodes[i].Text))
this.Nodes[i].Expand();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -