> The problem I am encountering is this:
private void listView1_SelectedIndexChanged(etc)
{
// show the subitem of the item that was clicked
}
I need a remedy for this situation.
OK, you're probably about to get flamed because you are either not reading
the docs or do not know how to read the docs. If you want to know how a
control works, check out the documentation. Microsoft has done a great job
and lays it all out for you, if you are willing to look for it and read it.
Here is a link to the .Net Framework documentation. You can drill down and
find documentation on every class and every control in the framework (many
times with sample code!). Here is the link:
http://msdn.microsoft.com/library/de...sctortopic.asp
Just to be sure to answer your current question (and try to keep you from
getting flamed), I'm going to attach a complete sample form to show you how
to get the subitems from your SelectedIndexChanged event. You should be
able to (1) create a C# WinForms project named ListViewTest01, (2) replace
the code of the Form1 class (be sure to close the designer window that is
showing Form1) and (3) run the application. There is a ListView in the
upper portion of the form listing some simple names and phone numbers. When
you click on one of the items in the ListView, a ListBox at the bottom of
the form is populated with the details (subitems) of the one you selected.
Good luck.
<code>
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace ListViewTest01
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ListView lvList;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.ListBox lbResults;
private System.Windows.Forms.ColumnHeader colhLastName;
private System.Windows.Forms.ColumnHeader colhFirstName;
private System.Windows.Forms.ColumnHeader colhPhoneNumber;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.lvList = new System.Windows.Forms.ListView();
this.label1 = new System.Windows.Forms.Label();
this.lbResults = new System.Windows.Forms.ListBox();
this.colhLastName = new System.Windows.Forms.ColumnHeader();
this.colhFirstName = new System.Windows.Forms.ColumnHeader();
this.colhPhoneNumber = new System.Windows.Forms.ColumnHeader();
this.SuspendLayout();
//
// lvList
//
this.lvList.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.colhLastName,
this.colhFirstName,
this.colhPhoneNumber});
this.lvList.FullRowSelect = true;
this.lvList.Location = new System.Drawing.Point(8, 8);
this.lvList.Name = "lvList";
this.lvList.Size = new System.Drawing.Size(296, 240);
this.lvList.TabIndex = 0;
this.lvList.View = System.Windows.Forms.View.Details;
this.lvList.SelectedIndexChanged += new
System.EventHandler(this.lvList_SelectedIndexChang ed);
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 280);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(280, 16);
this.label1.TabIndex = 1;
this.label1.Text = "Item Clicked:";
//
// lbResults
//
this.lbResults.Location = new System.Drawing.Point(16, 296);
this.lbResults.Name = "lbResults";
this.lbResults.Size = new System.Drawing.Size(280, 69);
this.lbResults.TabIndex = 2;
//
// colhLastName
//
this.colhLastName.Text = "LastName";
this.colhLastName.Width = 90;
//
// colhFirstName
//
this.colhFirstName.Text = "FirstName";
this.colhFirstName.Width = 90;
//
// colhPhoneNumber
//
this.colhPhoneNumber.Text = "PhoneNumber";
this.colhPhoneNumber.Width = 100;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(320, 382);
this.Controls.Add(this.lbResults);
this.Controls.Add(this.label1);
this.Controls.Add(this.lvList);
this.Name = "Form1";
this.Text = "ListView Selection Sample";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
// load some dummy data to click on.
this.lvList.Items.Add( CreateListViewItem("Doe", "John",
"111-111-1111"));
this.lvList.Items.Add( CreateListViewItem("Spade", "Sam",
"222-222-2222"));
this.lvList.Items.Add( CreateListViewItem("Client", "Clara",
"333-333-3333"));
}
/// <summary>
/// This class does nothing more than create a ListViewItem to add to the
ListView.
/// </summary>
/// <param name="lastName">The person's last name.</param>
/// <param name="firstName">The person's fist name.</param>
/// <param name="phoneNumber">The person's phone number.</param>
/// <returns>A ListViewItem that you can add to the Items collection of a
ListView.</returns>
private ListViewItem CreateListViewItem(String lastName, String firstName,
String phoneNumber)
{
String[] subitems = new String[3];
subitems[0] = lastName;
subitems[1] = firstName;
subitems[2] = phoneNumber;
ListViewItem lvi = new ListViewItem(subitems);
return lvi;
}
private void lvList_SelectedIndexChanged(object sender, System.EventArgs
e)
{
// clear all items from the list box
lbResults.Items.Clear();
// make sure something is selected
if (lvList.SelectedItems.Count == 0) return;
// otherwise populate the listbox with the information
ListViewItem lvi = lvList.SelectedItems[0];
lbResults.Items.Add("LastName :" + lvi.SubItems[0]);
lbResults.Items.Add("FirstName:" + lvi.SubItems[1]);
lbResults.Items.Add("PhoneNbr :" + lvi.SubItems[2]);
}
}
}
</code>