opérations sur un listview
Slt!
Je programmee en visual C# 2005 et j'ai créé 3 boutons : l'un pour l'ajout de fichiers en (ca marche bien), l'autre pour la modification de l'élément selectionné du listview et le dernier pour la suppression de l'élément selectionné du listview.
J'ai un problème au niveau du code pour le bouton modifier, j'ai essayé
Code:
1 2 3 4 5 6 7 8 9 10 11 12
|
private void mod_Click(object sender, EventArgs e)
{
listView1.SelectedItems.Clear();
if (of.ShowDialog() == DialogResult.Cancel)
return;
else
{
string fil = of.FileName;
listView1.Items.Add(fil);
}
} |
ce code ne fait qu'ajouter de nouveaux éléments sans pour autant me supprimer l'élément selectionné avant le clic sur le bouton modifier.
Par là donc, pour la suppression,
Code:
1 2 3 4 5
|
private void sup_Click(object sender, EventArgs e)
{
listView1.SelectedItems .Clear();
} |
ne marche pas.
Merci TM018
Voici le code comme demandé
Voici le code comme demandé.
Code:
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
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace compone
{
public partial class Form1 : Form
{
string fil; string[] files;
public Form1()
{
InitializeComponent();
}
private void ad_Click(object sender, EventArgs e)
{
if (of.ShowDialog() == DialogResult.Cancel)
return;
else
{
string []files=of.FileNames ;
foreach (string fil in files)
{
ListViewItem mic = new ListViewItem(fil);
FileInfo tm0 = new FileInfo(fil);
listView1.Items.Add (""+mic,""+tm0.Extension,""+tm0.Length );
}
}
}
private void mod_Click(object sender, EventArgs e)
{
//Sert à modifier l'élément selectionnné mais ne fonctionne pas!!!
try
{
listView1.Items[SelectedIndex] = fil;
}
catch (Exception tm)
{
MessageBox.Show(tm.Message);
}
}
private void sup_Click(object sender, EventArgs e)
{
listView1.Items.RemoveAt(listView1.SelectedIndex);
}
private void slf_Click(object sender, EventArgs e)
{
try
{
FolderBrowserDialog mic = new FolderBrowserDialog();
mic.ShowDialog();
listView1.Items.Add(mic.SelectedPath);
}
catch (Exception m)
{
MessageBox.Show(m.Message);
}
}
private void Form1_Load(object sender, EventArgs e)
{
sup.BackColor = Color.Beige;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("Voulez-vous vraiment fermer ?", "Confirmation", MessageBoxButtons.OKCancel,MessageBoxIcon.Question) == DialogResult.Cancel)
{
e.Cancel = true;
}
}
private void qt_MouseHover(object sender, EventArgs e)
{
ToolTip tm = new ToolTip();
tm.SetToolTip(qt, "Ceci est mon controle préféré!");
}
private void qt_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
} |
Je rappelle que le bouton ajouter fonctionne sur le listview!!!
Comme vous le vouyez sur la liste déroulante de l'image ,
il n'ya pas selected index.
L'image montre l'erreur générée par le code essayé.
J'ai aussi essayé pour le bouton mod:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
private void mod_Click(object sender, EventArgs e)
{
//Sert à modifier l'élément selectionnné mais ne fonctionne pas!!!
try
{
listView1.Items.Remove(listView1.SelectedItems);
}
catch (Exception tm)
{
MessageBox.Show(tm.Message);
}
} |
Merci
TM018