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);
}
}
}
} 6 2469
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); } } } }
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)";
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)";
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.
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.
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.
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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()...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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.
...
|
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...
|
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...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
| |