473,404 Members | 2,174 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,404 software developers and data experts.

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 2506
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: andrewcw | last post by:
I have a simple winform with the following code. But although I can read back the info, the display fails to provide the text or the cell background color changes. private void ListViewBroke()...
1
by: J_Max | last post by:
Hello, This might be a really easy question, but... I am developing a simple Smart Device application that uses a listview. I have a function that adds a item to the listview - code is below. I...
7
by: Dave Y | last post by:
I am a newbie to C# and am having trouble trying to override a ListView property method. I have created a new class derived from the Forms.Listview and I cannot figure out the syntax to override...
3
by: MikeY | last post by:
Hopefully someone can help me on this. I am using C#, making Windows forms. I have created a listView with checkbox's. I have enabled the checkboxes under the properties, and all the data,...
1
by: Derck | last post by:
SORRY, for the crosspost, but I think I posted it in the wrong group! Hello all, I have a question.. I am tying to make a global listview class where other listviews in my application points...
12
by: J L | last post by:
When I fill a listview, I resize the columns to fit the data. I need to know if the data will fit vertically or if there will be a vertical scroll bar. I need to know this so I can allow for it on...
1
by: Chris | last post by:
Hi all, I posted the following in microsoft.public.dotnet.framework.windowsforms but it seems that group has little traffic. Hi all, I have a listview box which is populated from methods of...
12
by: garyusenet | last post by:
I have had no replies to my previous post so perhaps I didn't write it good enough. Please excuse new thread but i wanted to break from the last thread hopefully this thread will be better. ...
1
by: =?Utf-8?B?THluYkBtcy5jb20=?= | last post by:
I have a executable winforms application I would like to change. I use quite a number of listview controls in my main form. I dump about 15 columns of data into a couple of listviews. This data...
5
by: Mark Olbert | last post by:
How do I get the DataPager and ListView to play nice together when I use a custom datasource? In my webpage, I use linq to pull data from a SqlServer database and assign the resulting...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
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.