本文將逐步告訴您,如何使用
下拉式方塊 控制項編輯
ListView 控制項中的資料。這個方法會取代編輯
清單檢視 控制項中的資料標準的文字] 方塊中的方法。
深入了解的技巧
使用
清單檢視 控制項的
LabelEdit 屬性,您可以允許使用者編輯
清單檢視 控制項的內容。若要編輯
清單檢視 控制項中的資料,您可以使用標準的文字方塊。偶爾,您可能想要編輯控制項的另一個控制項。本文將告訴您,如何使用
下拉式方塊 控制項來編輯在
ListView 控制項中的資料,當
清單檢視 控制項是在詳細資料檢視。
當使用者在
ListView 控制項中選取一列時,計算已執行找不到使用者所按之資料列的第一個資料行所度量的矩形。計算會考慮資料行可能不可見或不可能無法完全顯示,當使用者按一下資料列以及
組合方塊 控制項的大小是及適當顯示。
會除了定位和調整
下拉式方塊 控制項大小,這個範例應用程式也監控
ListView 控制項上下列兩個郵件:
每當使用者中垂直或水平捲動
清單檢視 控制項時,就會發生這些訊息。因為
下拉式方塊 控制項實際上不是
清單檢視 控制項的一部分,
ComboBox 控制項無法自動捲動與
清單檢視 控制項。因此,每當這些項目的其中兩個訊息發生於
下拉式方塊 控制項必須被隱藏起來。若要監看的這些訊息,您必須建立自訂的
使用者控制項 類別繼承自
清單檢視 類別。在此自訂控制項中
WndProc 方法會被覆寫,讓所有的訊息檢查的捲動。
附註若要覆寫
WndProc 方法程式碼和呼叫它的任何程式碼必須擁有 Unmanaged 程式碼權限 (
SecurityPermissionUnmanagedCode 旗標指定)。
建立繼承的清單檢視控制項
- 啟動 Microsoft Visual Studio.NET 2003年或 Microsoft Visual Studio 2005。
- 在 [檔案] 功能表上指向 [新增],然後按一下 [專案]。
- 在 [新增專案] 對話方塊按一下 [專案類型 下的 [Visual C++ 專案,然後再按一下 [範本] 下方的 [Windows 控制項程式庫 (.NET)。
附註在 Visual 的 Studio 2005 中按一下 [專案類型,] 下的 [Visual C++],然後按一下 [範本] 下方的 [Windows Form 控制項程式庫]。 - 在 [名稱] 方塊中,輸入 MyListView。在 [位置] 方塊中輸入 c:\test,],然後再按一下 [確定]]。
- 所有的 使用者控制項 類別中的程式碼取代下列程式碼:
#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年中加入常用的語言執行階段支援編譯器選項,請依照下列步驟執行: - 按一下 [專案,] 然後按一下 [<ProjectName> 屬性。
附註<ProjectName> 是專案的名稱的預留位置。 - 展開 [組態屬性,] 然後按一下 [一般。
- 按一下以選取 通用語言執行階段支援、 舊語法 (/ clr:oldSyntax)通用語言執行階段支援 的專案設定中在右窗格中,按一下 套用,],然後再按一下 [確定]。
如需有關通用語言執行階段支援編譯器選項,請造訪下列 Microsoft 網站: - 儲存並建置專案。
建立範例應用程式
- 請依照下列步驟執行,以建立新的 Windows Form Microsoft Visual C++.NET 的應用程式:
- 在 [檔案] 功能表上指向 [新增],然後按一下 [專案]。
- 在 [新增專案] 對話方塊按一下 [專案類型 下的 [Visual C++ 專案,然後再按一下 [範本] 下方的 [Windows Form 應用程式 (.NET)。
附註在 Visual 的 Studio 2005 中按一下 [專案類型,] 下的 [Visual C++],然後按一下 [範本] 下方的 [Windows Form 應用程式]。 - 在 [名稱] 方塊中輸入 Q816188,],然後再按一下 [確定]]。預設情況下,會建立名為 Form1 的表單。
- 請依照下列步驟執行以加入在本文 < "Create the inherited ListView
control" > 一節中加入您的 Windows 應用程式的方式建立的控制項:
- 在 [工具] 功能表上按一下 新增/移除工具箱項目。
- 在 [自訂工具箱] 對話方塊按一下 [.NET Framework 元件] 索引標籤,然後按一下 [瀏覽]。
- 在 [開啟舊檔] 對話方塊找出您在 ["Create the
inherited ListView control"] 區段中建立的控制項,然後按一下 [開啟舊檔]。此步驟新增至 [工具箱這個控制項,以便您可以使用同樣的到任何其他控制項控制項。
- 將 MyListView 控制項加入至 Form1。
- 將 下拉式方塊 控制項加入至 Form1。
- 下拉式方塊 控制項的 [屬性] 視窗中將的 Name 屬性的值設定為 cbListViewCombo,然後將 看得見] 屬性設為 False]。
- 將下列程式碼加入至 Form1 的類別建構函式的上方:
private:
ListViewItem *lvItem;
- 將下列程式碼加入至 Form1 的 Load 事件:
// 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;
} - 將下列程式碼加入至 下拉式列示方塊 控制項的 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;
- 將下列程式碼加入至 下拉式列示方塊 控制項的 保留 事件:
// Set text of the ListView item to match the ComboBox control.
lvItem->Text = this->cbListViewCombo->Text;
// Hide the ComboBox control.
this->cbListViewCombo->Visible = false;
- 將下列程式碼加入至 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;
}
} - 將下列程式碼加入至 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();
}
請確認它可以運作
- 儲存並執行範例。
- 按一下在 清單檢視 控制項中的資料列。
請注意下拉式方塊出現在目前資料列的第一欄的位置上。 - 來隱藏下拉式方塊、 按一下下拉式方塊中的項目、 按 ESC 鍵,並再捲動 清單檢視 控制項或是按一下 [另一個控制項。
請注意在下拉式方塊中按一下將值放在第一欄,按下的資料列的 清單檢視 控制項。