Bu adım adım makalede,
ComboBox denetimi
ListView denetimdeki verileri düzenlemek için nasıl kullanılacağı açıklanır. Bu yöntem, standart bir metin kutusu yaklaşım,
ListView denetimdeki verileri düzenleme yerini alır.
Bu yöntem hakkında bilgi edinin.
ListView denetimin
LabelEdit özelliğini kullanarak,
ListView denetimin içeriğini düzenlemek kullanıcının izin verebilirsiniz.
ListView denetimdeki verileri düzenlemek için <a0></a0>, standart bir metin kutusu kullanabilirsiniz. Bazen, denetimi'ni düzenlemek için başka bir denetime başvurabilirsiniz. Bu makalede,
ComboBox denetimi
ListView denetim Ayrıntılar görünümünde olduğunda,
ListView denetimdeki verileri düzenlemek için nasıl kullanılacağı açıklanır.
Kullanıcı bir satır
ListView denetimde seçtiğinde, hesaplama kullanıcının satırın ilk sütun için bir sınırlama dikdörtgen bulmak için gerçekleştirilir. Hesaplama sütunu görünmeyebilir veya yok olabilir dikkate kullanıcı satır tıklattığınızda ve
ComboBox denetimi boyutlandırılır ve uygun biçimde görüntülenir tam olarak görülebilir.
Konumlandırma ve
ComboBox denetimi boyutlandırma yanı sıra, bu örnek uygulama
ListView denetimi aşağıdaki iki iletiler için de izler:
Bu iletiler, kullanıcı
ListView denetimini dikey veya yatay olarak kaydırır olduğunda oluşur.
ComboBox denetimi fiziksel
ListView denetimini bir parçası olmadığından,
ComboBox denetimi otomatik olarak
ListView denetimle kaydırma değil. Bu nedenle, her ikisinden biri bu iki iletileri
ComboBox denetimi gizli oluşur. Bu iletileri izlemek için <a0></a0>,
ListView sınıfından devralan bir özel
UserControl sınıf oluşturmanız gerekir. Bu özel bir denetimde kaydırma için denetlenecek tüm iletilere izin vermek için
WndProc yöntemi geçersiz kılınır.
NotWndProc geçersiz kılmak için kod izinleri (
SecurityPermission belirtilen
UnmanagedCode bayrağıyla) yöntemi, hem de, kodunuzu ve onu çağıran herhangi bir kod yönetilmeyen gerekir.
Devralınan ListView denetim oluşturma
- Microsoft Visual Studio .NET 2003 veya Microsoft Visual Studio 2005'i başlatın.
- Dosya menüsünde Yeni ' nin üzerine gelin ve sonra Project ' i tıklatın.
- Yeni proje) iletişim kutusunda, Visual C++ ProjectsProject Types altında tıklatın ve şablonları altında .NET (Windows Denetim Kitaplığı)'ı tıklatın.
Not BVisual Studio 2005 ' altında Project TypesVisual C++ ' ı tıklatın ve sonra Windows Forms denetim kitaplığışablonları altında'ı tıklatın. - Ad kutusuna MyListView yazın. Konum kutusu, C:\Test yazın ve Tamam ' ı tıklatın.
- Tüm UserControl sınıf kodu aşağıdaki kodla değiştirin: bir kez ad sistem kullanarak <a1>Ad</a1> System::ComponentModel kullanılarak; <a1>Ad</a1> System::Collections kullanılarak; ad System::Windows::Forms kullanarak; <a1>Ad</a1> System::Data kullanılarak; <a1>Ad</a1> System::Drawing kullanılarak
#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);
}
};
} Common language runtime eklemeniz gerekir Not desteği derleyici seçeneği (/ clr:oldSyntax) başarıyla tüm kod örneğini derlemek için Visual C++ 2005'te. Visual C++ 2005'te ortak dil çalışma zamanı destek derleyici seçeneği eklemek için aşağıdaki adımları izleyin: - Project tıklatın ve sonra <ProjectName> Özellikleri.
Not<ProjectName> projenin adı için yer tutucudur. - Yapılandırma özellikleri ' ni genişletin ve sonra Genel ' i tıklatın.
- Seçmek için tıklatın ortak dil çalışma zamanı modülü ve Destek, eski sözdizimi (/ clr:oldSyntax)Common Language Runtime desteği proje ayarında sağ bölmede, Uygula ' yı tıklatın ve sonra Tamam ' ı tıklatın.
Ortak dil çalıştırma hakkında daha fazla bilgi için derleyici seçeneği destekleyen, aşağıdaki Microsoft Web sitesini ziyaret edin: - Kaydetmek ve projeyi derleyin.
Örnek uygulama
- Yeni bir Windows formları oluşturmak için bu adımları, Visual C++ .NET uygulama:
- Dosya menüsünde Yeni ' nin üzerine gelin ve sonra Project ' i tıklatın.
- Yeni proje) iletişim kutusunda, Visual C++ ProjectsProject Types altında tıklatın ve şablonları altında Windows Forms uygulaması (.NET)'ı tıklatın.
Not Visual Studio 2005 ' te Project Types altında Visual C++ ' ı tıklatın ve şablonları altında Windows Forms uygulama ' yı tıklatın. - Adı kutusuna, Q816188 yazın ve Tamam ' ı tıklatın. Varsayılan olarak, Form1 adlı bir form oluşturulur.
- Bu makalenin "Create the inherited ListView
control" bölümüne Windows uygulamanız için oluşturduğunuz bir denetim eklemek için şu adımları izleyin:
- Araçlar menüsünde Ekle/Kaldır araç kutusu öğeleri.
- Toolbox iletişim kutusunda, .NET Framework bileşenleri</a1> sekmesini tıklatın ve sonra da <a2>Gözat</a2>'ı tıklatın.
- Aç iletişim kutusunda, "Create the
inherited ListView control" bölümünde oluşturduğunuz denetimini bulun ve sonra Aç ' ı tıklatın. Bu adım, bu denetim araç kutusuna ekler, böylece benzer şekilde bir başka denetime denetimi kullanabilirsiniz.
- MyListView denetim için Form1 ekleyin.
- Bir ComboBox denetimi için Form1 ekleyin.
- ComboBox denetimi Properties penceresinde cbListViewCombo için <a0>ad</a0> özelliğinin değerini ayarlamak ve Visible özelliğini false olarak ayarlayın.
- Form1 sınıf oluşturucu üstünde aşağıdaki kodu ekleyin:
private:
ListViewItem *lvItem;
- Form1 <a1>Load</a1> olayı aşağıdaki kodu ekleyin:
// 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;
} - ComboBox denetimi SelectedValueChanged olayına aşağıdaki kodu ekleyin:
// 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;
- ComboBox denetimi Bırak olayı aşağıdaki kodu ekleyin:
// Set text of the ListView item to match the ComboBox control.
lvItem->Text = this->cbListViewCombo->Text;
// Hide the ComboBox control.
this->cbListViewCombo->Visible = false;
- Aşağıdaki kodu için KeyPress ekleyin ComboBox denetiminin olayı:
// 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;
}
} - Aşağıdaki kodu için MouseUp ekleyin myListViewContro11 denetiminin olayı:
// 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();
}
Çalıştığından emin olun.
- Kaydedin ve sonra örnek çalıştırın.
- ListView denetimini bir satırda Ek Yardım düğmesini tıklatın.
Birleşik giriş kutusunun üzerinde ilk sütun geçerli satırın konumunu görüntülendiğine dikkat edin. - Birleşik giriş kutusunu gizlemek için <a0></a0>, birleşik giriş kutusundaki bir öğeyi tıklatın, ESC, basın ve liste görünümü denetimi aracılığıyla kaydırarak veya başka bir denetimi tıklatın.
Birleşik giriş kutusunda tıklattığınız değeri ListView denetiminin tıklattığınız satırın ilk sütunda yerleştirilir dikkat edin.