【转】c#中ListView控件加入ComboBox
2014-12-25 19:30
417 查看
转自:http://www.shangxueba.com/jingyan/95154.html
很多项目中要用到ListView控件来呈现并编辑数据。为方便用户的输入,可在ListView控件中加入Combobox来提高其用户操作性。实现的效果图:
1.建立一用户控件,命名MyListView,继承自ListView控件。
直接贴出代码:
2.建立一新的Windows应用程序项目。添加刚才创建的MyListView控件的引用。拖入MyListView控件和一ComboBox控件,令ComboBox Visible设为false。
直接贴出代码:
很多项目中要用到ListView控件来呈现并编辑数据。为方便用户的输入,可在ListView控件中加入Combobox来提高其用户操作性。实现的效果图:
1.建立一用户控件,命名MyListView,继承自ListView控件。
直接贴出代码:
using System; using System.Collections; using System.ComponentModel; using System.Drawing; using System.Data; using System.Windows.Forms; namespace InheritedListView { ///
/// Summary description for UserControl1. ///
public class MyListView : System.Windows.Forms.ListView { /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public MyListView() { // This call is required by the Windows.Forms Form. Designer. InitializeComponent(); // TODO: Add any initialization after the InitForm. call } /// /// Clean up any resources being used. /// protected override void Dispose(bool disposing) { if (disposing) { if (components != null) components.Dispose(); } base.Dispose(disposing); } #region Component Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { components = new System.ComponentModel.Container(); } #endregion private const int WM_HSCROLL = 0x114; private const int WM_VSCROLL = 0x115; protected override void WndProc(ref Message msg) { // Look for the WM_VSCROLL or the WM_HSCROLL messages. if ((msg.Msg == WM_VSCROLL) || (msg.Msg == WM_HSCROLL)) { // Move focus to the ListView to cause ComboBox to lose focus. this.Focus(); } // Pass message to default handler. base.WndProc(ref msg); } } }
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; } } }
相关文章推荐
- (转)在Visual Studio 2005的DataGridView控件中加入ComboBox下拉列表框的实现(C#)
- 在Visual Studio 2005的DataGridView控件中加入ComboBox下拉列表框的实现(C#)
- DataGridView控件中加入ComboBox下拉列表框的实现(C#)
- 在Visual Studio 2005的DataGridView控件中加入ComboBox下拉列表框的实现(C#)
- 在Visual Studio 2005的DataGridView控件中加入ComboBox下拉列表框的实现(C#)
- DataGridView控件中加入ComboBox下拉列表框的实现(C#)
- 在Visual Studio 2005的DataGridView控件中加入ComboBox下拉列表框的实现(C#)
- 在Visual Studio 2005的DataGridView控件中加入ComboBox下拉列表框的实现(C#)
- 在Visual Studio 2005的DataGridView控件中加入ComboBox下拉列表框的实现(C#)
- Visual Studio 2005的DataGridView控件中加入ComboBox下拉列表框的实现(C#)
- C#-ListView控件中列添加控件ComboBox,控件TextBox,添加时间选择列DateTimePicker
- 在Visual Studio 2005的DataGridView控件中加入ComboBox下拉列表框的实现(C#)
- 在DataGridView控件中加入ComboBox下拉列表框的实现(C#)
- 在Visual Studio 2005的DataGridView控件中加入ComboBox下拉列表框的实现(C#)
- 在Visual Studio 2005的DataGridView控件中加入ComboBox下拉列表框的实现(C#)
- C#WINFORM控件之ComboBox
- c# WinForms 通过ComboBox控件 引用数据库表与修改数据库
- 在DataGridView控件中加入ComboBox下拉列表框的实现4
- 在DataGridView控件中加入ComboBox下拉列表框的实现
- 深入浅出C#2008从入门到提高Step By Step(五)Windows应用程序常用控件类(8)-ComboBox