[C# 윈폼] 윈폼 ListView 컨트롤 기본 사용 방법

안녕하세요.

 

오늘은 C# 윈폼에서 ListView 컨트롤 기본 사용 방법에 대해서 알려 드리려고 합니다.

 

ListView 컨트롤은 윈폼으로 프로그램 개발해 보신 분들이라면 아시겠지만 여러 다방면으로 정말 많이 사용되는 컨트롤이기 때문에 사용 방법을 익히시면 두고두고 사용 하실 수 있는 그런 컨트롤입니다.

 

그럼 바로 예제 프로그램을 통해서 어떻게 ListView 컨트롤을 사용하는지 보여 드리도록 하겠습니다.

 

아래와 같이 빈 윈폼 프로젝트를 생성해 주시고 ListView 컨트롤 하나를 배치해 주시기 바랍니다.

 

참고 사이트
 

ListView 컨트롤 - C# 프로그래밍 배우기 (Learn C# Programming)

ListView 컨트롤 ListView 컨트롤은 파일 탐색기의 디렉토리 내 파일들처럼 아이템들을 여러 리스트 형태로 보여주는 컨트롤이다. 리스트 형태를 지정하는 View 프로퍼티는 Details, List, Tile, LargeIcon, Sma

www.csharpstudy.com

빈 윈폼 프로젝트 생성 및 ListView 컨트롤 배치

위와 같이 빈 윈폼 프로젝트에 ListView 컨트롤 하나를 배치 해주시기 바랍니다.

 

그럼 이제 예제 코드를 작성해서 ListView 컨트롤에 Item을 넣어 보도록 하겠습니다.

 

여기까지 따라하셨다면, 이제 ListViewe Item들 앞에 이미지를 넣기 위해 ImageList 컨트롤을 아래와 같이 추가해 주시고, 이미지는 아무 이미지나 임의로 넣어 주시기 바랍니다.

 

ImageList 컨트롤 넣기

저는 위와 같이 문서.jpg” 라는 이미지를 넣었습니다.

 

그럼 이제 컨트롤 배치는 모두 끝냈으니까 예제 코드를 작성해 보도록 하겠습니다

 

예제 코드
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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.Threading.Tasks;
using System.Windows.Forms;
 
namespace ListViewTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
 
            this.Load += Form1_Load;
        }
        
        /// <summary>
        /// Load 이벤트 핸들러
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            SettingListView(uiLv_Main, GetCurrentDirecotry());
        }
 
        private FileInfo[] GetCurrentDirecotry()
        {
            string currDir = Environment.CurrentDirectory;
            DirectoryInfo di = new DirectoryInfo(currDir);
            FileInfo[] files = di.GetFiles();
 
            return files;
        }
 
        private void SettingListView(ListView listView, FileInfo[] files)
        {
            // 리스트뷰 아이템을 업데이트 하기 시작.
            // 업데이트가 끝날 때까지 UI 갱신 중지.
            listView.BeginUpdate();
 
            // 뷰모드 지정
            listView.View = View.Details;
 
            //ListView 이미지 List 적용
            listView.LargeImageList = imageList1;
            listView.SmallImageList = imageList1;
 
            foreach (var fi in files)
            {
                // 각 파일별로 ListViewItem객체를 하나씩 만듦
                // 파일명, 사이즈, 날짜 정보를 추가
                ListViewItem lvi = new ListViewItem(fi.Name);
                lvi.SubItems.Add(fi.Length.ToString());
                lvi.SubItems.Add(fi.LastWriteTime.ToString());
                lvi.ImageIndex = 0;
 
                // ListViewItem객체를 Items 속성에 추가
                listView.Items.Add(lvi);
            }
 
            // 컬럼명과 컬럼사이즈 지정
            listView.Columns.Add("파일명"200, HorizontalAlignment.Left);
            listView.Columns.Add("사이즈"70, HorizontalAlignment.Left);
            listView.Columns.Add("날짜"150, HorizontalAlignment.Left);
 
            // 리스뷰를 Refresh하여 보여줌
            listView.EndUpdate();
 
            this.Refresh();
        }
    }
}
 
cs

 

실행 결과

위와 같이 ListView 컨트롤에 아이템들이 알맞게 Add 되어 보여지는 것을 확인하실 수 있습니다.

 

감사합니다.^^

728x90

이 글을 공유하기

댓글

Designed by JB FACTORY