Bonsoir,
Je viens de faire ce que tu souhaites faire en light voire très light
Mais tu as une bonne base pour commençer...
Le code du designer.cs
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
| partial class FileDirectorySelector
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (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.treeView1 = new System.Windows.Forms.TreeView();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// treeView1
//
this.treeView1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.treeView1.CheckBoxes = true;
this.treeView1.FullRowSelect = true;
this.treeView1.HideSelection = false;
this.treeView1.Location = new System.Drawing.Point(12, 12);
this.treeView1.Name = "treeView1";
this.treeView1.ShowNodeToolTips = true;
this.treeView1.Size = new System.Drawing.Size(300, 321);
this.treeView1.TabIndex = 0;
//
// button1
//
this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.button1.Location = new System.Drawing.Point(83, 341);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "&OK";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.button2.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.button2.Location = new System.Drawing.Point(162, 341);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 2;
this.button2.Text = "&Cancel";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// FileDirectorySelector
//
this.AcceptButton = this.button1;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.CancelButton = this.button2;
this.ClientSize = new System.Drawing.Size(324, 373);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.treeView1);
this.Name = "FileDirectorySelector";
this.ShowIcon = false;
this.ShowInTaskbar = false;
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.TreeView treeView1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
} |
code de la classe :
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
| public partial class FileDirectorySelector : Form
{
protected List<FileSystemInfo> SelectedObjects { get; set; }
private FileDirectorySelector()
{
InitializeComponent();
SelectedObjects = new List<FileSystemInfo>();
}
public static IEnumerable<FileSystemInfo> Afficher(IWin32Window owner, string pText)
{
using (FileDirectorySelector fds = new FileDirectorySelector())
{
fds.Text = pText;
DialogResult result;
if (owner == null)
result = fds.ShowDialog();
else
result = fds.ShowDialog(owner);
if (result == DialogResult.OK)
{
return fds.SelectedObjects;
}
}
return null;
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
var root = treeView1.Nodes.Add(Environment.MachineName);
root.Nodes.AddRange((from drive in DriveInfo.GetDrives()
where drive.IsReady
select new TreeNode(drive.RootDirectory.Name) { Tag = drive.RootDirectory/*, ImageKey = "ImageLecteur"*/}).ToArray());
treeView1.BeforeExpand += new TreeViewCancelEventHandler(treeView1_BeforeExpand);
treeView1.AfterCheck += new TreeViewEventHandler(treeView1_AfterCheck);
}
void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
if (e.Node.Checked)
{
if (e.Node.Tag != null)
SelectedObjects.Add((FileSystemInfo)e.Node.Tag);
}
else
{
if (e.Node.Tag != null)
SelectedObjects.Remove((FileSystemInfo)e.Node.Tag);
}
}
void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
{
DirectoryInfo di;
foreach (TreeNode item in e.Node.Nodes)
{
if (item.Tag is DirectoryInfo)
{
try
{
di = (DirectoryInfo)item.Tag;
item.Nodes.Clear();
item.Nodes.AddRange((from repertoire in di.EnumerateDirectories()
select new TreeNode(repertoire.Name) { Tag = repertoire/*, ImageKey = "ImageRepertoire"*/}).ToArray());
item.Nodes.AddRange((from fichier in di.EnumerateFiles()
select new TreeNode(fichier.Name) { Tag = fichier/*, ImageKey = "ImageFichier"*/}).ToArray());
}
catch
{
//Si une exception se produit, on ne traite pas l'erreur
}
}
}
}
private void button2_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
this.Close();
}
} |
et à l'utilisation :
IEnumerable<FileSystemInfo> fichiersEtDossiersSelectionnes = FileDirectorySelector.Afficher(this, "coucou");
Bon courage...
PS : Nous n'avons pas toujours le temps pour répondre rapidement à tes questions, sois un peu compréhensif
Partager