如何使用 ComboBox 控件来编辑 Visual C# 中 ListView 控件中数据

翻译|其它|编辑:郝浩|2008-06-16 11:34:44.000|阅读 4620 次

概述:

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

创建继承 ListView 控件

1. 启动 MicrosoftVisualStudio.NET 或 Microsoft Visual Studio 2005。

2. 在文件菜单, 指向新建 , 然后单击项目 。

3. 在新建项目 对话框中, 单击 ProjectTypes@@ , 下VisualC #项目,然后单击模板下WindowsControlLibrary@@ 。
注意 对于 Visual Studio 2005, 单击 项目类型 下 VisualC # 。

4. 用以下代码替换所有 UserControl 类中代码:
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
namespace InheritedListView{
  /// <summary>
  /// Summary description for UserControl1.
  /// </summary>
  public class MyListView : System.Windows.Forms.ListView
  {
    /// <summary>
    /// Required designer variable.
    /// </summary>
    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
    }
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if( components != null )
        components.Dispose();
      }
      base.Dispose( disposing );
    }
    #region Component Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    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);
    }
  }
}
注意 : 代码应更改 Visual Studio 2005 中。 当您创建 WindowsForms 项目, VisualC # 将一个表单添加到项目默认。 此表单名为 Form 1。 表示形式两文件命名为 Form 1 和 Form1.designer.cs。 Form 1 中编写代码。 Designer.cs 文件是其中 Windows 窗体设计器编写代码实现所有操作您执行通过添加控件。 请有关 WindowsForms 设计器在 Visual C# 2005, 访问以下 MicrosoftWeb 站点:http://msdn2.microsoft.com/en-us/library/ms173077.aspx (http://msdn2.microsoft.com/en-us/library/ms173077.aspx)

5. 保存并生成项目。

创建示例应用程序

1. 请执行下列步骤来创建一个新 Windows 应用程序或 VisualC # 2005 VisualC # .NET 中:
  • 在文件菜单, 指向新建 , 然后单击项目 。
  • 在新建项目对话框中, 单击项目类型,下Visual C # 项目然后再单击模板下面 Windows 应用程序。默认情况下, 创建 Form 1。
  • 注意对于 Visual Studio 2005, 单击 项目类型 下 VisualC # 。
2. 请按照下列步骤来添加该控件与您创建中创建继承 ListView 控件 向 Windows 应用程序部分:
  • 在工具菜单上, 单击 CustomizeToolbox@@@ 。
  • 在 .NET 框架组件选项卡, 单击浏览 。
  • 在打开对话框中, 定位与中创建控件创建继承 ListView 控件部分, 然后单击打开 。 将该控件添加到工具箱以便您可以使用任何其他控件类似控件。
  • 将MyListView从工具箱的常规部分到 Form 1。
3. 将 ComboBox 控件从工具箱的 WindowsForms 部分拖到 Form 1。

4. 在属性窗口是 ComboBox , 将 Name 属性设置为 cbListViewCombo然后将 Visible 属性设置为 False 。

5. 以下代码添加到上面构造函数是 Form 1 类:private ListViewItem lvItem;

6. 以下代码添加到 Form 1 的 Load 事件:
// 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;
}

7. 将以下代码添加到 ComboBox 的 SelectedValueChanged 事件:
// Set text of ListView item to match the ComboBox.lvItem.Text = this.cbListViewCombo.Text;
// Hide the ComboBox.this.cbListViewCombo.Visible = false;

8. 将以下代码添加到 保留 的 ComboBox 事件:
// Set text of ListView item to match the ComboBox.lvItem.Text = this.cbListViewCombo.Text;
// Hide the ComboBox.this.cbListViewCombo.Visible = false;

9. 将以下代码添加到 ComboBox 的 KeyPress 事件:
// 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;
  }
}

10. 将以下代码添加到的 myListView1 MouseUp 事件:
// Get the item on the row that is clicked.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;
  // 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();
}

验证它工作

1. 保存并运行示例。

2. 单击 ListView 中行。 注意, 第一列的当前行位置上出现一个组合框。

3. 以隐藏组合框, 单击组合框中的项目、 按 Esc, 和 ListView 然后滚动或其他控件。 注意, 位于第一列的 ListView 单击行的值, 组合框中单击了。
标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com

文章转载自:CSDN

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP