473,725 Members | 2,197 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Data;
namespace WindowsApplicat ion1
{
public class Form1 : System.Windows. Forms.Form
{
private System.Windows. Forms.TextBox textBox1;
private System.Windows. Forms.ColumnHea der columnHeader1;
private System.Windows. Forms.ColumnHea der columnHeader2;
private System.Windows. Forms.ListView listView1;
private System.Componen tModel.Containe r components = null;
public Form1()
{
InitializeCompo nent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Disp ose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeCompo nent()
{
this.textBox1 = new System.Windows. Forms.TextBox() ;
this.columnHead er1 = new System.Windows. Forms.ColumnHea der();
this.columnHead er2 = new System.Windows. Forms.ColumnHea der();
this.listView1 = new System.Windows. Forms.ListView( );
this.SuspendLay out();
//
// textBox1
//
this.textBox1.L ocation = new System.Drawing. Point(16, 192);
this.textBox1.N ame = "textBox1";
this.textBox1.S ize = new System.Drawing. Size(256, 20);
this.textBox1.T abIndex = 1;
this.textBox1.T ext = "";
//
// columnHeader1
//
this.columnHead er1.Text = "number";
this.columnHead er1.Width = 49;
//
// columnHeader2
//
this.columnHead er2.Text = "name";
this.columnHead er2.Width = 186;
//
// listView1
//
this.listView1. Columns.AddRang e(new System.Windows. Forms.ColumnHea der[] {
this.columnHead er1,
this.columnHead er2});
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.Deta ils;
this.listView1. SelectedIndexCh anged += new
System.EventHan dler(this.listV iew1_SelectedIn dexChanged);
//
// Form1
//
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(292, 266);
this.Controls.A dd(this.textBox 1);
this.Controls.A dd(this.listVie w1);
this.FormBorder Style = System.Windows. Forms.FormBorde rStyle.FixedToo lWindow;
this.Name = "Form1";
this.ShowInTask bar = false;
this.Text = "Form1";
this.Load += new System.EventHan dler(this.Form1 _Load);
this.ResumeLayo ut(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run (new Form1());
}
private void listView1_Selec tedIndexChanged (object sender, EventArgs e)
{
ListView.Select edListViewItemC ollection contents =
this.listView1. SelectedItems;
textBox1.Text = contents[0].SubItems[1].Text;/////////////////////error
}
private void Form1_Load(obje ct sender, System.EventArg s e)
{
string [ ] show0 = new string [] {"one","two","t hree","four"};
string [ ] show1 = new string [] {"ene","mene"," mane","moo"};
for (int i = 0; i<show0.Length; i++)
{
ListViewItem listItem = new ListViewItem(sh ow0[i]);
using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Data;
namespace WindowsApplicat ion1
{
public class Form1 : System.Windows. Forms.Form
{
private System.Windows. Forms.TextBox textBox1;
private System.Windows. Forms.ColumnHea der columnHeader1;
private System.Windows. Forms.ColumnHea der columnHeader2;
private System.Windows. Forms.ListView listView1;
private System.Componen tModel.Containe r components = null;
public Form1()
{
InitializeCompo nent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Disp ose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeCompo nent()
{
this.textBox1 = new System.Windows. Forms.TextBox() ;
this.columnHead er1 = new System.Windows. Forms.ColumnHea der();
this.columnHead er2 = new System.Windows. Forms.ColumnHea der();
this.listView1 = new System.Windows. Forms.ListView( );
this.SuspendLay out();
//
// textBox1
//
this.textBox1.L ocation = new System.Drawing. Point(16, 192);
this.textBox1.N ame = "textBox1";
this.textBox1.S ize = new System.Drawing. Size(256, 20);
this.textBox1.T abIndex = 1;
this.textBox1.T ext = "";
//
// columnHeader1
//
this.columnHead er1.Text = "number";
this.columnHead er1.Width = 49;
//
// columnHeader2
//
this.columnHead er2.Text = "name";
this.columnHead er2.Width = 186;
//
// listView1
//
this.listView1. Columns.AddRang e(new System.Windows. Forms.ColumnHea der[] {
this.columnHead er1,
this.columnHead er2});
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.Deta ils;
this.listView1. SelectedIndexCh anged += new
System.EventHan dler(this.listV iew1_SelectedIn dexChanged);
//
// Form1
//
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(292, 266);
this.Controls.A dd(this.textBox 1);
this.Controls.A dd(this.listVie w1);
this.FormBorder Style = System.Windows. Forms.FormBorde rStyle.FixedToo lWindow;
this.Name = "Form1";
this.ShowInTask bar = false;
this.Text = "Form1";
this.Load += new System.EventHan dler(this.Form1 _Load);
this.ResumeLayo ut(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run (new Form1());
}
private void listView1_Selec tedIndexChanged (object sender, EventArgs e)
{
ListView.Select edListViewItemC ollection contents =
this.listView1. SelectedItems;
textBox1.Text = contents[0].SubItems[1].Text;/////////////////////error
}
private void Form1_Load(obje ct sender, System.EventArg s e)
{
string [ ] show0 = new string [] {"one","two","t hree","four"};
string [ ] show1 = new string [] {"ene","mene"," mane","moo"};
for (int i = 0; i<show0.Length; i++)
{
ListViewItem listItem = new ListViewItem(sh ow0[i]);
listItem.SubIte ms.Add(show1[i]);
listView1.Items .Add(listItem);
}
}
}
}
listItem.SubIte ms.Add(show1[i]);
listView1.Items .Add(listItem);
}
}
}
}


Nov 16 '05 #1
6 2531
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*****@hipper dy.hop> wrote in message
news:10******** *******@news-01.evisp.enerte l.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.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Data;
namespace WindowsApplicat ion1
{
public class Form1 : System.Windows. Forms.Form
{
private System.Windows. Forms.TextBox textBox1;
private System.Windows. Forms.ColumnHea der columnHeader1;
private System.Windows. Forms.ColumnHea der columnHeader2;
private System.Windows. Forms.ListView listView1;
private System.Componen tModel.Containe r components = null;
public Form1()
{
InitializeCompo nent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Disp ose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeCompo nent()
{
this.textBox1 = new System.Windows. Forms.TextBox() ;
this.columnHead er1 = new System.Windows. Forms.ColumnHea der();
this.columnHead er2 = new System.Windows. Forms.ColumnHea der();
this.listView1 = new System.Windows. Forms.ListView( );
this.SuspendLay out();
//
// textBox1
//
this.textBox1.L ocation = new System.Drawing. Point(16, 192);
this.textBox1.N ame = "textBox1";
this.textBox1.S ize = new System.Drawing. Size(256, 20);
this.textBox1.T abIndex = 1;
this.textBox1.T ext = "";
//
// columnHeader1
//
this.columnHead er1.Text = "number";
this.columnHead er1.Width = 49;
//
// columnHeader2
//
this.columnHead er2.Text = "name";
this.columnHead er2.Width = 186;
//
// listView1
//
this.listView1. Columns.AddRang e(new System.Windows. Forms.ColumnHea der[] {
this.columnHead er1,
this.columnHead er2});
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.Deta ils;
this.listView1. SelectedIndexCh anged += new
System.EventHan dler(this.listV iew1_SelectedIn dexChanged);
//
// Form1
//
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(292, 266);
this.Controls.A dd(this.textBox 1);
this.Controls.A dd(this.listVie w1);
this.FormBorder Style = System.Windows. Forms.FormBorde rStyle.FixedToo lWindow; this.Name = "Form1";
this.ShowInTask bar = false;
this.Text = "Form1";
this.Load += new System.EventHan dler(this.Form1 _Load);
this.ResumeLayo ut(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run (new Form1());
}
private void listView1_Selec tedIndexChanged (object sender, EventArgs e)
{
ListView.Select edListViewItemC ollection contents =
this.listView1. SelectedItems;
textBox1.Text = contents[0].SubItems[1].Text;/////////////////////error
}
private void Form1_Load(obje ct sender, System.EventArg s e)
{
string [ ] show0 = new string [] {"one","two","t hree","four"};
string [ ] show1 = new string [] {"ene","mene"," mane","moo"};
for (int i = 0; i<show0.Length; i++)
{
ListViewItem listItem = new ListViewItem(sh ow0[i]);
listItem.SubIte ms.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*********@ho tmail.nojunk.co m> wrote in message
news:un******** *****@TK2MSFTNG P12.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.enerte l.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*********@ho tmail.nojunk.co m> wrote in message
news:un******** *****@TK2MSFTNG P12.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*********@ho tmail.nojunk.co m> wrote in message
news:eG******** ******@tk2msftn gp13.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.enerte l.nl...
What about the second column? Can't be done?

"Pete Beech" <pe*********@ho tmail.nojunk.co m> wrote in message
news:eG******** ******@tk2msftn gp13.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*********@ho tmail.nojunk.co m> wrote in message
news:%2******** ********@TK2MSF TNGP12.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.enerte l.nl...
What about the second column? Can't be done?

"Pete Beech" <pe*********@ho tmail.nojunk.co m> wrote in message
news:eG******** ******@tk2msftn gp13.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
2812
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() { listView.View = View.Details; ArrayList LVcolTitles= new ArrayList(); LVcolTitles.Add("Drive"); LVcolTitles.Add("Cust");
1
787
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 use the type listviewitem, selitem, on one form to modify the listviewitem, using a timer, every second, and use selitem1 to modify items, after they have ended the time on the timer form, from the form that the listview is located - this way I...
7
6448
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 ListView.Items.Add(), . I see that it is a virtual method so it should be easy to do. If anyone can help I would appreciate it greatly. I can do what I need to do in a different way this would just make everything significantly cleaner and eaasier...
3
9307
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, checkbox's and icons are displayed as they are meant to be. My problem is this, I am trying to achieve the same results as I get with CheckListBox(s), where I am able to select (Check-mark) various checkboxs items and able to alter the data. I do...
1
1433
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 to, so when I change something in that listview all listviews who points to that listview must be updated.. But I don't have a idea why it isn't working.. I tried to make a singeton listview class and get a instance on the form
12
7484
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 the overall size of the listview. My question therefore is, how can I tell if the items I have added will fit in the listview at its given height? A secondary one, just for interest sake...is there a way to determine the exact heght needed...
1
2858
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 objects stored in a List<T> collection. The items in this collection are generated from a user filled out form. The items in the listview have the same index as the ones in List<T>, so I
12
3412
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. Very simple. I would like to create listviewitem's for display in a listview control. The listview items need to contain properties from Internet Explorer windows i've managed to collect into an arraylist.
1
2174
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 comes from classes that implement Ienumerable ... I use a method in the main form to update the listviews... s. I want to change this application to Only to show 4 columns of data (new data)
5
9534
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 IEnumerable<to the ListView's Datasource property. This all works fine to display the first 3 items (the DataPager is set to display three items at a time), but when I click on the next page in the DataPager...nothing happens. There's a roundtrip to the...
0
8888
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9257
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9176
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9113
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6702
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3221
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2635
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.