Du kan väl använda dig av SelectedIndexChanged som du sätter till en vald metod.
Hej
Nu är jag fast i listView smeten igen.
Den här gången har jag en listview som populerats via Active Directoriet
i ett antal "items"(rader) som lästs ut från en arraylist under columnerna.
Nu vill jag i samband med att jag väljer (OnFocus, OnClick eller dylikt) en rad (item) att då ska
jag trigga en funktion/metod som lägger ut innehållet i den raden i ett par textboxar istället.
Jag slänger med kod för en betydligt enklare variant av listviewer för lättare överblick på koden.
Samt att det är bara att Copy/Pasta allt till ett nytt projekt för er så spar ni lite tid...
Tacksam för allehanda uppslag och ideer om hur man ska kunna fixa detta. Jag är tyvärr fortfarande
slav under det gamla funktionsorienterade tänket vilket gör att objekt tänket är grymt klurigt för
en annan än så länge...
Så jag hoppas ni kan ha överseende med mina ibland kanske naiva frågor...
namespace ListViewSort
{
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.Button button1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
// Create a new ListView control.
// ListView listView1 = new ListView();
listView1.Bounds = new Rectangle(new Point(10,10), new Size(344,200));
// Set the view to show details.
listView1.View = View.Details;
// Allow the user to edit item text.
listView1.LabelEdit = true;
// Allow the user to rearrange columns.
listView1.AllowColumnReorder = true;
// Display check boxes.
listView1.CheckBoxes = true;
// Select the item and subitems when selection is made.
listView1.FullRowSelect = true;
// Display grid lines.
listView1.GridLines = true;
// Sort the items in the list in ascending order.
listView1.Sorting = SortOrder.Ascending;
// Create three items and three sets of subitems for each item.
ListViewItem item1 = new ListViewItem("item1",0);
// Place a check mark next to the item.
item1.Checked = true;
item1.SubItems.Add("1");
item1.SubItems.Add("44");
item1.SubItems.Add("3");
ListViewItem item2 = new ListViewItem("item2",1);
item2.SubItems.Add("22");
item2.SubItems.Add("5");
item2.SubItems.Add("3");
ListViewItem item3 = new ListViewItem("item3",0);
// Place a check mark next to the item.
item3.Checked = false;
item3.SubItems.Add("66");
item3.SubItems.Add("23");
item3.SubItems.Add("43");
ListViewItem item4 = new ListViewItem("item4",0);
// Place a check mark next to the item.
item4.Checked = false;
item4.SubItems.Add("11");
item4.SubItems.Add("8");
item4.SubItems.Add("1");
ListViewItem item5 = new ListViewItem("item5",2);
// Place a check mark next to the item.
item5.Checked = false;
item5.SubItems.Add("5");
item5.SubItems.Add("44");
item5.SubItems.Add("13");
ListViewItem item6 = new ListViewItem("item6",0);
// Place a check mark next to the item.
item6.Checked = false;
item6.SubItems.Add("0");
item6.SubItems.Add("-1");
item6.SubItems.Add("48");
// Create columns for the items and subitems.
listView1.Columns.Add("Item Column", -2, HorizontalAlignment.Left);
listView1.Columns.Add("Column 2", -2, HorizontalAlignment.Left);
listView1.Columns.Add("Column 3", -2, HorizontalAlignment.Left);
listView1.Columns.Add("Column 4", -2, HorizontalAlignment.Center);
//Add the items to the ListView.
listView1.Items.AddRange(new ListViewItem[]{item1,item2,item3, item4, item5, item6});
// Create two ImageList objects.
ImageList imageListSmall = new ImageList();
ImageList imageListLarge = new ImageList();
// Initialize the ImageList objects with bitmaps.
imageListSmall.Images.Add(SystemIcons.Asterisk.ToBitmap());
imageListSmall.Images.Add(SystemIcons.Application.ToBitmap());
imageListLarge.Images.Add(SystemIcons.Exclamation.ToBitmap());
imageListLarge.Images.Add(SystemIcons.Hand.ToBitmap());
//Assign the ImageList objects to the ListView.
listView1.LargeImageList = imageListLarge;
listView1.SmallImageList = imageListSmall;
// Add the ListView to the control collection.
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.listView1 = new System.Windows.Forms.ListView();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.textBox4 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// listView1
//
this.listView1.Location = new System.Drawing.Point(8, 16);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(344, 208);
this.listView1.TabIndex = 0;
this.listView1.ColumnClick += new System.Windows.Forms.ColumnClickEventHandler(this.listView1_ColumnClick);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(8, 240);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 1;
this.textBox1.Text = "";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(8, 264);
this.textBox2.Name = "textBox2";
this.textBox2.TabIndex = 2;
this.textBox2.Text = "";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(120, 240);
this.textBox3.Name = "textBox3";
this.textBox3.TabIndex = 3;
this.textBox3.Text = "";
//
// textBox4
//
this.textBox4.Location = new System.Drawing.Point(120, 264);
this.textBox4.Name = "textBox4";
this.textBox4.TabIndex = 4;
this.textBox4.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(232, 240);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(120, 48);
this.button1.TabIndex = 5;
this.button1.Text = "Hämta Innehåll från rad till textbox";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(376, 302);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox4);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.listView1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
public static void Main()
{
Application.Run(new Form1());
}
private void listView1_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)
{
//don't sort col 0
if(e.Column > 0)
{
SorterClass sc = new SorterClass(e.Column, listView1.Sorting);
listView1.ListViewItemSorter = sc;
if(listView1.Sorting == SortOrder.Ascending)
listView1.Sorting = SortOrder.Descending;
else
listView1.Sorting = SortOrder.Ascending ;
}
}
}
public class SorterClass : IComparer
{
private int _colNum = 0;
private SortOrder _sortOrder = SortOrder.Ascending;
public SorterClass(int colNum, SortOrder sortOrder)
{
_colNum = colNum;
_sortOrder = sortOrder;
}
//this routine should return -1 if x<y, 1 if x>y and 0 if x==y.
// for our sample we'll just use string comparison
public int Compare(object x, object y)
{
System.Windows.Forms.ListViewItem item1 = (System.Windows.Forms.ListViewItem) x;
System.Windows.Forms.ListViewItem item2 = (System.Windows.Forms.ListViewItem) y;
if(_sortOrder == SortOrder.Descending)
{
if(int.Parse(item1.SubItems[_colNum].Text) < int.Parse(item2.SubItems[_colNum].Text))
return -1;
else if(int.Parse(item1.SubItems[_colNum].Text) > int.Parse(item2.SubItems[_colNum].Text))
return 1;
else
return 0;
}
else
{
if(int.Parse(item1.SubItems[_colNum].Text) > int.Parse(item2.SubItems[_colNum].Text))
return -1;
else if(int.Parse(item1.SubItems[_colNum].Text) < int.Parse(item2.SubItems[_colNum].Text))
return 1;
else
return 0;
}
}
}
}
Tack för uppmärksamheten :e
/T

