Let us look at a simple C# Windows Forms ListView example.
Example 1: ListView
This example will comprise the following files:
ListView.cs
Resources.Designer.cs
Step 1: Create Project
- The first step is to create a C# Project.
- Go to
FILE-->New-->Project
to create a new project.
Step 2: Write Code
Write Code as follows:
*(a). ListView.cs
Create a file named ListView.cs
Here is the full code
using System;
using System.Windows.Forms;
namespace Examples {
class MainForm : Form {
public static void Main() {
Application.EnableVisualStyles();
Application.Run(new MainForm());
}
public MainForm() {
Text = "ListView example";
ClientSize = new System.Drawing.Size(350, 260);
listView1.Parent = this;
listView1.Location = new System.Drawing.Point(10, 10);
listView1.Size = new System.Drawing.Size(330, 200);
listView1.View = View.Details;
listView1.LargeImageList = new ImageList();
listView1.LargeImageList.ImageSize = new System.Drawing.Size(48, 48);
listView1.LargeImageList.Images.AddRange(new System.Drawing.Image[] {Properties.Resources.Ai, Properties.Resources.Avi, Properties.Resources.Bmp});
listView1.SmallImageList = new ImageList();
listView1.SmallImageList.ImageSize = new System.Drawing.Size(16, 16);
listView1.SmallImageList.Images.AddRange(new System.Drawing.Image[] {Properties.Resources.Ai, Properties.Resources.Avi, Properties.Resources.Bmp});
listView1.Columns.Add("Name", 80);
listView1.Columns.Add("Type", 50);
listView1.Columns.Add("Size", 50);
listView1.Columns.Add("Comment", 140);
listView1.Items.Add("First", 1).SubItems.AddRange(new string[] {"Movie", "5389", "This is the first item"});
listView1.Items.Add("Second", 0).SubItems.AddRange(new string[] {"Picture", "1256", "This is the second item"});
listView1.Items.Add("Third", 2).SubItems.AddRange(new string[] {"Picture", "4284", "This is the third item"});
comboBox1.Location = new System.Drawing.Point(10, 220);
comboBox1.Parent = this;
comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
comboBox1.Items.AddRange(Enum.GetNames(typeof(View)));
comboBox1.SelectedIndex = (int)listView1.View;
comboBox1.SelectedIndexChanged += delegate(object sender, EventArgs e) {
listView1.View = (View)comboBox1.SelectedIndex;
};
}
private ListView listView1 = new ListView();
private ComboBox comboBox1 = new ComboBox();
}
}
Run
Simply copy the source code into your C# Project,Build and Run. Alternatively download the code using the links provided below, then open the .csproj
project, build and run.
Reference
Download the code using the below links:
Number | Link |
---|---|
1. | Download Example |
2. | Follow code author |
*(a). Resources.Designer.cs
Create a file named Resources.Designer.cs
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
-----------------------------------------------------------------------------
```csharp
namespace Examples.Properties {
using System;
/// <summary>
/// A strongly-typed resource class, for looking up localized strings, etc.
/// </summary>
// This class was auto-generated by the StronglyTypedResourceBuilder
// class via a tool like ResGen or Visual Studio.
// To add or remove a member, edit your .ResX file then rerun ResGen
// with the /str option, or rebuild your VS project.
internal class Resources {
private static global::System.Resources.ResourceManager resourceMan;
private static global::System.Globalization.CultureInfo resourceCulture;
internal Resources() {
}
/// <summary>
/// Returns the cached ResourceManager instance used by this class.
/// </summary>
internal static global::System.Resources.ResourceManager ResourceManager {
get {
if (object.ReferenceEquals(resourceMan, null)) {
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Examples.Properties.Resources", typeof(Resources).Assembly);
resourceMan = temp;
}
return resourceMan;
}
}
/// <summary>
/// Overrides the current thread's CurrentUICulture property for all
/// resource lookups using this strongly typed resource class.
/// </summary>
internal static global::System.Globalization.CultureInfo Culture {
get {
return resourceCulture;
}
set {
resourceCulture = value;
}
}
/// <summary>
/// Looks up a localized resource of type System.Drawing.Bitmap.
/// </summary>
internal static System.Drawing.Bitmap Ai {
get {
object obj = ResourceManager.GetObject("Ai", resourceCulture);
return ((System.Drawing.Bitmap)(obj));
}
}
/// <summary>
/// Looks up a localized resource of type System.Drawing.Bitmap.
/// </summary>
internal static System.Drawing.Bitmap Avi {
get {
object obj = ResourceManager.GetObject("Avi", resourceCulture);
return ((System.Drawing.Bitmap)(obj));
}
}
/// <summary>
/// Looks up a localized resource of type System.Drawing.Bitmap.
/// </summary>
internal static System.Drawing.Bitmap Bmp {
get {
object obj = ResourceManager.GetObject("Bmp", resourceCulture);
return ((System.Drawing.Bitmap)(obj));
}
}
}
}
Here is the full code
```csharp
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
namespace Examples.Properties {
using System;
/// <summary>
/// A strongly-typed resource class, for looking up localized strings, etc.
/// </summary>
// This class was auto-generated by the StronglyTypedResourceBuilder
// class via a tool like ResGen or Visual Studio.
// To add or remove a member, edit your .ResX file then rerun ResGen
// with the /str option, or rebuild your VS project.
internal class Resources {
private static global::System.Resources.ResourceManager resourceMan;
private static global::System.Globalization.CultureInfo resourceCulture;
internal Resources() {
}
/// <summary>
/// Returns the cached ResourceManager instance used by this class.
/// </summary>
internal static global::System.Resources.ResourceManager ResourceManager {
get {
if (object.ReferenceEquals(resourceMan, null)) {
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Examples.Properties.Resources", typeof(Resources).Assembly);
resourceMan = temp;
}
return resourceMan;
}
}
/// <summary>
/// Overrides the current thread's CurrentUICulture property for all
/// resource lookups using this strongly typed resource class.
/// </summary>
internal static global::System.Globalization.CultureInfo Culture {
get {
return resourceCulture;
}
set {
resourceCulture = value;
}
}
/// <summary>
/// Looks up a localized resource of type System.Drawing.Bitmap.
/// </summary>
internal static System.Drawing.Bitmap Ai {
get {
object obj = ResourceManager.GetObject("Ai", resourceCulture);
return ((System.Drawing.Bitmap)(obj));
}
}
/// <summary>
/// Looks up a localized resource of type System.Drawing.Bitmap.
/// </summary>
internal static System.Drawing.Bitmap Avi {
get {
object obj = ResourceManager.GetObject("Avi", resourceCulture);
return ((System.Drawing.Bitmap)(obj));
}
}
/// <summary>
/// Looks up a localized resource of type System.Drawing.Bitmap.
/// </summary>
internal static System.Drawing.Bitmap Bmp {
get {
object obj = ResourceManager.GetObject("Bmp", resourceCulture);
return ((System.Drawing.Bitmap)(obj));
}
}
}
}
Run
Simply copy the source code into your C# Project,Build and Run. Alternatively download the code using the links provided below, then open the .csproj
project, build and run.
Reference
Download the code using the below links:
Number | Link |
---|---|
1. | Download Example |
2. | Follow code author |