472,328 Members | 1,107 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

Selecting from ListView

How do I select a subitem from a listview
after clicking on the first column? Couldn't
find it in MSDN.
Thank you.
Paddy.


Nov 16 '05 #1
4 9593

You can access it via the SubItems collection.
See:

ms-help://MS.MSDNQTR.2004APR.1033/cpref/html/frlrfsystemwindowsformslistview
itemclasssubitemstopic.htm
Here is a code snippit of some dummy code. The first routine adds an item,
the second routine changes some of the sub items.

<code>
private void button1_Click(object sender, System.EventArgs e)

{

ListViewItem lvi = listView1.Items.Add(
"adfkljasdflkj" );

lvi.SubItems.Add("This");

lvi.SubItems.Add("is");

lvi.SubItems.Add("a");

lvi.SubItems.Add("test.");

}

private void button2_Click(object sender, System.EventArgs e)

{

ListViewItem lvi = listView1.Items[0];

lvi.SubItems[1].Text = "changed1";

lvi.SubItems[2].Text = "changed2";

lvi.SubItems[3].Text = "changed3";

}

</code>

"Paddy" <wa**********@all.here> wrote in message
news:69**************************@freeler.nl...
How do I select a subitem from a listview
after clicking on the first column? Couldn't
find it in MSDN.
Thank you.
Paddy.

Nov 16 '05 #2
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.
Nov 16 '05 #3
> 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>

Nov 16 '05 #4
Thank you John Mark, for your comprehensive answer
to my question.
Paddy.
"John Mark Howell" <jm*********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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
<snipped>


Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: vince | last post by:
I need to programmatically select the first item in a listview before the listview is displayed, so far I'm not having any luck. Here's what I...
1
by: Sean Chapman | last post by:
I was wondering how I would go about programmincally selecting a listView item in a listView since the Selected val is read-only. I can't seem to...
1
by: Bob Geltz | last post by:
I am able to populate a ListView with several detail lines (several columns). When finished, I would like to pre-select the first item in the list...
2
by: David Anderson | last post by:
I'm working on a Windows app that has a ListView containing a bunch of items. When the user clicks on an item, the app displays the item'd details....
3
by: Bry | last post by:
I know you can select a single item in a list view using the following code listView.Items.Selected = true; Is it possible to do this for all...
2
by: Fritz | last post by:
I know it, I know it. It's come up a lot. I've done the Googling, but the standard answer doesn't do what I expect it to. For the record, the...
3
by: larry mckay | last post by:
anyone have the code to select and listview item or row (subitems) after a doubleclick event from a listview. *** Sent via Developersdex...
4
by: Mark Rae | last post by:
Hi, I have a bog-standard ListView control in a v1.1 C# WinForms project, with three colums, Details View. There is "Select All" button under...
5
by: Bart Steur | last post by:
Hi, What's the best way to programmaticly select (click) another listview Item after removing the selected/focused one. using VB2005 Express....
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.