본문 바로가기

   
Programming/Winform

윈폼(winform) TreeView, ListView 사용예제

반응형

윈폼(winform) TreeView, ListView 사용예제

반드시 이미지 리스트를 통해서만 넣을수 있다.
트리뷰는 선택이 됐을때 안됐을때 이미지를 다르게 줄수 있다.
노드 속성을 많이 사용한다. 할아버지 레벨0 아버지 레벨1 형 레벨2
트리뷰가 Nodes라는 컬렉션으로 관리한다.


 
 




using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace Start

{

       public partial class TreeView : Form

       {

             public TreeView()

             {

                    InitializeComponent();

             }

 

             private void button1_Click(object sender, EventArgs e)

             {

                    //SelectedItem 동일

                    TreeNode node = treeView1.SelectedNode;

                    //MessageBox.Show(node.Text);

 

                    //선택된 노드의 전체경로

                    //MessageBox.Show(node.FullPath);

 

                    //선택된 노드의 레벨

                    //MessageBox.Show(node.Level.ToString());

             }

 

             private void button2_Click(object sender, EventArgs e)

             {

                    //할아버지 펼치기

                    //MessageBox.Show(this.treeView1.Nodes.Count.ToString());\

                    this.treeView1.Nodes[0].Nodes[1].Expand();

 

                    //아버지

                    //treeView1.Nodes[0].Nodes[1]

 

                    this.treeView1.Nodes[0].Nodes[1].ExpandAll();

             }

 

             private void button3_Click(object sender, EventArgs e)

             {

                    //노드접기

                    this.treeView1.Nodes[0].Collapse();

             }

       }

}

 

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace Start

{

       public partial class TreeView : Form

       {

             public TreeView()

             {

                    InitializeComponent();

             }

 

             private void button1_Click(object sender, EventArgs e)

              {

                    //SelectedItem 동일

                    TreeNode node = treeView1.SelectedNode;

                    //MessageBox.Show(node.Text);

 

                    //선택된 노드의 전체경로

                    //MessageBox.Show(node.FullPath);

 

                    //선택된 노드의 레벨

                    //MessageBox.Show(node.Level.ToString());

             }

 

             private void button2_Click(object sender, EventArgs e)

             {

                    //할아버지 펼치기

                    //MessageBox.Show(this.treeView1.Nodes.Count.ToString());\

                    this.treeView1.Nodes[0].Nodes[1].Expand();

 

                    //아버지

                    //treeView1.Nodes[0].Nodes[1]

 

                    this.treeView1.Nodes[0].Nodes[1].ExpandAll();

             }

 

             private void button3_Click(object sender, EventArgs e)

             {

                    //노드접기

                    this.treeView1.Nodes[0].Collapse();

             }

 

             private void TreeView_Load(object sender, EventArgs e)

             {

                    treeView1.Nodes[0].Expand();

             }

 

             private void button4_Click(object sender, EventArgs e)

             {

                    //누구의 자식?

                    //선택한 노드의 자식으로 노드 추가하기

                    TreeNode node = new TreeNode("자식노드", 4, 4);

                    treeView1.SelectedNode.Nodes.Add(node);

             }

 

             private void button5_Click(object sender, EventArgs e)

             {

                    //노드 삭제

                    //선택한 노드를 삭제

                    treeView1.SelectedNode.Remove();

             }

 

             private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)

             {

                    //노드 선택이 바뀔떄마다 표시

                    label1.Text = treeView1.SelectedNode.Text;

             }

 

             private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)

             {

                    //라벨 편집이 완료된 후에 발생

                    //탐색기의 경우 : 폴더의 이름을 바꾸는 처리 실행

                    MessageBox.Show("이름 바뀜" + e.Label);

             }

 

             private void treeView1_AfterExpand(object sender, TreeViewEventArgs e)

             {

                    //어떤 노드가 펼쳐?

                    label2.Text = e.Node.Text + " : 펼쳐짐";

             }

 

             private void treeView1_AfterCollapse(object sender, TreeViewEventArgs e)

             {

                    //어떤 노드가 접는?

                    label2.Text = e.Node.Text + " : 접힘";

             }

       }

}



TreeView2


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.IO;

 

namespace Start

{

       public partial class TreeViewLoop : Form

       {

             public TreeViewLoop()

             {

                    InitializeComponent();

             }

 

             private void TreeView03_Load(object sender, EventArgs e)

             {

                    //DirectoryInfo, TreeNode

                    DirectoryInfo dir = new DirectoryInfo(@"C:\");

                    TreeNode node = new TreeNode(dir.Name);

                    treeView1.Nodes.Add(node);

 

                    //1. 자식이 있는지 목록 가져오기

                    //2. 자식이름으로 TreeNode 생성 후 추가

 

                    //3. 자식의 자식이 있는 목록

                    //4. 자식의 자식이름으로 TreeNode 생성 후 추가

                    //5. 자식의 자식의 자식이 있는 목록..

                    //6.

                    SetDirectory(dir, node);

 

             }

 

             private void SetDirectory(DirectoryInfo dir, TreeNode node)

             {

                    try

                    {

                           DirectoryInfo[] sub = dir.GetDirectories();

                           for (int i = 0; i < sub.Length; i++)

                           {

                                 TreeNode cnode = new TreeNode(sub[i].Name, 1, 2);

                                 node.Nodes.Add(cnode);

 

                                 //** 재귀 구조:자기 자신을 또호출

                                 SetDirectory(sub[i], cnode);

 

                                 treeView1.Nodes[0].ExpandAll();

                           }

                    }

                    catch{}

                   

             }

       }

}

 

 


TreeView3 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.IO;

 

namespace Start

{

       public partial class TreeView03 : Form

       {

             public TreeView03()

             {

                    InitializeComponent();

             }

 

             private void TreeView03_Load(object sender, EventArgs e)

             {

                    DirectoryInfo dir = new DirectoryInfo(@"D:\");

                    TreeNode node = new TreeNode("로컬 디스크(D:)", 0, 0);

 

                    node.Tag = dir.FullName;//실제경로

                    treeView1.Nodes.Add(node);

 

                    SetDirectory(dir, node);

             }

 

             private void SetDirectory(DirectoryInfo dir, TreeNode node)

             {

                    try

                    {

                           DirectoryInfo[] sub = dir.GetDirectories();

 

                           for (int i = 0; i < sub.Length; i++)

                           {

                                 TreeNode cnode = new TreeNode(sub[i].Name, 1, 2);

                                

                                 cnode.Tag = sub[i].FullName;

                                 node.Nodes.Add(cnode);

                                

                                 //재귀?

                                 try

                                 {

                                        if (sub[i].GetDirectories().Length > 0)

                                        {

                                              TreeNode tempNode = new TreeNode("임시");

                                              cnode.Nodes.Add(tempNode);

                                        }

                                 }

                                 catch { }

                           }

                    }

                    catch { }

             }

 

             private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)

             {

                    //+를 누르고 펼치지기 직전의 이벤트

                    // -> 임시 자식을 삭제하고 진짜 자식으로 교체

 

                    //준비물

                    //1. + 가 눌린 진짜 폴더(DirectoryInfo)

                    //2. + 가 눌린 TreeNode

                    //e.Node

 

                    //MessageBox.Show(e.Node.FullPath);

                    //MessageBox.Show(e.Node.Tag.ToString());

 

                    //1.임시 삭제

                    e.Node.Nodes.Clear();

 

                    //2.DirectoryInfo를 가지고 +눌러진 자식을 가져오기

                    DirectoryInfo dir = new DirectoryInfo(e.Node.Tag.ToString());//*****

 

                    DirectoryInfo[] sub = dir.GetDirectories();

 

                    try

                    {

                           //3. TreeNode를 만들어 추가

                           for (int i = 0; i < sub.Length; i++)

                           {

                                 TreeNode cnode = new TreeNode(sub[i].Name, 1, 2);

                                 cnode.Tag = sub[i].FullName;

                                 e.Node.Nodes.Add(cnode);

 

                                 try

                                 {

                                        //4. 눈속임으로 손자 만들어 두기

                                        if (sub[i].GetDirectories().Length > 0)

                                        {

                                              cnode.Nodes.Add(new TreeNode("임시"));

                                        }

                                 }

                                 catch { }

                           }

                    }

                    catch { }

 

             }

       }

}

 

 
ListView
행전체에 해당하는것은 ListViewItem.Text
행안에서 항목들은 SubItem.Text

 


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace Start

{

       public partial class ListView : Form

       {

             public ListView()

             {

                    InitializeComponent();

             }

 

             private void button1_Click(object sender, EventArgs e)

             {

                    //listview1.SelectedItems

                    //listView1.SelectedIndices

 

                    // ** 선택된 항목이 존재하면..

                    if (listView1.SelectedItems.Count > 0)

                    {

                           ListViewItem item = listView1.SelectedItems[0];

 

                           //item : 한가지 정보X -> 컬럼이 총 3

                           string name = item.Text;//첫번쨰 컬럼값

                           string age = item.SubItems[1].Text;//두번째 컬럼값

                           string address = item.SubItems[2].Text;

                          string msg = string.Format("이름 : {0}\r\n나이 : {1}\r\n주소 : {2}", name, age, address);

 

                           MessageBox.Show(msg);

                           MessageBox.Show(item.Checked.ToString());

                    }

             }

 

             private void button2_Click(object sender, EventArgs e)

             {

                    //새로운 사람 추가

                    //이순신/30/고구려

                    ListViewItem item = new ListViewItem("이순신", 2);

                    item.SubItems.Add("30");//두번쨰 컬럼

                    item.SubItems.Add("서울시");//세번쨰 컬럼

 

                    //리스트뷰에 추가

                    listView1.Items.Add(item);

             }

 

             private void button3_Click(object sender, EventArgs e)

             {

                    //선택항목

                    if (listView1.SelectedItems.Count > 0)

                    {

                           listView1.Items.Remove(listView1.SelectedItems[0]);

                    }

             }

 

             private void listView1_SelectedIndexChanged(object sender, EventArgs e)

             {

                    //선택되고 다른것을 선택할떄 선택된것이 풀릴때의 값도 카운트가 된다

                    //label1.Text += "\r\n" + listView1.SelectedItems.Count.ToString();

 

                    //listView한에서는 선택항목이 변경될때 중간에 SelectedItems.Count 0이 되는 상황이 발생

                    //0이 되는 상황에서도 SelectedIndexChanged이벤트가 발생된다.

                    //listView1.SelectedItems[0] 항목을 접근 불가능 하기 때문에 에러발생

                    //따라서 항상 SelectedItems.Count > 0 보다 큰지 검사 하는것이 필수 이다.

                    if (listView1.SelectedItems.Count > 0)

                    {

                           label1.Text = listView1.SelectedItems[0].Text;

                    }

                   

             }

       }

}

 

 


반응형