473,411 Members | 2,083 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 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 9688

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 tried in the init for the listview... ...
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 find any other method that will do this for me. I'm...
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 (before the user interacts with the list). This...
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. The user then has the opportunity to edit these...
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 items in the listView control without using a...
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 standard answer is: "Set the Selected property of a...
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 http://www.developersdex.com *** Don't just participate...
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 the ListView control with the following code: ...
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. Thanks Bart
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.