ListView.ColumnHeaderCollection.Add ListView.ColumnHeaderCollection.Add ListView.ColumnHeaderCollection.Add ListView.ColumnHeaderCollection.Add Method

Definición

Agrega un encabezado de columna a la colección.Adds a column header to the collection.

Sobrecargas

Add(String) Add(String) Add(String) Add(String)

Crea una columna con el texto especificado y la agrega a la colección.Creates and adds a column with the specified text to the collection.

Add(ColumnHeader) Add(ColumnHeader) Add(ColumnHeader) Add(ColumnHeader)

Agrega un objeto ColumnHeader existente a la colección.Adds an existing ColumnHeader to the collection.

Add(String, Int32) Add(String, Int32) Add(String, Int32) Add(String, Int32)

Crea una columna con el texto y el ancho especificados y la agrega a la colección.Creates and adds a column with the specified text and width to the collection.

Add(String, String) Add(String, String) Add(String, String) Add(String, String)

Crea una columna con el texto y la clave especificados y la agrega a la colección.Creates and adds a column with the specified text and key to the collection.

Add(String, String, Int32) Add(String, String, Int32) Add(String, String, Int32) Add(String, String, Int32)

Crea una columna con el texto, la clave y el ancho especificados y la agrega a la colección.Creates and adds a column with the specified text, key, and width to the collection.

Add(String, Int32, HorizontalAlignment) Add(String, Int32, HorizontalAlignment) Add(String, Int32, HorizontalAlignment) Add(String, Int32, HorizontalAlignment)

Agrega un encabezado de columna a la colección con los valores de alineación, ancho y texto específicos.Adds a column header to the collection with specified text, width, and alignment settings.

Add(String, String, Int32, HorizontalAlignment, Int32) Add(String, String, Int32, HorizontalAlignment, Int32) Add(String, String, Int32, HorizontalAlignment, Int32) Add(String, String, Int32, HorizontalAlignment, Int32)

Crea una columna con la clave, el texto alineado, el ancho y la imagen especificados, y la agrega a la colección.Creates and adds a column with the specified key, aligned text, width, and image index to the collection.

Add(String, String, Int32, HorizontalAlignment, String) Add(String, String, Int32, HorizontalAlignment, String) Add(String, String, Int32, HorizontalAlignment, String) Add(String, String, Int32, HorizontalAlignment, String)

Crea una columna con la clave, el texto alineado, el ancho y la clave de imagen especificados, y la agrega a la colección.Creates and adds a column with the specified key, aligned text, width, and image key to the collection.

Add(String) Add(String) Add(String) Add(String)

Crea una columna con el texto especificado y la agrega a la colección.Creates and adds a column with the specified text to the collection.

public:
 virtual System::Windows::Forms::ColumnHeader ^ Add(System::String ^ text);
public virtual System.Windows.Forms.ColumnHeader Add (string text);
abstract member Add : string -> System.Windows.Forms.ColumnHeader
override this.Add : string -> System.Windows.Forms.ColumnHeader
Public Overridable Function Add (text As String) As ColumnHeader

Parámetros

text
String String String String

Texto que se va a mostrar en el encabezado de columna.The text to display in the column header.

Devoluciones

Objeto ColumnHeader con el texto especificado que se ha agregado a la colección ListView.ColumnHeaderCollection.The ColumnHeader with the specified text that was added to the ListView.ColumnHeaderCollection.

Comentarios

El encabezado de columna se agregará al final de la colección.The column header will be added to the end of the collection.

Add(ColumnHeader) Add(ColumnHeader) Add(ColumnHeader) Add(ColumnHeader)

Agrega un objeto ColumnHeader existente a la colección.Adds an existing ColumnHeader to the collection.

public:
 virtual int Add(System::Windows::Forms::ColumnHeader ^ value);
public virtual int Add (System.Windows.Forms.ColumnHeader value);
abstract member Add : System.Windows.Forms.ColumnHeader -> int
override this.Add : System.Windows.Forms.ColumnHeader -> int
Public Overridable Function Add (value As ColumnHeader) As Integer

Parámetros

value
ColumnHeader ColumnHeader ColumnHeader ColumnHeader

ColumnHeader que se agrega a la colección.The ColumnHeader to add to the collection.

Devoluciones

Índice basado en cero en la colección donde se almacena el elemento.The zero-based index into the collection where the item was added.

Ejemplos

En el ejemplo de código siguiente se crea un formulario que contiene un ListView control que ordena manualmente los elementos cuando una columna en el ListView se hace clic en el control.The following code example creates a form that contains a ListView control that manually sorts items when a column in the ListView control is clicked. El ejemplo define una clase denominada ListViewItemComparer que implementa el System.Collections.IComparer interfaz que realiza el ListViewItem comparación.The example defines a class called ListViewItemComparer that implements the System.Collections.IComparer interface that performs the ListViewItem comparison. En el ejemplo se crea una instancia de ListViewItemComparer y lo usa para establecer el ListViewItemSorter propiedad de la ListView control.The example creates an instance of ListViewItemComparer and uses it to set the ListViewItemSorter property of the ListView control. El Sort llame al método el ColumnClick controlador de eventos utiliza los métodos definidos en ListViewItemComparer para realizar la ordenación de elementos, en función de la columna que se hizo clic.The Sort method call in the ColumnClick event handler uses the methods defined in ListViewItemComparer to perform the sort of items, based on the column that is clicked.

#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
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 'New


        ' 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

Comentarios

Puede usar esta versión de la Add método para agregar una existente ColumnHeader a la colección.You can use this version of the Add method to add an existing ColumnHeader to the collection. Si desea crear un nuevo ColumnHeader en el momento de agregarlo a la colección, puede utilizar la otra versión de la Add método.If you want to create a new ColumnHeader at the time you add it to the collection, you can use the other version of the Add method.

Cuando se agregan los encabezados de columna, se agrega al final de la colección.When column headers are added, they are added to the end of the collection. Para insertar un encabezado de columna en la colección en una posición específica, utilice el Insert método.To insert a column header into the collection at a specific position, use the Insert method. Para agregar un conjunto de encabezados de columna a la colección en una sola operación, use el AddRange método.To add a set of column headers to the collection in a single operation, use the AddRange method.

Consulte también:

Add(String, Int32) Add(String, Int32) Add(String, Int32) Add(String, Int32)

Crea una columna con el texto y el ancho especificados y la agrega a la colección.Creates and adds a column with the specified text and width to the collection.

public:
 virtual System::Windows::Forms::ColumnHeader ^ Add(System::String ^ text, int width);
public virtual System.Windows.Forms.ColumnHeader Add (string text, int width);
abstract member Add : string * int -> System.Windows.Forms.ColumnHeader
override this.Add : string * int -> System.Windows.Forms.ColumnHeader
Public Overridable Function Add (text As String, width As Integer) As ColumnHeader

Parámetros

text
String String String String

Texto del objeto ColumnHeader que se va a agregar a la colección.The text of the ColumnHeader to add to the collection.

width
Int32 Int32 Int32 Int32

Ancho del objeto ColumnHeader que se va a agregar a la colección.The width of the ColumnHeader to add to the collection.

Devoluciones

Objeto ColumnHeader con el texto y el ancho especificados que se ha agregado a la colección ListView.ColumnHeaderCollection.The ColumnHeader with the specified text and width that was added to the ListView.ColumnHeaderCollection.

Comentarios

La columna se agrega al final de la colección.The column is added at the end of the collection.

Add(String, String) Add(String, String) Add(String, String) Add(String, String)

Crea una columna con el texto y la clave especificados y la agrega a la colección.Creates and adds a column with the specified text and key to the collection.

public:
 virtual System::Windows::Forms::ColumnHeader ^ Add(System::String ^ key, System::String ^ text);
public virtual System.Windows.Forms.ColumnHeader Add (string key, string text);
abstract member Add : string * string -> System.Windows.Forms.ColumnHeader
override this.Add : string * string -> System.Windows.Forms.ColumnHeader
Public Overridable Function Add (key As String, text As String) As ColumnHeader

Parámetros

key
String String String String

Clave del objeto ColumnHeader que se va a agregar a la colección.The key of the ColumnHeader to add to the collection.

text
String String String String

Texto del objeto ColumnHeader que se va a agregar a la colección.The text of the ColumnHeader to add to the collection.

Devoluciones

Objeto ColumnHeader con el texto y la clave especificados que se ha agregado a la colección ListView.ColumnHeaderCollection.The ColumnHeader with the specified key and text that was added to the ListView.ColumnHeaderCollection.

Comentarios

El Name propiedad se corresponde con la clave para una columna en la ListView.ColumnHeaderCollection.The Name property corresponds to the key for a column in the ListView.ColumnHeaderCollection.

La columna se agrega al final de la colección.The column is added at the end of the collection.

Add(String, String, Int32) Add(String, String, Int32) Add(String, String, Int32) Add(String, String, Int32)

Crea una columna con el texto, la clave y el ancho especificados y la agrega a la colección.Creates and adds a column with the specified text, key, and width to the collection.

public:
 virtual System::Windows::Forms::ColumnHeader ^ Add(System::String ^ key, System::String ^ text, int width);
public virtual System.Windows.Forms.ColumnHeader Add (string key, string text, int width);
abstract member Add : string * string * int -> System.Windows.Forms.ColumnHeader
override this.Add : string * string * int -> System.Windows.Forms.ColumnHeader
Public Overridable Function Add (key As String, text As String, width As Integer) As ColumnHeader

Parámetros

key
String String String String

Clave del encabezado de columna.The key of the column header.

text
String String String String

Texto que se va a mostrar en el encabezado de columna.The text to display in the column header.

width
Int32 Int32 Int32 Int32

Ancho inicial del objeto ColumnHeader.The initial width of the ColumnHeader.

Devoluciones

Objeto ColumnHeader con el texto, la clave y el ancho especificados que se ha agregado a la colección.The ColumnHeader with the given text, key, and width that was added to the collection.

Comentarios

El Name propiedad se corresponde con la clave para una columna en la ListView.ColumnHeaderCollection.The Name property corresponds to the key for a column in the ListView.ColumnHeaderCollection.

La columna se agrega al final de la colección.The column is added at the end of the collection.

Add(String, Int32, HorizontalAlignment) Add(String, Int32, HorizontalAlignment) Add(String, Int32, HorizontalAlignment) Add(String, Int32, HorizontalAlignment)

Agrega un encabezado de columna a la colección con los valores de alineación, ancho y texto específicos.Adds a column header to the collection with specified text, width, and alignment settings.

public:
 virtual System::Windows::Forms::ColumnHeader ^ Add(System::String ^ text, int width, System::Windows::Forms::HorizontalAlignment textAlign);
public virtual System.Windows.Forms.ColumnHeader Add (string text, int width, System.Windows.Forms.HorizontalAlignment textAlign);
abstract member Add : string * int * System.Windows.Forms.HorizontalAlignment -> System.Windows.Forms.ColumnHeader
override this.Add : string * int * System.Windows.Forms.HorizontalAlignment -> System.Windows.Forms.ColumnHeader
Public Overridable Function Add (text As String, width As Integer, textAlign As HorizontalAlignment) As ColumnHeader

Parámetros

text
String String String String

Texto que se va a mostrar en el encabezado de columna.The text to display in the column header.

width
Int32 Int32 Int32 Int32

Ancho inicial del encabezado de columna.The initial width of the column header.

Devoluciones

ColumnHeader creado y agregado a la colección.The ColumnHeader that was created and added to the collection.

Ejemplos

En el ejemplo de código siguiente se crea un ListView control con tres ListViewItem objetos especificados y tres ListViewItem.ListViewSubItem objetos especificados para cada elemento.The following code example creates a ListView control with three ListViewItem objects specified and three ListViewItem.ListViewSubItem objects specified for each item. El ejemplo también crea ColumnHeader objetos que se mostrarán los subelementos en la vista de detalles.The example also creates ColumnHeader objects to display the subitems in details view. Dos ImageList objetos también se crean en el ejemplo de código para proporcionar imágenes para la ListViewItem objetos.Two ImageList objects are also created in the code example to provide images for the ListViewItem objects. Estos ImageList se agregan objetos a la LargeImageList y SmallImageList propiedades.These ImageList objects are added to the LargeImageList and SmallImageList properties. El ejemplo utiliza las propiedades siguientes para crear el ListView control:The example uses the following properties in creating the ListView control:

Este ejemplo requiere que se ha agregado el código para un Form y llame al método creado en el ejemplo desde el constructor u otro método en el formulario.This example requires that you have added the code to a Form and call the method created in the example from the constructor or another method on the form. El ejemplo también requiere que las imágenes denominado MySmallImage1, MySmallImage2, MyLargeImage1, y MyLargeImage2 se encuentran en el directorio raíz de la unidad C.The example also requires that images named MySmallImage1, MySmallImage2, MyLargeImage1, and MyLargeImage2 are located in the root directory of drive C.

private:
   void CreateMyListView()
   {
      // Create a new ListView control.
      ListView^ listView1 = gcnew ListView;
      listView1->Bounds = Rectangle(Point(10,10),System::Drawing::Size( 300, 200 ));

      // Set the view to show details.
      listView1->View = View::Details;

      // Allow the user to edit item text.
      listView1->LabelEdit = true;

      // Allow the user to rearrange columns.
      listView1->AllowColumnReorder = true;

      // Display check boxes.
      listView1->CheckBoxes = true;

      // Select the item and subitems when selection is made.
      listView1->FullRowSelect = true;

      // Display grid lines.
      listView1->GridLines = true;

      // Sort the items in the list in ascending order.
      listView1->Sorting = SortOrder::Ascending;

      // Create three items and three sets of subitems for each item.
      ListViewItem^ item1 = gcnew ListViewItem( "item1",0 );

      // Place a check mark next to the item.
      item1->Checked = true;
      item1->SubItems->Add( "1" );
      item1->SubItems->Add( "2" );
      item1->SubItems->Add( "3" );
      ListViewItem^ item2 = gcnew ListViewItem( "item2",1 );
      item2->SubItems->Add( "4" );
      item2->SubItems->Add( "5" );
      item2->SubItems->Add( "6" );
      ListViewItem^ item3 = gcnew ListViewItem( "item3",0 );

      // Place a check mark next to the item.
      item3->Checked = true;
      item3->SubItems->Add( "7" );
      item3->SubItems->Add( "8" );
      item3->SubItems->Add( "9" );

      // Create columns for the items and subitems.
      // Width of -2 indicates auto-size.
      listView1->Columns->Add( "Item Column", -2, HorizontalAlignment::Left );
      listView1->Columns->Add( "Column 2", -2, HorizontalAlignment::Left );
      listView1->Columns->Add( "Column 3", -2, HorizontalAlignment::Left );
      listView1->Columns->Add( "Column 4", -2, HorizontalAlignment::Center );

      //Add the items to the ListView.
      array<ListViewItem^>^temp1 = {item1,item2,item3};
      listView1->Items->AddRange( temp1 );

      // Create two ImageList objects.
      ImageList^ imageListSmall = gcnew ImageList;
      ImageList^ imageListLarge = gcnew ImageList;

      // Initialize the ImageList objects with bitmaps.
      imageListSmall->Images->Add( Bitmap::FromFile( "C:\\MySmallImage1.bmp" ) );
      imageListSmall->Images->Add( Bitmap::FromFile( "C:\\MySmallImage2.bmp" ) );
      imageListLarge->Images->Add( Bitmap::FromFile( "C:\\MyLargeImage1.bmp" ) );
      imageListLarge->Images->Add( Bitmap::FromFile( "C:\\MyLargeImage2.bmp" ) );

      //Assign the ImageList objects to the ListView.
      listView1->LargeImageList = imageListLarge;
      listView1->SmallImageList = imageListSmall;
      
      // Add the ListView to the control collection.
      this->Controls->Add( listView1 );
   }
private void CreateMyListView()
{
	// Create a new ListView control.
	ListView listView1 = new ListView();
	listView1.Bounds = new Rectangle(new Point(10,10), new Size(300,200));

	// Set the view to show details.
	listView1.View = View.Details;
	// Allow the user to edit item text.
	listView1.LabelEdit = true;
	// Allow the user to rearrange columns.
	listView1.AllowColumnReorder = true;
	// Display check boxes.
	listView1.CheckBoxes = true;
	// Select the item and subitems when selection is made.
	listView1.FullRowSelect = true;
	// Display grid lines.
	listView1.GridLines = true;
	// Sort the items in the list in ascending order.
	listView1.Sorting = SortOrder.Ascending;
          			
	// Create three items and three sets of subitems for each item.
	ListViewItem item1 = new ListViewItem("item1",0);
	// Place a check mark next to the item.
	item1.Checked = true;
	item1.SubItems.Add("1");
	item1.SubItems.Add("2");
	item1.SubItems.Add("3");
	ListViewItem item2 = new ListViewItem("item2",1);
	item2.SubItems.Add("4");
	item2.SubItems.Add("5");
	item2.SubItems.Add("6");
	ListViewItem item3 = new ListViewItem("item3",0);
	// Place a check mark next to the item.
	item3.Checked = true;
	item3.SubItems.Add("7");
	item3.SubItems.Add("8");
	item3.SubItems.Add("9");

	// Create columns for the items and subitems.
	// Width of -2 indicates auto-size.
	listView1.Columns.Add("Item Column", -2, HorizontalAlignment.Left);
	listView1.Columns.Add("Column 2", -2, HorizontalAlignment.Left);
	listView1.Columns.Add("Column 3", -2, HorizontalAlignment.Left);
	listView1.Columns.Add("Column 4", -2, HorizontalAlignment.Center);

	//Add the items to the ListView.
	listView1.Items.AddRange(new ListViewItem[]{item1,item2,item3});

	// Create two ImageList objects.
	ImageList imageListSmall = new ImageList();
	ImageList imageListLarge = new ImageList();

	// Initialize the ImageList objects with bitmaps.
	imageListSmall.Images.Add(Bitmap.FromFile("C:\\MySmallImage1.bmp"));
	imageListSmall.Images.Add(Bitmap.FromFile("C:\\MySmallImage2.bmp"));
	imageListLarge.Images.Add(Bitmap.FromFile("C:\\MyLargeImage1.bmp"));
	imageListLarge.Images.Add(Bitmap.FromFile("C:\\MyLargeImage2.bmp"));

	//Assign the ImageList objects to the ListView.
	listView1.LargeImageList = imageListLarge;
	listView1.SmallImageList = imageListSmall;

	// Add the ListView to the control collection.
	this.Controls.Add(listView1);
}
Private Sub CreateMyListView()
    ' Create a new ListView control.
    Dim listView1 As New ListView()
    listView1.Bounds = New Rectangle(New Point(10, 10), New Size(300, 200))

    ' Set the view to show details.
    listView1.View = View.Details
    ' Allow the user to edit item text.
    listView1.LabelEdit = True
    ' Allow the user to rearrange columns.
    listView1.AllowColumnReorder = True
    ' Display check boxes.
    listView1.CheckBoxes = True
    ' Select the item and subitems when selection is made.
    listView1.FullRowSelect = True
    ' Display grid lines.
    listView1.GridLines = True
    ' Sort the items in the list in ascending order.
    listView1.Sorting = SortOrder.Ascending

    ' Create three items and three sets of subitems for each item.
    Dim item1 As New ListViewItem("item1", 0)
    ' Place a check mark next to the item.
    item1.Checked = True
    item1.SubItems.Add("1")
    item1.SubItems.Add("2")
    item1.SubItems.Add("3")
    Dim item2 As New ListViewItem("item2", 1)
    item2.SubItems.Add("4")
    item2.SubItems.Add("5")
    item2.SubItems.Add("6")
    Dim item3 As New ListViewItem("item3", 0)
    ' Place a check mark next to the item.
    item3.Checked = True
    item3.SubItems.Add("7")
    item3.SubItems.Add("8")
    item3.SubItems.Add("9")

    ' Create columns for the items and subitems.
    ' Width of -2 indicates auto-size.
    listView1.Columns.Add("Item Column", -2, HorizontalAlignment.Left)
    listView1.Columns.Add("Column 2", -2, HorizontalAlignment.Left)
    listView1.Columns.Add("Column 3", -2, HorizontalAlignment.Left)
    listView1.Columns.Add("Column 4", -2, HorizontalAlignment.Center)

    'Add the items to the ListView.
    listView1.Items.AddRange(New ListViewItem() {item1, item2, item3})

    ' Create two ImageList objects.
    Dim imageListSmall As New ImageList()
    Dim imageListLarge As New ImageList()

    ' Initialize the ImageList objects with bitmaps.
    imageListSmall.Images.Add(Bitmap.FromFile("C:\MySmallImage1.bmp"))
    imageListSmall.Images.Add(Bitmap.FromFile("C:\MySmallImage2.bmp"))
    imageListLarge.Images.Add(Bitmap.FromFile("C:\MyLargeImage1.bmp"))
    imageListLarge.Images.Add(Bitmap.FromFile("C:\MyLargeImage2.bmp"))

    'Assign the ImageList objects to the ListView.
    listView1.LargeImageList = imageListLarge
    listView1.SmallImageList = imageListSmall

    ' Add the ListView to the control collection.
    Me.Controls.Add(listView1)
End Sub 'CreateMyListView

Comentarios

Puede usar esta versión de la Add método para crear un nuevo ColumnHeader para agregar a la colección.You can use this version of the Add method to create a new ColumnHeader to add to the collection. El texto de la nueva ColumnHeader agregado para el control se basa en el text parámetro.The text of the new ColumnHeader added to the control is based on the text parameter. Esta versión de la Add método también permite especificar el ancho de la columna y la alineación del texto del encabezado de columna.This version of the Add method also allows you to specify the width of the column and the alignment of text in the column header. Si dispone de una ColumnHeader que desea agregar a la colección, use la versión de la Add método que acepta un ColumnHeader como su parámetro.If you have an existing ColumnHeader that you want to add to the collection, use the version of the Add method that accepts a ColumnHeader as its parameter.

Cuando se agregan los encabezados de columna, se agrega al final de la colección.When column headers are added, they are added to the end of the collection. Para insertar un encabezado de columna en la colección en una posición específica, utilice el Insert método.To insert a column header into the collection at a specific position, use the Insert method. Para agregar un conjunto de encabezados de columna a la colección en una sola operación, use el AddRange método.To add a set of column headers to the collection in a single operation, use the AddRange method.

Consulte también:

Add(String, String, Int32, HorizontalAlignment, Int32) Add(String, String, Int32, HorizontalAlignment, Int32) Add(String, String, Int32, HorizontalAlignment, Int32) Add(String, String, Int32, HorizontalAlignment, Int32)

Crea una columna con la clave, el texto alineado, el ancho y la imagen especificados, y la agrega a la colección.Creates and adds a column with the specified key, aligned text, width, and image index to the collection.

public:
 virtual System::Windows::Forms::ColumnHeader ^ Add(System::String ^ key, System::String ^ text, int width, System::Windows::Forms::HorizontalAlignment textAlign, int imageIndex);
public virtual System.Windows.Forms.ColumnHeader Add (string key, string text, int width, System.Windows.Forms.HorizontalAlignment textAlign, int imageIndex);
abstract member Add : string * string * int * System.Windows.Forms.HorizontalAlignment * int -> System.Windows.Forms.ColumnHeader
override this.Add : string * string * int * System.Windows.Forms.HorizontalAlignment * int -> System.Windows.Forms.ColumnHeader
Public Overridable Function Add (key As String, text As String, width As Integer, textAlign As HorizontalAlignment, imageIndex As Integer) As ColumnHeader

Parámetros

key
String String String String

Clave del encabezado de columna.The key of the column header.

text
String String String String

Texto que se va a mostrar en el encabezado de columna.The text to display in the column header.

width
Int32 Int32 Int32 Int32

Ancho inicial del encabezado de columna.The initial width of the column header.

imageIndex
Int32 Int32 Int32 Int32

Valor de índice de la imagen que se va a mostrar en la columna.The index value of the image to display in the column.

Devoluciones

Objeto ColumnHeader con la clave, el texto alineado, el ancho y el índice de imagen especificados que se ha agregado a la colección.The ColumnHeader with the specified key, aligned text, width, and image index that has been added to the collection.

Comentarios

El Name propiedad se corresponde con la clave para una columna en la ListView.ColumnHeaderCollection.The Name property corresponds to the key for a column in the ListView.ColumnHeaderCollection.

La columna se agrega al final de la colección.The column is added at the end of the collection.

Add(String, String, Int32, HorizontalAlignment, String) Add(String, String, Int32, HorizontalAlignment, String) Add(String, String, Int32, HorizontalAlignment, String) Add(String, String, Int32, HorizontalAlignment, String)

Crea una columna con la clave, el texto alineado, el ancho y la clave de imagen especificados, y la agrega a la colección.Creates and adds a column with the specified key, aligned text, width, and image key to the collection.

public:
 virtual System::Windows::Forms::ColumnHeader ^ Add(System::String ^ key, System::String ^ text, int width, System::Windows::Forms::HorizontalAlignment textAlign, System::String ^ imageKey);
public virtual System.Windows.Forms.ColumnHeader Add (string key, string text, int width, System.Windows.Forms.HorizontalAlignment textAlign, string imageKey);
abstract member Add : string * string * int * System.Windows.Forms.HorizontalAlignment * string -> System.Windows.Forms.ColumnHeader
override this.Add : string * string * int * System.Windows.Forms.HorizontalAlignment * string -> System.Windows.Forms.ColumnHeader
Public Overridable Function Add (key As String, text As String, width As Integer, textAlign As HorizontalAlignment, imageKey As String) As ColumnHeader

Parámetros

key
String String String String

Clave del encabezado de columna.The key of the column header.

text
String String String String

Texto que se va a mostrar en el encabezado de columna.The text to display in the column header.

width
Int32 Int32 Int32 Int32

Ancho inicial del encabezado de columna.The initial width of the column header.

imageKey
String String String String

Valor clave de la imagen que se va a mostrar en la columna.The key value of the image to display in the column header.

Devoluciones

Objeto ColumnHeader con la clave, el texto alineado, el ancho y la clave de imagen especificados que se ha agregado a la colección.The ColumnHeader with the specified key, aligned text, width, and image key that has been added to the collection.

Comentarios

El Name propiedad se corresponde con la clave para una columna en la ListView.ColumnHeaderCollection.The Name property corresponds to the key for a column in the ListView.ColumnHeaderCollection.

La columna se agrega al final de la colección.The column is added at the end of the collection.

Se aplica a