ListView problem
Jeg har følgende Form med et listview:-----------------------------------------------------------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace TidsurClient
{
public class TidsursOperationer : System.Windows.Forms.Form
{
private JobValgDialog jobvalg;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
public System.Windows.Forms.ListView listView1;
private System.ComponentModel.Container components = null;
public TidsursOperationer(JobValgDialog jobvalg)
{
this.jobvalg = jobvalg;
InitializeComponent();
InitializeListView();
}
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.groupBox1 = new System.Windows.Forms.GroupBox();
this.listView1 = new System.Windows.Forms.ListView();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.listView1);
this.groupBox1.Location = new System.Drawing.Point(8, 8);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(378, 256);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
//
// listView1
//
this.listView1.ForeColor = System.Drawing.SystemColors.WindowText;
this.listView1.FullRowSelect = true;
this.listView1.GridLines = true;
this.listView1.Location = new System.Drawing.Point(8, 16);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(360, 232);
this.listView1.TabIndex = 0;
this.listView1.View = System.Windows.Forms.View.Details;
this.listView1.SelectedIndexChanged += new System.EventHandler(this.listView1_SelectedIndexChanged);
//
// button1
//
this.button1.Location = new System.Drawing.Point(312, 280);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "Hjælp";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(216, 280);
this.button2.Name = "button2";
this.button2.TabIndex = 2;
this.button2.Text = "Annuller";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(120, 280);
this.button3.Name = "button3";
this.button3.TabIndex = 3;
this.button3.Text = "OK";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// TidsursOperationer
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(400, 323);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.groupBox1);
this.MaximumSize = new System.Drawing.Size(408, 350);
this.MinimumSize = new System.Drawing.Size(408, 350);
this.Name = "TidsursOperationer";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "TidsursOperationer";
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private void InitializeListView()
{
ColumnHeader header1 = this.listView1.Columns.Add("Operation", 50, HorizontalAlignment.Center);
ColumnHeader header2 = this.listView1.Columns.Add("Beskrivelse", 250, HorizontalAlignment.Left);
}
private void button2_Click(object sender, System.EventArgs e)
{
this.Dispose();
}
private void button1_Click(object sender, System.EventArgs e)
{
MessageBox.Show("HjælpeFunktionen.....");
}
private void button3_Click(object sender, System.EventArgs e)
{
MessageBox.Show("IKKE FÆRDIGIMPLEMENTERET");
}
private void listView1_SelectedIndexChanged(object sender, System.EventArgs e)
{
MessageBox.Show("Index: " + listView1.SelectedItems[0].Index.ToString());
}
}
}
-----------------------------------------------------------
Får indfyldt data fra et object jeg selv har lavet - SetPacket setPacket :
-----------------------------------------------------------
private void operationsBeskrivelse()
{
TidsursOperationer t = new TidsursOperationer(jobvalg);
string[] item;
sendPacket();
receiveSetPacket();
for(int i = 0; i < setPacket.GetSize(); i++)
{
item = new string[]
{
setPacket.GetSOperation(i), setPacket.GetSOpBeskrivelse(i)
};
ListViewItem lvi = new ListViewItem(item);
//insert all the items into the listview at the last available row
t.listView1.Items.Add(lvi);
}
t.Show();
}
-----------------------------------------------------------
Når man vælger en linie med musen oprettes en MessageBox, som udskriver items index. Dette lykkes første gang, men anden gang man trykker fremkommer følgende exception:
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.
************** Exception Text **************
System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: index
at System.Windows.Forms.SelectedListViewItemCollection.get_Item(Int32 index)
at TidsurClient.TidsursOperationer.listView1_SelectedIndexChanged(Object sender, EventArgs e) in c:\mywork\csharp\tidsur\tidsurclient\tidsursoperationerdialog.cs:line 159
at System.Windows.Forms.ListView.OnSelectedIndexChanged(EventArgs e)
at System.Windows.Forms.ListView.WmReflectNotify(Message& m)
at System.Windows.Forms.ListView.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
************** Loaded Assemblies **************
mscorlib
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase: file:///c:/windows/microsoft.net/framework/v1.1.4322/mscorlib.dll
----------------------------------------
TidsurClient
Assembly Version: 1.0.1399.19255
Win32 Version: 1.0.1399.19255
CodeBase: file:///C:/MyWork/CSharp/Tidsur/TidsurClient/bin/Debug/TidsurClient.exe
----------------------------------------
System.Windows.Forms
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase: file:///c:/windows/assembly/gac/system.windows.forms/1.0.5000.0__b77a5c561934e089/system.windows.forms.dll
----------------------------------------
System
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase: file:///c:/windows/assembly/gac/system/1.0.5000.0__b77a5c561934e089/system.dll
----------------------------------------
System.Drawing
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase: file:///c:/windows/assembly/gac/system.drawing/1.0.5000.0__b03f5f7f11d50a3a/system.drawing.dll
----------------------------------------
delt
Assembly Version: 1.0.1399.15308
Win32 Version: 1.0.1399.15308
CodeBase: file:///C:/MyWork/CSharp/Tidsur/TidsurClient/bin/Debug/delt.DLL
----------------------------------------
System.Xml
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase: file:///c:/windows/assembly/gac/system.xml/1.0.5000.0__b77a5c561934e089/system.xml.dll
----------------------------------------
Hjælp!
