2.建立一新的Windows應用程序項目。添加剛才創建的MyListView控件的引用。拖入MyListVIEw控件和一ComboBox控件,令ComboBox Visible設為false。
直接貼出代碼:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication3
{
public partial class Form1 : Form
{
private ListVIEwItem lvItem;
public Form1()
{
InitializeComponent();
// Add a few items to the combo box list.
this.cbListVIEwCombo.Items.Add("NC");
this.cbListVIEwCombo.Items.Add("WA");
// Set view of ListVIEw to Details.
this.myListView1.View = VIEw.Details;
// Turn on full row select.
this.myListVIEw1.FullRowSelect = true;
// Add data to the ListVIEw.
ColumnHeader columnheader;
ListViewItem listvIEwitem;
// Create sample ListVIEw data.
listviewitem = new ListVIEwItem("NC");
listvIEwitem.SubItems.Add("North Carolina");
this.myListView1.Items.Add(listvIEwitem);
listviewitem = new ListVIEwItem("WA");
listvIEwitem.SubItems.Add("Washington");
this.myListView1.Items.Add(listvIEwitem);
// Create column headers for the data.
columnheader = new ColumnHeader();
columnheader.Text = "State Abbr.";
this.myListVIEw1.Columns.Add(columnheader);
columnheader = new ColumnHeader();
columnheader.Text = "State";
this.myListVIEw1.Columns.Add(columnheader);
// Loop through and size each column header to fit the column header text.
foreach (ColumnHeader ch in this.myListVIEw1.Columns)
{
ch.Width = -2;
}
}
private void cbListVIEwCombo_SelectedValueChanged(object sender, EventArgs e)
{
// Set text of ListVIEw item to match the ComboBox.
lvItem.Text = this.cbListVIEwCombo.Text;
// Hide the ComboBox.
this.cbListVIEwCombo.Visible = false;
}
private void cbListVIEwCombo_SelectedIndExchanged(object sender, EventArgs e)
{
// Set text of ListVIEw item to match the ComboBox.
lvItem.Text = this.cbListVIEwCombo.Text;
// Hide the ComboBox.
this.cbListVIEwCombo.Visible = false;
}
private void cbListVIEwCombo_KeyPress(object sender, KeyPressEventArgs e)
{
// Verify that the user presses ESC.
switch (e.KeyChar)
{
case (char)(int)Keys.Escape:
{
// Reset the original text value, and then hide the ComboBox.
this.cbListVIEwCombo.Text = lvItem.Text;
this.cbListVIEwCombo.Visible = false;
break;
}
case (char)(int)Keys.Enter:
{
// Hide the ComboBox.
this.cbListVIEwCombo.Visible = false;
break;
}
}
}
private void myListVIEw1_MouseUp(object sender, MouseEventArgs e)
{
lvItem = this.myListVIEw1.GetItemAt(e.X, e.Y);
// Make sure that an item is clicked.
if (lvItem != null)
{
// Get the bounds of the item that is clicked.
Rectangle ClickedItem = lvItem.Bounds;
//單擊ListVIEw第一列顯示ComBoBox控件
if (e.X > this.myListVIEw1.Columns[0].Width)
{
return;
}
// Verify that the column is completely scrolled off to the left.
if ((ClickedItem.Left + this.myListVIEw1.Columns[0].Width) < 0)
{
// If the cell is out of vIEw to the left, do nothing.
return;
} // Verify that the column is partially scrolled off to the left.
else if (ClickedItem.Left < 0)
{
// Determine if column extends beyond right side of ListVIEw.
if ((ClickedItem.Left + this.myListView1.Columns[0].Width) > this.myListVIEw1.Width)
{
// Set width of column to match width of ListVIEw.
ClickedItem.Width = this.myListVIEw1.Width;
ClickedItem.X = 0;
}
else
{
// Right side of cell is in vIEw.
ClickedItem.Width = this.myListVIEw1.Columns[0].Width + ClickedItem.Left;
ClickedItem.X = 2;
}
}
else if (this.myListView1.Columns[0].Width > this.myListVIEw1.Width)
{
ClickedItem.Width = this.myListVIEw1.Width;
}
else
{
ClickedItem.Width = this.myListVIEw1.Columns[0].Width;
ClickedItem.X = 2;
}
// Adjust the top to account for the location of the ListVIEw.
ClickedItem.Y += this.myListVIEw1.Top;
ClickedItem.X += this.myListVIEw1.Left;
// Assign calculated bounds to the ComboBox.
this.cbListVIEwCombo.Bounds = ClickedItem;
// Set default text for ComboBox to match the item that is clicked.
this.cbListVIEwCombo.Text = lvItem.Text;
// Display the ComboBox, and make sure that it is on top with focus.
this.cbListVIEwCombo.Visible = true;
this.cbListVIEwCombo.BringToFront();
this.cbListVIEwCombo.Focus();
}
}
private void myListVIEw1_Click(object sender, EventArgs e)
{
//this.myListVIEw1.Select();
this.cbListVIEwCombo.Visible = false;
}
}
}