LoginSignup
0
0

cscの作法 その475

Posted at

概要

cscの作法、調べてみた。
練習問題やってみた。

練習問題

treeviewを、使え。

参考にしたページ

写真

image.png

サンプルコード

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace App
{
	class DirTreeNode: TreeNode {
		public DirTreeNode() {
		}
		public DirTreeNode(string directoryName, string directoryPath) : base(directoryName) {
			DirectoryPath = directoryPath;
		}
		public string DirectoryPath {
			get;
			set;
		}
	}
	public partial class Form1: Form {
		private TreeView treeView1;
		public Form1() {
			ClientSize = new Size(500, 300);
			Text = "TreeView";
			treeView1 = new TreeView();
			SuspendLayout();
			treeView1.Dock = DockStyle.Fill;
			treeView1.HotTracking = true;
			//treeView1.ImageList = imageList1;
			treeView1.Location = new Point(10, 10);
			treeView1.Name = "treeView1";
			treeView1.Size = new Size(480, 280);
			treeView1.TabIndex = 0;
			treeView1.BeforeExpand += new TreeViewCancelEventHandler(treeView1_BeforeExpand);
			Controls.AddRange(new Control[] {
				treeView1
			});
			DirTreeNode driveNode = new DirTreeNode("C:Neos", "C:\\Neos");
			treeView1.Nodes.Add(driveNode);
			driveNode.Nodes.Add(new DirTreeNode());
		}
		private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e) {
			DirTreeNode dirNode = (DirTreeNode) e.Node;
			dirNode.Nodes.Clear();
			foreach (string directoryPath in Directory.GetDirectories(dirNode.DirectoryPath))
			{
				DirectoryInfo directoryInfo = new DirectoryInfo(directoryPath);
				if ((directoryInfo.Attributes & FileAttributes.System) != FileAttributes.System)
				{
					DirTreeNode subDirNode = new DirTreeNode(directoryInfo.Name, directoryPath);
					dirNode.Nodes.Add(subDirNode);
					if (Directory.GetDirectories(directoryPath).Length > 0)
					{
						subDirNode.Nodes.Add(new DirTreeNode());
					}
				}
			}
		}
		[STAThread]
		static void Main() {
			Application.Run(new Form1());
		}
	}
}





以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0