文章編號: 816188 - 上次校閱: 2005年12月30日 - 版次: 2.0

如何使用下拉式方塊控制項,ListView 控制項在 Visual C++.NET 或 Visual C++ 2005年中編輯資料

系統提示本文適用於您使用的作業系統之外的作業系統。與您不相關的文章內容已停用。

在此頁中

全部展開 | 全部摺疊

結論

本文將逐步告訴您,如何使用 下拉式方塊 控制項編輯 ListView 控制項中的資料。這個方法會取代編輯 清單檢視 控制項中的資料標準的文字] 方塊中的方法。

深入了解的技巧

使用 清單檢視 控制項的 LabelEdit 屬性,您可以允許使用者編輯 清單檢視 控制項的內容。若要編輯 清單檢視 控制項中的資料,您可以使用標準的文字方塊。偶爾,您可能想要編輯控制項的另一個控制項。本文將告訴您,如何使用 下拉式方塊 控制項來編輯在 ListView 控制項中的資料,當 清單檢視 控制項是在詳細資料檢視。

當使用者在 ListView 控制項中選取一列時,計算已執行找不到使用者所按之資料列的第一個資料行所度量的矩形。計算會考慮資料行可能不可見或不可能無法完全顯示,當使用者按一下資料列以及 組合方塊 控制項的大小是及適當顯示。

會除了定位和調整 下拉式方塊 控制項大小,這個範例應用程式也監控 ListView 控制項上下列兩個郵件:
  • WM_VSCROLL
  • WM_HSCROLL。
每當使用者中垂直或水平捲動 清單檢視 控制項時,就會發生這些訊息。因為 下拉式方塊 控制項實際上不是 清單檢視 控制項的一部分,ComboBox 控制項無法自動捲動與 清單檢視 控制項。因此,每當這些項目的其中兩個訊息發生於 下拉式方塊 控制項必須被隱藏起來。若要監看的這些訊息,您必須建立自訂的 使用者控制項 類別繼承自 清單檢視 類別。在此自訂控制項中 WndProc 方法會被覆寫,讓所有的訊息檢查的捲動。

附註若要覆寫 WndProc 方法程式碼和呼叫它的任何程式碼必須擁有 Unmanaged 程式碼權限 (SecurityPermissionUnmanagedCode 旗標指定)。

建立繼承的清單檢視控制項

  1. 啟動 Microsoft Visual Studio.NET 2003年或 Microsoft Visual Studio 2005。
  2. 在 [檔案] 功能表上指向 [新增],然後按一下 [專案]。
  3. 在 [新增專案] 對話方塊按一下 [專案類型 下的 [Visual C++ 專案,然後再按一下 [範本] 下方的 [Windows 控制項程式庫 (.NET)

    附註在 Visual 的 Studio 2005 中按一下 [專案類型,] 下的 [Visual C++],然後按一下 [範本] 下方的 [Windows Form 控制項程式庫]。
  4. 在 [名稱] 方塊中,輸入 MyListView。在 [位置] 方塊中輸入 c:\test,],然後再按一下 [確定]]。
  5. 所有的 使用者控制項 類別中的程式碼取代下列程式碼:
    #pragma once
    
    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;
    
    namespace MyListView
    {
    	/// <summary> 
    	/// Summary for MyListViewControl
    	/// </summary>
    	///
    	/// WARNING: If you change the name of this class, you must change the 
    	///          'Resource File Name' property for the managed resource compiler tool 
    	///          that is associated with all .resx files that this class depends on.  
     ///          Otherwise, the designers cannot interact properly with localized
    	///          resources that are associated with this form.
    public __gc class MyListViewControl : public System::Windows::Forms::ListView
    	{	
    	public:
    		MyListViewControl(void)	
    		{
    			InitializeComponent();
    		}
    		
    	protected:
    		void Dispose(Boolean disposing)	
    		{
    			if (disposing && components)
    			{
    				components->Dispose();
    			}
    			__super::Dispose(disposing);
    		}
    		
    	private:
    		/// <summary>
    		/// Required designer variable.
    		/// </summary>
    		System::ComponentModel::Container* components;
    
    		/// <summary>
    		/// Required method for Designer support - do not modify
    		/// the contents of this method with the code editor.
    		/// </summary>
    		void InitializeComponent(void)
    		{
    		
    		}
    
     	private:
    		static const int WM_HSCROLL = 0x114;
    		static const int WM_VSCROLL = 0x115;
    
    	protected:
    		void WndProc(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 control to cause the ComboBox control to lose focus.
    				this->Focus();  
    			}
                
    			// Pass the message to default handler.
    			__super::WndProc(msg);
    		} 
    	};
    }
    附註 您必須加入通用語言執行階段支援編譯器選項 (/ clr:oldSyntax) 來順利編譯整個程式碼範例的 Visual C++ 2005年中。 若要在 Visual C++ 2005年中加入常用的語言執行階段支援編譯器選項,請依照下列步驟執行:
    1. 按一下 [專案,] 然後按一下 [<ProjectName> 屬性

      附註<ProjectName> 是專案的名稱的預留位置。
    2. 展開 [組態屬性,] 然後按一下 [一般
    3. 按一下以選取 通用語言執行階段支援、 舊語法 (/ clr:oldSyntax)通用語言執行階段支援 的專案設定中在右窗格中,按一下 套用,],然後再按一下 [確定]
    如需有關通用語言執行階段支援編譯器選項,請造訪下列 Microsoft 網站:
    / clr (通用語言執行階段編譯)
    http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx (http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx)
  6. 儲存並建置專案。

建立範例應用程式

  1. 請依照下列步驟執行,以建立新的 Windows Form Microsoft Visual C++.NET 的應用程式:
    1. 在 [檔案] 功能表上指向 [新增],然後按一下 [專案]。
    2. 在 [新增專案] 對話方塊按一下 [專案類型 下的 [Visual C++ 專案,然後再按一下 [範本] 下方的 [Windows Form 應用程式 (.NET)

      附註在 Visual 的 Studio 2005 中按一下 [專案類型,] 下的 [Visual C++],然後按一下 [範本] 下方的 [Windows Form 應用程式]。
    3. 在 [名稱] 方塊中輸入 Q816188,],然後再按一下 [確定]]。預設情況下,會建立名為 Form1 的表單。
  2. 請依照下列步驟執行以加入在本文 < "Create the inherited ListView control" > 一節中加入您的 Windows 應用程式的方式建立的控制項:
    1. 在 [工具] 功能表上按一下 新增/移除工具箱項目
    2. 在 [自訂工具箱] 對話方塊按一下 [.NET Framework 元件] 索引標籤,然後按一下 [瀏覽]
    3. 在 [開啟舊檔] 對話方塊找出您在 ["Create the inherited ListView control"] 區段中建立的控制項,然後按一下 [開啟舊檔]。此步驟新增至 [工具箱這個控制項,以便您可以使用同樣的到任何其他控制項控制項。
    4. MyListView 控制項加入至 Form1
  3. 下拉式方塊 控制項加入至 Form1
  4. 下拉式方塊 控制項的 [屬性] 視窗中將的 Name 屬性的值設定為 cbListViewCombo,然後將 看得見] 屬性設為 False]。
  5. 將下列程式碼加入至 Form1 的類別建構函式的上方:
    private:
    	ListViewItem *lvItem;
  6. 將下列程式碼加入至 Form1Load 事件:
    // Add some items to the combo box list.
    this->cbListViewCombo->Items->Add(S"NC");
    this->cbListViewCombo->Items->Add(S"WA");
    
    // Set the view of the ListView control to Details.
    this->myListViewControl1->View = View::Details;
    
    // Turn on full row select.
    this->myListViewControl1->FullRowSelect = true;
    			
    // Add data to the ListView control.
    ColumnHeader *columnheader;
    ListViewItem *listviewitem;
    
    // Create sample ListView data.
    listviewitem = new ListViewItem(S"NC");
    listviewitem->SubItems->Add(S"North Carolina");
    this->myListViewControl1->Items->Add(listviewitem);
    
    listviewitem = new ListViewItem(S"WA");
    listviewitem->SubItems->Add(S"Washington");
    this->myListViewControl1->Items->Add(listviewitem);
    			
    // Create column headers for the data.
    columnheader = new ColumnHeader();
    columnheader->Text = S"State Abbreviation";
    this->myListViewControl1->Columns->Add(columnheader);
    
    columnheader = new ColumnHeader();
    columnheader->Text = S"State";
    this->myListViewControl1->Columns->Add(columnheader);
    
    // Loop through and size each column header to fit the column header text.
    ColumnHeader *ch;
    IEnumerator *ie = this->myListViewControl1->Columns->GetEnumerator();
    while (ie->MoveNext())
    {
    	ch = __try_cast<ColumnHeader *> (ie->Current);
    	ch->Width = -2;
    }
  7. 將下列程式碼加入至 下拉式列示方塊 控制項的 SelectedValueChanged 事件:
    // Set the text of the ListView item to match the ComboBox control.
    lvItem->Text = this->cbListViewCombo->Text;
    
    // Hide the ComboBox control.
    this->cbListViewCombo->Visible = false;
  8. 將下列程式碼加入至 下拉式列示方塊 控制項的 保留 事件:
    // Set text of the ListView item to match the ComboBox control.
    lvItem->Text = this->cbListViewCombo->Text;
    
    // Hide the ComboBox control.
    this->cbListViewCombo->Visible = false;
    
  9. 將下列程式碼加入至 KeyPress下拉式方塊 控制項的事件:
    // Verify that the user presses the ESC key.
    switch (e->KeyChar)
    {
    case (char)(int)Keys::Escape:
    {
    	// Reset the original text value, and then hide the ComboBox control.
    	this->cbListViewCombo->Text = lvItem->Text;
    	this->cbListViewCombo->Visible = false;
    	break;
    }
    case (char)(int)Keys::Enter:
    {
    	// Hide the ComboBox control.
    	this->cbListViewCombo->Visible = false;
    	break;
    }
    }
  10. 將下列程式碼加入至 MouseUpmyListViewContro11 控制項的事件:
    // Get the item on the row that the user clicks.
    lvItem = this->myListViewControl1->GetItemAt(e->get_X(), e->get_Y());
    			 
    // Make sure that an item the user clicks.
    if (lvItem != NULL)
    {
     // Get the bounds of the item that the user clicks.
     Rectangle ClickedItem = lvItem->Bounds;
                     
     // Verify that the column is completely scrolled off to the left.
     if ((ClickedItem.Left + this->myListViewControl1->Columns->get_Item(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 the column extends beyond the right side of the ListView control.
     if ((ClickedItem.Left + this->myListViewControl1->Columns->get_Item(0)->Width) > this->myListViewControl1->Width)
     {
      // Set the width of the column to match the width of the ListView control.
      ClickedItem.Width = this->myListViewControl1->Width;
      ClickedItem.X = 0;
     }
     else
     {
      // The right side of the cell is in view.
      ClickedItem.Width = this->myListViewControl1->Columns->get_Item(0)->Width + ClickedItem.Left;
      ClickedItem.X = 2;
     }					
    }
    else if (this->myListViewControl1->Columns->get_Item(0)->Width > this->myListViewControl1->Width)
    {
     ClickedItem.Width = this->myListViewControl1->Width;
    }
    else
    {
     ClickedItem.Width = this->myListViewControl1->Columns->get_Item(0)->Width;
     ClickedItem.X = 2;
    }				
    	 
    // Adjust the top to account for the location of the ListView control.
    ClickedItem.Y += this->myListViewControl1->Top;
    ClickedItem.X += this->myListViewControl1->Left;	
    
    // Assign calculated bounds to the ComboBox control.
    this->cbListViewCombo->Bounds = ClickedItem;
                     
    // Set default text for the ComboBox control to match the item that the user clicks.
    this->cbListViewCombo->Text = lvItem->Text;
    
    // Display the ComboBox control, and then make sure that it is on top with focus.
    this->cbListViewCombo->Visible = true;
    this->cbListViewCombo->BringToFront();
    this->cbListViewCombo->Focus();
    }
    

請確認它可以運作

  1. 儲存並執行範例。
  2. 按一下在 清單檢視 控制項中的資料列。

    請注意下拉式方塊出現在目前資料列的第一欄的位置上。
  3. 來隱藏下拉式方塊、 按一下下拉式方塊中的項目、 按 ESC 鍵,並再捲動 清單檢視 控制項或是按一下 [另一個控制項。

    請注意在下拉式方塊中按一下將值放在第一欄,按下的資料列的 清單檢視 控制項。

這篇文章中的資訊適用於:
  • Microsoft Visual C++ 2005 Express Edition
  • Microsoft Visual C++ .NET 2003 Standard Edition
關鍵字:?
kbmt kbwindowsforms kbcode kbprogramming kblistview kbcontrol kbcombobox kbhowtomaster KB816188 KbMtzh
機器翻譯機器翻譯
重要:本文是以 Microsoft 機器翻譯軟體翻譯而成,而非使用人工翻譯而成。Microsoft 同時提供使用者人工翻譯及機器翻譯兩個版本的文章,讓使用者可以依其使用語言使用知識庫中的所有文章。但是,機器翻譯的文章可能不盡完美。這些文章中也可能出現拼字、語意或文法上的錯誤,就像外國人在使用本國語言時可能發生的錯誤。Microsoft 不為內容的翻譯錯誤或客戶對該內容的使用所產生的任何錯誤或損害負責。Microsoft也同時將不斷地就機器翻譯軟體進行更新。
按一下這裡查看此文章的英文版本:816188? (http://support.microsoft.com/kb/816188/en-us/ )
Microsoft及(或)其供應商不就任何在本伺服器上發表的文字資料及其相關圖表資訊的恰當性作任何承諾。所有文字資料及其相關圖表均以「現狀」供應,不負任何擔保責任。Microsoft及(或)其供應商謹此聲明,不負任何對與此資訊有關之擔保責任,包括關於適售性、適用於某一特定用途、權利或不侵權的明示或默示擔保責任。Microsoft及(或)其供應商無論如何不對因或與使用本伺服器上資訊或與資訊的實行有關而引起的契約、過失或其他侵權行為之訴訟中的特別的、間接的、衍生性的損害或任何因使用而喪失所導致的之損害、資料或利潤負任何責任。