概要
cscの作法、調べてみた。
練習問題やってみた。
練習問題
TreeViewで、nodeにTagがあったら表示せよ。
サンプルコード
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Diagnostics;
namespace App
{
public class Form1: Form {
private TreeView treeView1;
private Splitter splitter1;
private ListView listView1;
private StatusBar statusBar1;
private System.ComponentModel.IContainer components;
public Form1() {
InitializeComponent();
}
protected override void Dispose(bool disposing) {
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
private void InitializeComponent() {
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.treeView1 = new TreeView();
this.splitter1 = new Splitter();
this.listView1 = new ListView();
this.statusBar1 = new StatusBar();
this.SuspendLayout();
this.treeView1.Dock = DockStyle.Left;
this.treeView1.ImageIndex = -1;
this.treeView1.Name = "treeView1";
this.treeView1.SelectedImageIndex = -1;
this.treeView1.Size = new System.Drawing.Size(121, 273);
this.treeView1.TabIndex = 0;
this.treeView1.AfterSelect += new TreeViewEventHandler(this.treeView1_AfterSelect);
this.treeView1.BeforeExpand += new TreeViewCancelEventHandler(this.treeView1_BeforeExpand);
this.splitter1.Location = new System.Drawing.Point(121, 0);
this.splitter1.Name = "splitter1";
this.splitter1.Size = new System.Drawing.Size(3, 273);
this.splitter1.TabIndex = 1;
this.splitter1.TabStop = false;
this.listView1.Dock = DockStyle.Fill;
this.listView1.Location = new System.Drawing.Point(124, 0);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(168, 273);
this.listView1.TabIndex = 2;
this.listView1.View = View.List;
this.statusBar1.Location = new System.Drawing.Point(124, 251);
this.statusBar1.Name = "statusBar1";
this.statusBar1.Size = new System.Drawing.Size(168, 22);
this.statusBar1.TabIndex = 3;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new Control[] {
this.statusBar1,
this.listView1,
this.splitter1,
this.treeView1
});
this.Name = "Form1";
this.Text = "TreeView_by_Dk";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.ClientSize = new Size(400, 400);
}
private void Form1_Load(object sender, System.EventArgs e) {
TreeNode tNode;
tNode = treeView1.Nodes.Add("Websites");
treeView1.Nodes[0].Nodes.Add("Net-informations.com");
treeView1.Nodes[0].Nodes[0].Nodes.Add("CLR");
treeView1.Nodes[0].Nodes.Add("https://net-informations.com/vb/default.htm");
treeView1.Nodes[0].Nodes[1].Nodes.Add("String Tutorial");
treeView1.Nodes[0].Nodes[1].Nodes.Add("Excel Tutorial");
treeView1.Nodes[0].Nodes.Add("Csharp.net-informations.com");
treeView1.Nodes[0].Nodes[2].Nodes.Add("ADO.NET");
treeView1.Nodes[0].Nodes[2].Nodes[0].Nodes.Add("Dataset");
TreeNode node1 = new TreeNode("項目1");
node1.Tag = "これは項目1のカスタムデータです";
treeView1.Nodes.Add(node1);
TreeNode node2 = new TreeNode("項目2");
node2.Tag = new {
Description = "項目2の説明",
Value = 123
};
treeView1.Nodes.Add(node2);
}
private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e) {
TreeNode selectedNode = e.Node;
//Console.WriteLine(selectedNode.FullPath);
//selectedNode.Nodes.Clear();
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) {
listView1.Clear();
TreeNode selectedNode = e.Node;
Console.WriteLine(selectedNode.FullPath);
listView1.Items.Add(selectedNode.Text);
if (selectedNode.Tag != null)
listView1.Items.Add(selectedNode.Tag.ToString());
//statusBarTextShow(e);
}
private void statusBarTextShow(TreeViewEventArgs e) {
string s1,
s2;
if (e.Node.FullPath.Length <= 3)
{
s1 = e.Node.FullPath.Substring(0, 3);
s2 = e.Node.FullPath.Substring(3, (e.Node.FullPath.Length - 3));
}
else
{
s1 = e.Node.FullPath.Substring(0, 3);
s2 = e.Node.FullPath.Substring(4, (e.Node.FullPath.Length - 4));
}
statusBar1.Text = s1 + s2;
}
[STAThread]
static void Main() {
Application.Run(new Form1());
}
}
}
写真
以上。