HOW TO:在 Windows Form ListView 控制項中啟用並排顯示
ListView 控制項的並排顯示功能,可以讓您在圖形和文字資訊之間取得視覺上的平衡。 在並排顯示中顯示的項目文字資訊,和詳細檢視定義的資料行資訊相同。 並排顯示需要和 ListView 控制項的群組或插入標記功能一起運作。
並排顯示會使用一個 32 x 32 像素的圖示和數行文字,如下圖所示。
並排顯示的圖示和文字
若要啟用並排顯示,請將 View 屬性設定為 Tile。 如果要調整並排的大小,可以設定 TileSize 屬性,如果要改變並排所顯示的文字行數,則可以調整 Columns 集合。
注意事項 |
---|
當應用程式呼叫 Application.EnableVisualStyles 方法時,並排顯示功能只能在 Windows XP Home Edition、Windows XP Professional、Windows Server 2003 上使用。 在舊版的作業系統中,與並排顯示相關的任何程式碼都沒有作用,而且 ListView 控制項會以大型圖示顯示。 如需詳細資訊,請參閱 ListView.View。 |
以程式設計方式設定並排顯示
範例
在下列這個完整的程式碼範例中,會示範修改為可顯示三行文字的並排顯示, 而且並排大小也已調整過,以避免換行。
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Public Class ListViewTilingExample
Inherits Form
Private myImageList As ImageList
Public Sub New()
' Initialize myListView.
Dim myListView As New ListView()
myListView.Dock = DockStyle.Fill
myListView.View = View.Tile
' Initialize the tile size.
myListView.TileSize = new Size(400, 45)
' Initialize the item icons.
myImageList = New ImageList()
Dim myIcon as Icon = new Icon("book.ico")
Try
myImageList.Images.Add(myIcon)
Finally
myIcon.Dispose()
End Try
myIcon.Dispose()
myImageList.ImageSize = New Size(32, 32)
myListView.LargeImageList = myImageList
' Add column headers so the subitems will appear.
myListView.Columns.AddRange(New ColumnHeader() _
{New ColumnHeader(), New ColumnHeader(), New ColumnHeader()})
' Create items and add them to myListView.
Dim item0 As New ListViewItem( New String() _
{"Programming Windows", _
"Petzold, Charles", _
"1998"}, 0 )
Dim item1 As New ListViewItem( New String() _
{"Code: The Hidden Language of Computer Hardware and Software", _
"Petzold, Charles", _
"2000"}, 0 )
Dim item2 As New ListViewItem( New String() _
{"Programming Windows with C#", _
"Petzold, Charles", _
"2001"}, 0 )
Dim item3 As New ListViewItem( New String() _
{"Coding Techniques for Microsoft Visual Basic .NET", _
"Connell, John", _
"2001"}, 0 )
Dim item4 As New ListViewItem( New String() _
{"C# for Java Developers", _
"Jones, Allen / Freeman, Adam", _
"2002"}, 0 )
Dim item5 As New ListViewItem( New String() _
{"Microsoft .NET XML Web Services Step by Step", _
"Jones, Allen / Freeman, Adam", _
"2002"}, 0 )
myListView.Items.AddRange( _
New ListViewItem() {item0, item1, item2, item3, item4, item5})
' Initialize the form.
Me.Controls.Add(myListView)
Me.Size = new System.Drawing.Size(430, 330)
Me.Text = "ListView Tiling Example"
End Sub 'New
' Clean up any resources being used.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If (disposing) Then
myImageList.Dispose()
End If
MyBase.Dispose(disposing)
End Sub
<STAThread()> _
Shared Sub Main()
Application.EnableVisualStyles()
Application.Run(New ListViewTilingExample())
End Sub 'Main
End Class 'ListViewTilingExample
using System;
using System.Drawing;
using System.Windows.Forms;
public class ListViewTilingExample : Form
{
private ImageList myImageList;
public ListViewTilingExample()
{
// Initialize myListView.
ListView myListView = new ListView();
myListView.Dock = DockStyle.Fill;
myListView.View = View.Tile;
// Initialize the tile size.
myListView.TileSize = new Size(400, 45);
// Initialize the item icons.
myImageList = new ImageList();
using (Icon myIcon = new Icon("book.ico"))
{
myImageList.Images.Add(myIcon);
}
myImageList.ImageSize = new Size(32, 32);
myListView.LargeImageList = myImageList;
// Add column headers so the subitems will appear.
myListView.Columns.AddRange(new ColumnHeader[]
{new ColumnHeader(), new ColumnHeader(), new ColumnHeader()});
// Create items and add them to myListView.
ListViewItem item0 = new ListViewItem( new string[]
{"Programming Windows",
"Petzold, Charles",
"1998"}, 0 );
ListViewItem item1 = new ListViewItem( new string[]
{"Code: The Hidden Language of Computer Hardware and Software",
"Petzold, Charles",
"2000"}, 0 );
ListViewItem item2 = new ListViewItem( new string[]
{"Programming Windows with C#",
"Petzold, Charles",
"2001"}, 0 );
ListViewItem item3 = new ListViewItem( new string[]
{"Coding Techniques for Microsoft Visual Basic .NET",
"Connell, John",
"2001"}, 0 );
ListViewItem item4 = new ListViewItem( new string[]
{"C# for Java Developers",
"Jones, Allen & Freeman, Adam",
"2002"}, 0 );
ListViewItem item5 = new ListViewItem( new string[]
{"Microsoft .NET XML Web Services Step by Step",
"Jones, Allen & Freeman, Adam",
"2002"}, 0 );
myListView.Items.AddRange(
new ListViewItem[] {item0, item1, item2, item3, item4, item5});
// Initialize the form.
this.Controls.Add(myListView);
this.Size = new System.Drawing.Size(430, 330);
this.Text = "ListView Tiling Example";
}
// Clean up any resources being used.
protected override void Dispose(bool disposing)
{
if (disposing)
{
myImageList.Dispose();
}
base.Dispose(disposing);
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new ListViewTilingExample());
}
}
#using <System.dll>
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>
using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;
public ref class ListViewTilingExample: public Form
{
private:
ImageList^ myImageList;
public:
ListViewTilingExample()
{
// Initialize myListView.
ListView^ myListView = gcnew ListView;
myListView->Dock = DockStyle::Fill;
myListView->View = View::Tile;
// Initialize the tile size.
myListView->TileSize = System::Drawing::Size( 400, 45 );
// Initialize the item icons.
myImageList = gcnew ImageList;
System::Drawing::Icon^ myIcon = gcnew System::Drawing::Icon( "book.ico" );
try
{
myImageList->Images->Add( myIcon );
}
finally
{
if ( myIcon )
delete safe_cast<IDisposable^>(myIcon);
}
myImageList->ImageSize = System::Drawing::Size( 32, 32 );
myListView->LargeImageList = myImageList;
// Add column headers so the subitems will appear.
array<ColumnHeader^>^temp0 = {gcnew ColumnHeader,gcnew ColumnHeader,gcnew ColumnHeader};
myListView->Columns->AddRange( temp0 );
// Create items and add them to myListView.
array<String^>^temp1 = {"Programming Windows","Petzold, Charles","1998"};
ListViewItem^ item0 = gcnew ListViewItem( temp1,0 );
array<String^>^temp2 = {"Code: The Hidden Language of Computer Hardware and Software","Petzold, Charles","2000"};
ListViewItem^ item1 = gcnew ListViewItem( temp2,0 );
array<String^>^temp3 = {"Programming Windows with C#","Petzold, Charles","2001"};
ListViewItem^ item2 = gcnew ListViewItem( temp3,0 );
array<String^>^temp4 = {"Coding Techniques for Microsoft Visual Basic .NET","Connell, John","2001"};
ListViewItem^ item3 = gcnew ListViewItem( temp4,0 );
array<String^>^temp5 = {"C# for Java Developers","Jones, Allen & Freeman, Adam","2002"};
ListViewItem^ item4 = gcnew ListViewItem( temp5,0 );
array<String^>^temp6 = {"Microsoft .NET XML Web Services Step by Step","Jones, Allen & Freeman, Adam","2002"};
ListViewItem^ item5 = gcnew ListViewItem( temp6,0 );
array<ListViewItem^>^temp7 = {item0,item1,item2,item3,item4,item5};
myListView->Items->AddRange( temp7 );
// Initialize the form.
this->Controls->Add( myListView );
this->Size = System::Drawing::Size( 430, 330 );
this->Text = "ListView Tiling Example";
}
protected:
// Clean up any resources being used.
~ListViewTilingExample()
{
if ( myImageList != nullptr )
{
delete myImageList;
}
}
};
[STAThread]
int main()
{
Application::EnableVisualStyles();
Application::Run( gcnew ListViewTilingExample );
}
編譯程式碼
這個範例需要:
System 和 System.Windows.Forms 組件的參考。
在可執行檔的相同目錄中有一個名為 book.ico 的圖示檔。
如需從 Visual Basic 或 Visual C# 的命令列建置這個範例的詳細資訊,請參閱從命令列建置 (Visual Basic) 或使用 csc.exe 建置命令列。 您也可以透過將程式碼貼入新的專案,在 Visual Studio 中建置此範例。 如需詳細資訊,請參閱HOW TO:使用 Visual Studio 編譯及執行完整的 Windows Form 程式碼範例 和 HOW TO:使用 Visual Studio 編譯及執行完整的 Windows Form 程式碼範例 和 HOW TO:使用 Visual Studio 編譯及執行完整的 Windows Form 程式碼範例 和 HOW TO:使用 Visual Studio 編譯及執行完整的 Windows Form 程式碼範例 和 如何:使用 Visual Studio 編譯及執行完整的 Windows Form 程式碼範例.
請參閱
參考
概念
Windows XP 功能和 Windows Form 控制項