470,620 Members | 1,460 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,620 developers. It's quick & easy.

listView question


With this program I can do one selection,
but upon the second I get an error where
///////////////// is indicated. Please help.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication1
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.ColumnHeader columnHeader1;
private System.Windows.Forms.ColumnHeader columnHeader2;
private System.Windows.Forms.ListView listView1;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
this.listView1 = new System.Windows.Forms.ListView();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(16, 192);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(256, 20);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "";
//
// columnHeader1
//
this.columnHeader1.Text = "number";
this.columnHeader1.Width = 49;
//
// columnHeader2
//
this.columnHeader2.Text = "name";
this.columnHeader2.Width = 186;
//
// listView1
//
this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.columnHeader1,
this.columnHeader2});
this.listView1.GridLines = true;
this.listView1.LabelEdit = true;
this.listView1.Location = new System.Drawing.Point(24, 16);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(256, 160);
this.listView1.TabIndex = 0;
this.listView1.View = System.Windows.Forms.View.Details;
this.listView1.SelectedIndexChanged += new
System.EventHandler(this.listView1_SelectedIndexCh anged);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.listView1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWind ow;
this.Name = "Form1";
this.ShowInTaskbar = false;
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
ListView.SelectedListViewItemCollection contents =
this.listView1.SelectedItems;
textBox1.Text = contents[0].SubItems[1].Text;/////////////////////error
}
private void Form1_Load(object sender, System.EventArgs e)
{
string [ ] show0 = new string [] {"one","two","three","four"};
string [ ] show1 = new string [] {"ene","mene","mane","moo"};
for (int i = 0; i<show0.Length;i++)
{
ListViewItem listItem = new ListViewItem(show0[i]);
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication1
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.ColumnHeader columnHeader1;
private System.Windows.Forms.ColumnHeader columnHeader2;
private System.Windows.Forms.ListView listView1;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
this.listView1 = new System.Windows.Forms.ListView();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(16, 192);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(256, 20);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "";
//
// columnHeader1
//
this.columnHeader1.Text = "number";
this.columnHeader1.Width = 49;
//
// columnHeader2
//
this.columnHeader2.Text = "name";
this.columnHeader2.Width = 186;
//
// listView1
//
this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.columnHeader1,
this.columnHeader2});
this.listView1.GridLines = true;
this.listView1.LabelEdit = true;
this.listView1.Location = new System.Drawing.Point(24, 16);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(256, 160);
this.listView1.TabIndex = 0;
this.listView1.View = System.Windows.Forms.View.Details;
this.listView1.SelectedIndexChanged += new
System.EventHandler(this.listView1_SelectedIndexCh anged);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.listView1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWind ow;
this.Name = "Form1";
this.ShowInTaskbar = false;
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
ListView.SelectedListViewItemCollection contents =
this.listView1.SelectedItems;
textBox1.Text = contents[0].SubItems[1].Text;/////////////////////error
}
private void Form1_Load(object sender, System.EventArgs e)
{
string [ ] show0 = new string [] {"one","two","three","four"};
string [ ] show1 = new string [] {"ene","mene","mane","moo"};
for (int i = 0; i<show0.Length;i++)
{
ListViewItem listItem = new ListViewItem(show0[i]);
listItem.SubItems.Add(show1[i]);
listView1.Items.Add(listItem);
}
}
}
}
listItem.SubItems.Add(show1[i]);
listView1.Items.Add(listItem);
}
}
}
}


Nov 16 '05 #1
6 2291
The error is because an event is firing with nothing in the SelectedItems...
so trying to access contents[0] fails because there is nothing in contents.

Its a bit strange that there is an event fired with no selected items - it
seems that, when you click on a 2nd different item (without the shift key -
i.e. not making a multi selection), internally the listbox selection is
cleared, causing the event to fire with SelectedItems containing nothing,
before the 2nd item is added to the SelectedItems and the event is fired
again.

You should always protect against there being no selection anyway,
especially in a multiselect listbox - i.e. something like this:

if (contents.Count > 0)
textBox1.Text = contents[0].SubItems[1].Text;
else
textBox1.Text = "(Nothing)";
Cheers,
Pete Beech
"Vanessa" <va*****@hipperdy.hop> wrote in message
news:10***************@news-01.evisp.enertel.nl...

With this program I can do one selection,
but upon the second I get an error where
///////////////// is indicated. Please help.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication1
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.ColumnHeader columnHeader1;
private System.Windows.Forms.ColumnHeader columnHeader2;
private System.Windows.Forms.ListView listView1;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
this.listView1 = new System.Windows.Forms.ListView();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(16, 192);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(256, 20);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "";
//
// columnHeader1
//
this.columnHeader1.Text = "number";
this.columnHeader1.Width = 49;
//
// columnHeader2
//
this.columnHeader2.Text = "name";
this.columnHeader2.Width = 186;
//
// listView1
//
this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.columnHeader1,
this.columnHeader2});
this.listView1.GridLines = true;
this.listView1.LabelEdit = true;
this.listView1.Location = new System.Drawing.Point(24, 16);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(256, 160);
this.listView1.TabIndex = 0;
this.listView1.View = System.Windows.Forms.View.Details;
this.listView1.SelectedIndexChanged += new
System.EventHandler(this.listView1_SelectedIndexCh anged);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.listView1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWind ow; this.Name = "Form1";
this.ShowInTaskbar = false;
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
ListView.SelectedListViewItemCollection contents =
this.listView1.SelectedItems;
textBox1.Text = contents[0].SubItems[1].Text;/////////////////////error
}
private void Form1_Load(object sender, System.EventArgs e)
{
string [ ] show0 = new string [] {"one","two","three","four"};
string [ ] show1 = new string [] {"ene","mene","mane","moo"};
for (int i = 0; i<show0.Length;i++)
{
ListViewItem listItem = new ListViewItem(show0[i]);
listItem.SubItems.Add(show1[i]);
listView1.Items.Add(listItem);
}
}
}
}

Nov 16 '05 #2
Thank you Pete,

Your suggestion solved that one.

Can you also input into a listView? I do not see a "Read Only = true/false"
in with the Properties.
"Pete Beech" <pe*********@hotmail.nojunk.com> wrote in message
news:un*************@TK2MSFTNGP12.phx.gbl...
You should always protect against there being no selection anyway,
especially in a multiselect listbox - i.e. something like this:

if (contents.Count > 0)
textBox1.Text = contents[0].SubItems[1].Text;
else
textBox1.Text = "(Nothing)";

Nov 16 '05 #3
Setting LabelEdit to true allows the first column (the 'label') to be
edited. (In the example, LabelEdit is set to true.) To edit, you can single
click an item that is already selected.

"Patrick de Ridder" <se***@freeler.nl> wrote in message
news:10***************@news-01.evisp.enertel.nl...
Thank you Pete,

Your suggestion solved that one.

Can you also input into a listView? I do not see a "Read Only = true/false" in with the Properties.
"Pete Beech" <pe*********@hotmail.nojunk.com> wrote in message
news:un*************@TK2MSFTNGP12.phx.gbl...
You should always protect against there being no selection anyway,
especially in a multiselect listbox - i.e. something like this:

if (contents.Count > 0)
textBox1.Text = contents[0].SubItems[1].Text;
else
textBox1.Text = "(Nothing)";


Nov 16 '05 #4
What about the second column? Can't be done?

"Pete Beech" <pe*********@hotmail.nojunk.com> wrote in message
news:eG**************@tk2msftngp13.phx.gbl...
Setting LabelEdit to true allows the first column (the 'label') to be
edited. (In the example, LabelEdit is set to true.) To edit, you can single click an item that is already selected.


Nov 16 '05 #5
Not as far as I can see, but I have never looked into it before.

Pete

"Vanessa" <no*******@all.here> wrote in message
news:10***************@news-01.evisp.enertel.nl...
What about the second column? Can't be done?

"Pete Beech" <pe*********@hotmail.nojunk.com> wrote in message
news:eG**************@tk2msftngp13.phx.gbl...
Setting LabelEdit to true allows the first column (the 'label') to be
edited. (In the example, LabelEdit is set to true.) To edit, you can

single
click an item that is already selected.


Nov 16 '05 #6
Pete,
Thanks for responding. Guess it cannot be done.
Don't see much use for just being able to edit
the first column and not the orthers.

"Pete Beech" <pe*********@hotmail.nojunk.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Not as far as I can see, but I have never looked into it before.

Pete

"Vanessa" <no*******@all.here> wrote in message
news:10***************@news-01.evisp.enertel.nl...
What about the second column? Can't be done?

"Pete Beech" <pe*********@hotmail.nojunk.com> wrote in message
news:eG**************@tk2msftngp13.phx.gbl...
Setting LabelEdit to true allows the first column (the 'label') to be
edited. (In the example, LabelEdit is set to true.) To edit, you can

single
click an item that is already selected.



Nov 16 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by J_Max | last post: by
7 posts views Thread by Dave Y | last post: by
3 posts views Thread by MikeY | last post: by
1 post views Thread by Derck | last post: by
12 posts views Thread by J L | last post: by
1 post views Thread by Chris | last post: by
12 posts views Thread by garyusenet | last post: by
1 post views Thread by =?Utf-8?B?THluYkBtcy5jb20=?= | last post: by
5 posts views Thread by Mark Olbert | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.