1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WinCustomListView2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.listView1.View = View.Details;
this.listView1.Font = new Font("Tahoma", 10.0f, FontStyle.Bold);
ColumnHeader columnHeader1 = new ColumnHeader();
columnHeader1.Text = "Item";
columnHeader1. Width = 146;
columnHeader1. TextAlign = HorizontalAlignment.Left;
this.listView1.Columns.Add(columnHeader1);
String[] foodList = new String[] {"Jus de Fruit.txt", "Cafe.txt",
"Cereales.txt", "Fromage.txt"};
int count ;
for( count = 0 ;count<foodList.Length ;count++)
{
ListViewItem listItem =new ListViewItem(foodList[count]);
this.listView1.Items.Add(listItem);
}
myBoxNom.MouseEnter += new EventHandler(myBoxNom_MouseEnter);
myBoxNom.LostFocus += new EventHandler(myBoxNom_LostFocus);
}
private TextBox myBoxNom = new TextBox();
private TextBox myBoxExt = new TextBox();
private string[] arrStr ;
private ListViewItem currentItem = null;
private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
if (myBoxNom !=null )
{
listView1.Controls.Remove(myBoxNom);
listView1.Controls.Remove(myBoxExt);
}
currentItem = e.Item;
Rectangle rect = currentItem.GetBounds(ItemBoundsPortion.Label);
Graphics gr = listView1.CreateGraphics();
char[] sep =".".ToCharArray();
arrStr = currentItem.Text.Split(sep);
String strNom = arrStr[0];
String strExt = sep[0] + arrStr[1];
SizeF szNom = gr.MeasureString(strNom, e.Item.Font);
gr.Dispose();
Rectangle rectNom = new Rectangle(rect.Left, rect.Top, (int)szNom.Width, (int)szNom.Height);
Rectangle rectExt = new Rectangle(rectNom.Right, rectNom.Top, rect.Width - (int)szNom.Width, rect.Height - (int)szNom.Height);
currentItem.Focused = false;
myBoxNom.ForeColor =currentItem.ForeColor ;
myBoxNom.Size = new Size(rectNom.Width, rectNom.Height);
myBoxNom.Location = rectNom.Location;
myBoxNom.Text = strNom;
this.listView1.Controls.Add(myBoxNom);
myBoxExt.ForeColor = currentItem.ForeColor;
myBoxExt.Size = new Size(rectExt.Width, rectExt.Height);
myBoxExt.Location = rectExt.Location;
myBoxExt.Text = strExt;
myBoxExt.ReadOnly = true;
this.listView1.Controls.Add(myBoxExt);
myBoxNom.Focus();
}
void myBoxNom_LostFocus(object sender, EventArgs e)
{
currentItem.Text = myBoxNom.Text + myBoxExt.Text;
}
void myBoxNom_MouseEnter(object sender, EventArgs e)
{
myBoxNom.ForeColor = Color.Blue ;
}
}
} |
Partager