ListView.ColumnHeaderCollection Classe
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Représente la collection d’en-têtes de colonne dans un ListView contrôle.
public: ref class ListView::ColumnHeaderCollection : System::Collections::IList
public class ListView.ColumnHeaderCollection : System.Collections.IList
[System.ComponentModel.ListBindable(false)]
public class ListView.ColumnHeaderCollection : System.Collections.IList
type ListView.ColumnHeaderCollection = class
interface IList
interface ICollection
interface IEnumerable
[<System.ComponentModel.ListBindable(false)>]
type ListView.ColumnHeaderCollection = class
interface IList
interface ICollection
interface IEnumerable
Public Class ListView.ColumnHeaderCollection
Implements IList
- Héritage
-
ListView.ColumnHeaderCollection
- Attributs
- Implémente
Exemples
L’exemple de code suivant crée un formulaire qui contient un ListView contrôle qui trie manuellement les éléments lorsqu’une colonne du ListView contrôle est cliquée. L’exemple définit une classe appelée ListViewItemComparer qui implémente l’interface System.Collections.IComparer qui effectue la ListViewItem comparaison. L’exemple crée une instance de ListViewItemComparer et l’utilise pour définir la ListViewItemSorter propriété du ListView contrôle. L’appel Sort de méthode dans le ColumnClick gestionnaire d’événements utilise les méthodes définies pour ListViewItemComparer effectuer le tri des éléments, en fonction de la colonne qui est cliquée.
#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>
using namespace System;
using namespace System::Windows::Forms;
using namespace System::Drawing;
using namespace System::Collections;
// Implements the manual sorting of items by columns.
ref class ListViewItemComparer: public IComparer
{
private:
int col;
public:
ListViewItemComparer()
{
col = 0;
}
ListViewItemComparer( int column )
{
col = column;
}
virtual int Compare( Object^ x, Object^ y )
{
return String::Compare( (dynamic_cast<ListViewItem^>(x))->SubItems[ col ]->Text,
(dynamic_cast<ListViewItem^>(y))->SubItems[ col ]->Text );
}
};
public ref class ListViewSortForm: public Form
{
private:
ListView^ listView1;
public:
ListViewSortForm()
{
// Create ListView items to add to the control.
array<String^>^temp0 = {"Banana","a","b","c"};
ListViewItem^ listViewItem1 = gcnew ListViewItem( temp0,-1,Color::Empty,Color::Yellow,nullptr );
array<String^>^temp1 = {"Cherry","v","g","t"};
ListViewItem^ listViewItem2 = gcnew ListViewItem( temp1,-1,Color::Empty,Color::Red,
gcnew System::Drawing::Font( "Microsoft Sans Serif",8.25F,FontStyle::Regular,GraphicsUnit::Point,0 ) );
array<String^>^temp2 = {"Apple","h","j","n"};
ListViewItem^ listViewItem3 = gcnew ListViewItem( temp2,-1,Color::Empty,Color::Lime,nullptr );
array<String^>^temp3 = {"Pear","y","u","i"};
ListViewItem^ listViewItem4 = gcnew ListViewItem( temp3,-1,Color::Empty,Color::FromArgb( 192, 128, 156 ),nullptr );
//Initialize the ListView control and add columns to it.
this->listView1 = gcnew ListView;
// Set the initial sorting type for the ListView.
this->listView1->Sorting = SortOrder::None;
// Disable automatic sorting to enable manual sorting.
this->listView1->View = View::Details;
// Add columns and set their text.
this->listView1->Columns->Add( gcnew ColumnHeader );
this->listView1->Columns[ 0 ]->Text = "Column 1";
this->listView1->Columns[ 0 ]->Width = 100;
listView1->Columns->Add( gcnew ColumnHeader );
listView1->Columns[ 1 ]->Text = "Column 2";
listView1->Columns->Add( gcnew ColumnHeader );
listView1->Columns[ 2 ]->Text = "Column 3";
listView1->Columns->Add( gcnew ColumnHeader );
listView1->Columns[ 3 ]->Text = "Column 4";
// Suspend control logic until form is done configuring form.
this->SuspendLayout();
// Add Items to the ListView control.
array<ListViewItem^>^temp4 = {listViewItem1,listViewItem2,listViewItem3,listViewItem4};
this->listView1->Items->AddRange( temp4 );
// Set the location and size of the ListView control.
this->listView1->Location = Point(10,10);
this->listView1->Name = "listView1";
this->listView1->Size = System::Drawing::Size( 300, 100 );
this->listView1->TabIndex = 0;
// Enable editing of the items in the ListView.
this->listView1->LabelEdit = true;
// Connect the ListView::ColumnClick event to the ColumnClick event handler.
this->listView1->ColumnClick += gcnew ColumnClickEventHandler( this, &ListViewSortForm::ColumnClick );
// Initialize the form.
this->ClientSize = System::Drawing::Size( 400, 400 );
array<Control^>^temp5 = {this->listView1};
this->Controls->AddRange( temp5 );
this->Name = "ListViewSortForm";
this->Text = "Sorted ListView Control";
// Resume lay[Out] of* the form.
this->ResumeLayout( false );
}
private:
// ColumnClick event handler.
void ColumnClick( Object^ /*o*/, ColumnClickEventArgs^ e )
{
// Set the ListViewItemSorter property to a new ListViewItemComparer
// object. Setting this property immediately sorts the
// ListView using the ListViewItemComparer object.
this->listView1->ListViewItemSorter = gcnew ListViewItemComparer( e->Column );
}
};
[System::STAThreadAttribute]
int main()
{
Application::Run( gcnew ListViewSortForm );
}
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Collections;
namespace ListViewSortFormNamespace
{
public class ListViewSortForm : Form
{
private ListView listView1;
public ListViewSortForm()
{
// Create ListView items to add to the control.
ListViewItem listViewItem1 = new ListViewItem(new string[] {"Banana","a","b","c"}, -1, Color.Empty, Color.Yellow, null);
ListViewItem listViewItem2 = new ListViewItem(new string[] {"Cherry","v","g","t"}, -1, Color.Empty, Color.Red, new Font("Microsoft Sans Serif", 8.25F, FontStyle.Regular, GraphicsUnit.Point, ((System.Byte)(0))));
ListViewItem listViewItem3 = new ListViewItem(new string[] {"Apple","h","j","n"}, -1, Color.Empty, Color.Lime, null);
ListViewItem listViewItem4 = new ListViewItem(new string[] {"Pear","y","u","i"}, -1, Color.Empty, Color.FromArgb(((System.Byte)(192)), ((System.Byte)(128)), ((System.Byte)(156))), null);
//Initialize the ListView control and add columns to it.
this.listView1 = new ListView();
// Set the initial sorting type for the ListView.
this.listView1.Sorting = SortOrder.None;
// Disable automatic sorting to enable manual sorting.
this.listView1.View = View.Details;
// Add columns and set their text.
this.listView1.Columns.Add(new ColumnHeader());
this.listView1.Columns[0].Text = "Column 1";
this.listView1.Columns[0].Width = 100;
listView1.Columns.Add(new ColumnHeader());
listView1.Columns[1].Text = "Column 2";
listView1.Columns.Add(new ColumnHeader());
listView1.Columns[2].Text = "Column 3";
listView1.Columns.Add(new ColumnHeader());
listView1.Columns[3].Text = "Column 4";
// Suspend control logic until form is done configuring form.
this.SuspendLayout();
// Add Items to the ListView control.
this.listView1.Items.AddRange(new ListViewItem[] {listViewItem1,
listViewItem2,
listViewItem3,
listViewItem4});
// Set the location and size of the ListView control.
this.listView1.Location = new Point(10, 10);
this.listView1.Name = "listView1";
this.listView1.Size = new Size(300, 100);
this.listView1.TabIndex = 0;
// Enable editing of the items in the ListView.
this.listView1.LabelEdit = true;
// Connect the ListView.ColumnClick event to the ColumnClick event handler.
this.listView1.ColumnClick += new ColumnClickEventHandler(ColumnClick);
// Initialize the form.
this.ClientSize = new Size(400, 400);
this.Controls.AddRange(new Control[] {this.listView1});
this.Name = "ListViewSortForm";
this.Text = "Sorted ListView Control";
// Resume layout of the form.
this.ResumeLayout(false);
}
// ColumnClick event handler.
private void ColumnClick(object o, ColumnClickEventArgs e)
{
// Set the ListViewItemSorter property to a new ListViewItemComparer
// object. Setting this property immediately sorts the
// ListView using the ListViewItemComparer object.
this.listView1.ListViewItemSorter = new ListViewItemComparer(e.Column);
}
[System.STAThreadAttribute()]
public static void Main()
{
Application.Run(new ListViewSortForm());
}
}
// Implements the manual sorting of items by columns.
class ListViewItemComparer : IComparer
{
private int col;
public ListViewItemComparer()
{
col = 0;
}
public ListViewItemComparer(int column)
{
col = column;
}
public int Compare(object x, object y)
{
return String.Compare(((ListViewItem)x).SubItems[col].Text, ((ListViewItem)y).SubItems[col].Text);
}
}
}
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Collections
Namespace ListViewSortFormNamespace
Public Class ListViewSortForm
Inherits Form
Private listView1 As ListView
Public Sub New()
' Create ListView items to add to the control.
Dim listViewItem1 As New ListViewItem(New String() {"Banana", "a", "b", "c"}, -1, Color.Empty, Color.Yellow, Nothing)
Dim listViewItem2 As New ListViewItem(New String() {"Cherry", "v", "g", "t"}, -1, Color.Empty, Color.Red, New Font("Microsoft Sans Serif", 8.25F, FontStyle.Regular, GraphicsUnit.Point, CType(0, System.Byte)))
Dim listViewItem3 As New ListViewItem(New String() {"Apple", "h", "j", "n"}, -1, Color.Empty, Color.Lime, Nothing)
Dim listViewItem4 As New ListViewItem(New String() {"Pear", "y", "u", "i"}, -1, Color.Empty, Color.FromArgb(CType(192, System.Byte), CType(128, System.Byte), CType(156, System.Byte)), Nothing)
'Initialize the ListView control and add columns to it.
Me.listView1 = New ListView
' Set the initial sorting type for the ListView.
Me.listView1.Sorting = SortOrder.None
' Disable automatic sorting to enable manual sorting.
Me.listView1.View = View.Details
' Add columns and set their text.
Me.listView1.Columns.Add(New ColumnHeader)
Me.listView1.Columns(0).Text = "Column 1"
Me.listView1.Columns(0).Width = 100
listView1.Columns.Add(New ColumnHeader)
listView1.Columns(1).Text = "Column 2"
listView1.Columns.Add(New ColumnHeader)
listView1.Columns(2).Text = "Column 3"
listView1.Columns.Add(New ColumnHeader)
listView1.Columns(3).Text = "Column 4"
' Suspend control logic until form is done configuring form.
Me.SuspendLayout()
' Add Items to the ListView control.
Me.listView1.Items.AddRange(New ListViewItem() {listViewItem1, listViewItem2, listViewItem3, listViewItem4})
' Set the location and size of the ListView control.
Me.listView1.Location = New Point(10, 10)
Me.listView1.Name = "listView1"
Me.listView1.Size = New Size(300, 100)
Me.listView1.TabIndex = 0
' Enable editing of the items in the ListView.
Me.listView1.LabelEdit = True
' Connect the ListView.ColumnClick event to the ColumnClick event handler.
AddHandler Me.listView1.ColumnClick, AddressOf ColumnClick
' Initialize the form.
Me.ClientSize = New Size(400, 400)
Me.Controls.AddRange(New Control() {Me.listView1})
Me.Name = "ListViewSortForm"
Me.Text = "Sorted ListView Control"
' Resume layout of the form.
Me.ResumeLayout(False)
End Sub
' ColumnClick event handler.
Private Sub ColumnClick(ByVal o As Object, ByVal e As ColumnClickEventArgs)
' Set the ListViewItemSorter property to a new ListViewItemComparer
' object. Setting this property immediately sorts the
' ListView using the ListViewItemComparer object.
Me.listView1.ListViewItemSorter = New ListViewItemComparer(e.Column)
End Sub
End Class
' Implements the manual sorting of items by columns.
Class ListViewItemComparer
Implements IComparer
Private col As Integer
Public Sub New()
col = 0
End Sub
Public Sub New(ByVal column As Integer)
col = column
End Sub
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer _
Implements IComparer.Compare
Return [String].Compare(CType(x, ListViewItem).SubItems(col).Text, CType(y, ListViewItem).SubItems(col).Text)
End Function
End Class
End Namespace
Remarques
Une ListView.ColumnHeaderCollection classe stocke les en-têtes de colonne affichés dans le ListView contrôle lorsque la View propriété est définie Detailssur . ColumnHeader Les ListView.ColumnHeaderCollection objets qui définissent le texte à afficher pour une colonne ainsi que la façon dont l’en-tête de colonne est affiché dans le contrôle lors de l’affichage ListView des colonnes. Lorsqu’un ListView affiche des colonnes, les éléments et leurs sous-éléments sont affichés dans leurs propres colonnes. Pour spécifier les colonnes sous lesquelles les données sont affichées, consultez la ListViewItem.ListViewSubItemCollection classe.
Il existe plusieurs façons d’ajouter des en-têtes de colonne à la collection. La Add méthode ajoute un en-tête de colonne unique à la collection. Pour ajouter un certain nombre d’en-têtes de colonne à la collection, vous créez un tableau d’objets ColumnHeader et passez-le à la AddRange méthode. Si vous souhaitez insérer un en-tête de colonne à un emplacement spécifique dans la collection, vous pouvez utiliser la Insert méthode. Pour supprimer des en-têtes de colonne, vous pouvez utiliser la Remove méthode ou la RemoveAt méthode si vous savez où se trouve l’en-tête de colonne dans la collection. La Clear méthode vous permet de supprimer tous les en-têtes de colonne de la collection au lieu d’utiliser la méthode pour supprimer un en-tête de colonne unique à la Remove fois.
Outre les méthodes et propriétés permettant d’ajouter et de supprimer des en-têtes de colonne, les ListView.ColumnHeaderCollection méthodes permettent également de rechercher des en-têtes de colonne dans la collection. La Contains méthode vous permet de déterminer si un en-tête de colonne est membre de la collection. Une fois que vous savez que l’en-tête de colonne se trouve dans la collection, vous pouvez utiliser la IndexOf méthode pour déterminer où se trouve l’en-tête de colonne dans la collection.
Note
Un comportement inattendu peut se produire lorsque la largeur combinée de toutes les colonnes dépasse 32 768 pixels.
Constructeurs
| Nom | Description |
|---|---|
| ListView.ColumnHeaderCollection(ListView) |
Initialise une nouvelle instance de la classe ListView.ColumnHeaderCollection. |
Propriétés
| Nom | Description |
|---|---|
| Count |
Obtient le nombre d'éléments dans la collection. |
| IsReadOnly |
Obtient une valeur indiquant si la collection est en lecture seule. |
| Item[Int32] |
Obtient l’en-tête de colonne à l’index spécifié dans la collection. |
| Item[String] |
Obtient l’en-tête de colonne avec la clé spécifiée de la collection. |
Méthodes
| Nom | Description |
|---|---|
| Add(ColumnHeader) |
Ajoute un élément existant ColumnHeader à la collection. |
| Add(String, Int32, HorizontalAlignment) |
Ajoute un en-tête de colonne à la collection avec des paramètres de texte, de largeur et d’alignement spécifiés. |
| Add(String, Int32) |
Crée et ajoute une colonne avec le texte et la largeur spécifiés à la collection. |
| Add(String, String, Int32, HorizontalAlignment, Int32) |
Crée et ajoute une colonne avec la clé spécifiée, le texte aligné, la largeur et l’index d’image à la collection. |
| Add(String, String, Int32, HorizontalAlignment, String) |
Crée et ajoute une colonne avec la clé spécifiée, le texte aligné, la largeur et la clé d’image à la collection. |
| Add(String, String, Int32) |
Crée et ajoute une colonne avec le texte, la clé et la largeur spécifiés à la collection. |
| Add(String, String) |
Crée et ajoute une colonne avec le texte et la clé spécifiés à la collection. |
| Add(String) |
Crée et ajoute une colonne avec le texte spécifié à la collection. |
| AddRange(ColumnHeader[]) |
Ajoute un tableau d’en-têtes de colonne à la collection. |
| Clear() |
Supprime tous les en-têtes de colonne de la collection. |
| Contains(ColumnHeader) |
Détermine si l’en-tête de colonne spécifié se trouve dans la collection. |
| ContainsKey(String) |
Détermine si une colonne avec la clé spécifiée est contenue dans la collection. |
| Equals(Object) |
Détermine si l’objet spécifié est égal à l’objet actuel. (Hérité de Object) |
| GetEnumerator() |
Retourne un énumérateur à utiliser pour itérer dans la collection d’en-têtes de colonne. |
| GetHashCode() |
Sert de fonction de hachage par défaut. (Hérité de Object) |
| GetType() |
Obtient la Type de l’instance actuelle. (Hérité de Object) |
| IndexOf(ColumnHeader) |
Retourne l’index, dans la collection, de l’en-tête de colonne spécifié. |
| IndexOfKey(String) |
Détermine l’index d’une colonne avec la clé spécifiée. |
| Insert(Int32, ColumnHeader) |
Insère un en-tête de colonne existant dans la collection à l’index spécifié. |
| Insert(Int32, String, Int32, HorizontalAlignment) |
Crée un en-tête de colonne et l’insère dans la collection à l’index spécifié. |
| Insert(Int32, String, Int32) |
Crée un en-tête de colonne avec le texte et la largeur initiale spécifiés, puis insère l’en-tête dans la collection à l’index spécifié. |
| Insert(Int32, String, String, Int32, HorizontalAlignment, Int32) |
Crée un en-tête de colonne avec le texte aligné, la clé, la largeur et l’index d’image spécifiés, puis insère l’en-tête dans la collection à l’index spécifié. |
| Insert(Int32, String, String, Int32, HorizontalAlignment, String) |
Crée un en-tête de colonne avec le texte aligné, la clé, la largeur et la clé d’image spécifiés, puis insère l’en-tête dans la collection à l’index spécifié. |
| Insert(Int32, String, String, Int32) |
Crée un en-tête de colonne avec le texte, la clé et la largeur spécifiés, puis insère l’en-tête dans la collection à l’index spécifié. |
| Insert(Int32, String, String) |
Crée un en-tête de colonne avec le texte et la clé spécifiés, puis insère l’en-tête dans la collection à l’index spécifié. |
| Insert(Int32, String) |
Crée un en-tête de colonne avec le texte spécifié et insère l’en-tête dans la collection à l’index spécifié. |
| MemberwiseClone() |
Crée une copie superficielle du Objectactuel. (Hérité de Object) |
| Remove(ColumnHeader) |
Supprime l’en-tête de colonne spécifié de la collection. |
| RemoveAt(Int32) |
Supprime l’en-tête de colonne à l’index spécifié dans la collection. |
| RemoveByKey(String) |
Supprime la colonne avec la clé spécifiée de la collection. |
| ToString() |
Retourne une chaîne qui représente l’objet actuel. (Hérité de Object) |
Implémentations d’interfaces explicites
| Nom | Description |
|---|---|
| ICollection.CopyTo(Array, Int32) |
Copie les ColumnHeader objets dans le ListView.ColumnHeaderCollection tableau, en commençant par un index de tableau particulier. |
| ICollection.IsSynchronized |
Obtient une valeur indiquant si l’accès à l’objet ListView.ColumnHeaderCollection est synchronisé (thread safe). |
| ICollection.SyncRoot |
Obtient un objet qui peut être utilisé pour synchroniser l’accès à la collection de contrôles. |
| IList.Add(Object) |
Ajoute un ColumnHeader à l’objet ListView. |
| IList.Contains(Object) |
Détermine si l’en-tête de colonne spécifié se trouve dans la collection. |
| IList.IndexOf(Object) |
Cette API prend en charge l'infrastructure du produit et n'est pas destinée à être utilisée directement à partir de votre code. Retourne l’index, dans la collection, de l’en-tête de colonne spécifié. |
| IList.Insert(Int32, Object) |
Insère un en-tête de colonne existant dans la collection à l’index spécifié. |
| IList.IsFixedSize |
Obtient une valeur indiquant si la ListView.ColumnHeaderCollection taille est fixe. |
| IList.Item[Int32] |
Obtient ou définit l’en-tête de colonne à l’index spécifié dans la collection. |
| IList.Remove(Object) |
Supprime l’en-tête de colonne spécifié de la collection. |
Méthodes d’extension
| Nom | Description |
|---|---|
| AsParallel(IEnumerable) |
Active la parallélisation d’une requête. |
| AsQueryable(IEnumerable) |
Convertit un IEnumerable en IQueryable. |
| Cast<TResult>(IEnumerable) |
Convertit les éléments d’un IEnumerable en type spécifié. |
| OfType<TResult>(IEnumerable) |
Filtre les éléments d’une IEnumerable en fonction d’un type spécifié. |