반응형

 

 

 

위와 같이 리스트뷰에 텍스트 박스를 넣어서 편집을 하는 경우이다.

 

아래는 리스트뷰에 텍스트 박스를 넣기위한, 리스트뷰 디자이너쪽 프로퍼티와

리스트뷰 이벤트 1개, 텍스트 박스 이벤트 2개 소스.

 

 

 

//리스트뷰의 기본 생성 디자이너쪽

this.FileListView.CheckBoxes = true;
this.FileListView.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.col_No,
this.col_pageCnt,
this.col_FileName,
this.col_printTime});
this.FileListView.Font = new System.Drawing.Font("맑은 고딕", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(129)));
this.FileListView.FullRowSelect = true;
this.FileListView.GridLines = true;
this.FileListView.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable;
this.FileListView.HideSelection = false;
this.FileListView.Location = new System.Drawing.Point(6, 56);
this.FileListView.MultiSelect = false;
this.FileListView.Name = "FileListView";
this.FileListView.Size = new System.Drawing.Size(291, 402);
this.FileListView.TabIndex = 6;
this.FileListView.UseCompatibleStateImageBehavior = false;
this.FileListView.View = System.Windows.Forms.View.Details;
this.FileListView.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.FileListView_MouseDoubleClick);
this.txt_etc.Location = new System.Drawing.Point(665, 39);



this.txt_etc.Name = "txt_etc";
this.txt_etc.Size = new System.Drawing.Size(89, 21);
this.txt_etc.TabIndex = 18;
this.txt_etc.Visible = false;
this.txt_etc.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txt_etc_KeyDown);
this.txt_etc.Leave += new System.EventHandler(this.txt_etc_Leave);

당연 디테일뷰와 총 4개의 Columns (SubItem)을 갖게 해놨으며, 텍스트 박스와 리스트뷰 각각 이벤트를 연결해놓았다.
private bool _cancelEdit = false;
ListViewItem curItem;
ListViewItem.ListViewSubItem curSbItem;  

멤버 변수 참고
private void FileListView_MouseDoubleClick(object sender, MouseEventArgs e)
{           
	//위치기반으로 어느 Item과 어느 subitem을 떠블클릭한건지 확인
	curItem = FileListView.GetItemAt(e.X, e.Y);
	if (curItem == null)
		return;
	int idxRow = FileListView.Items.IndexOf(curItem);

	curSbItem = curItem.GetSubItemAt(e.X, e.Y);
	int idxSub = curItem.SubItems.IndexOf(curSbItem);

	//비고쪽을 떠블클릭한 것이 아니라면 Return
	switch (idxSub)
	{
		case 0: //순번
		case 1: //서식명

		case 3: //시간
		case 4: //서식코드
		case 5: //파일명
			return;

		case 2: //비고
			break;

		default: //N/A
			return;
	}

	//해당 셀의 위치로 텍스트박스를 이동
	int left = curSbItem.Bounds.Left + 2;
	int width = curSbItem.Bounds.Width;

	txt_etc.SetBounds(left + FileListView.Left + ImageListBox.Left,
		curSbItem.Bounds.Top + FileListView.Top + ImageListBox.Top, 
		width, curSbItem.Bounds.Height);

	//비고란에 데이타가 없으면(처음입력),
	if (string.IsNullOrEmpty(curSbItem.Text))
	{
		//윗줄에 비고값 확인
		if (idxRow > 0 && FileListView.Items[idxRow - 1].SubItems[2].Text != "")
			//있으면, 기본값은 윗줄의 비고값입력
			txt_etc.Text = FileListView.Items[idxRow - 1].SubItems[2].Text;
		else
			//없으면, 기본값은 공란
			txt_etc.Text = "";
	}
	else
	{
		txt_etc.Text = curSbItem.Text;
	}

	txt_etc.Show();
	txt_etc.Focus();

}

private void txt_etc_KeyDown(object sender, KeyEventArgs e)
{
	switch(e.KeyCode)
	{
		case System.Windows.Forms.Keys.Enter:
			e.Handled = true;
			_cancelEdit = false;
			txt_etc.Hide();

			break;
		case System.Windows.Forms.Keys.Escape:
			e.Handled = true;
			_cancelEdit = true;                    
			txt_etc.Hide();
			break;

		default:
			return;
	}


}

private void txt_etc_Leave(object sender, EventArgs e)
{
	txt_etc.Hide();
	if (!_cancelEdit)
	{
		curSbItem.Text = txt_etc.Text;
	}
	_cancelEdit = false;    // re init
	FileListView.Focus();

	//txt_etc.Hide();
	//if (_cancelEdit == false)
	//{
	//    if (txt_etc.Text.Trim() != "")
	//    {
	//        curSbItem.Text = txt_etc.Text;
	//        //int idxSub = curItem.SubItems.IndexOf(curSbItem);
	//        //int idx = curItem.Index;
	//    }
	//}
}

반응형
Posted by Rainfly
l