windowsフォームアプリケーションの作成で、
フォルダを選択するダイアログから指定したフォルダ内のファイルをすべてListViewに表示させるようにしているのですが、
実行結果はファイルパス名が表示されるとこまでは行けたのですが。。
一緒にそのファイルの中にある写真データをサムネイルのように、
中身の写真を表示させその写真の下にファイル名を表示させたいのですがどのようにコードを書けばいいのかわかりません。
ImageListを使って写真を格納してlistviewプロパティから表示させてみたのですが、
写真とファイルパスのそれぞれが別々の一覧で表示されてしまいました。
例えば、ImageListのコレクションメンバーに写真をダイアログで選択したファイルの画像を動的に格納できたりしませんか?
以下に現在までのコード添付します。
よろしくお願いいたします。
C#
1using System; 2using System.Collections.Generic; 3using System.ComponentModel; 4using System.Drawing; 5using System.Data; 6using System.Linq; 7using System.Text; 8using System.Threading.Tasks; 9using System.Windows.Forms; 10using System.Collections; 11using System.IO; 12 13namespace Page_Switch 14{ 15 public partial class FolderDialog : UserControl 16 { 17 /// <summary> 18 /// 開いているフォルダ名 19 /// </summary> 20 private string FolderName = ""; 21 /// <summary> 22 /// フォルダ内のすべてのファイルパス 23 /// </summary> 24 private string files = ""; 25 26 public FolderDialog() 27 { 28 InitializeComponent(); 29 } 30 31 /// <summary> 32 /// フォルダ選択ダイアログ表示 33 /// </summary> 34 /// <param name="FolderName">ファイル格納先</param> 35 public bool SelectFolderDialog(ref string FolderName) 36 { 37 bool ret = false; 38 39 //FolderBrowserDialogクラスのインスタンスを作成 40 FolderBrowserDialog fbd = new FolderBrowserDialog(); 41 42 //上部に表示する説明テキストを指定する 43 fbd.Description = "フォルダを指定してください。"; 44 //ルートフォルダを指定する 45 //デフォルトでDesktop 46 fbd.RootFolder = Environment.SpecialFolder.Desktop; 47 //最初に選択するフォルダを指定する 48 //RootFolder以下にあるフォルダである必要がある 49 fbd.SelectedPath = @"C:\Windows"; 50 //ユーザーが新しいフォルダを作成できるようにする 51 //デフォルトでTrue 52 fbd.ShowNewFolderButton = true; 53 54 //ダイアログを表示する 55 if (fbd.ShowDialog(this) == DialogResult.OK) 56 { 57 //選択されたフォルダを表示する 58 Console.WriteLine(fbd.SelectedPath); 59 FolderName = (fbd.SelectedPath); 60 ret = true; 61 } 62 return ret; 63 } 64 65 /// <summary> 66 /// フォルダ選択ボタンクリック 処理 67 /// </summary> 68 /// <param name="sender">オブジェクト</param> 69 /// <param name="e">イベント</param> 70 private void SelectFolderBtn_Click(object sender, EventArgs e) 71 { 72 // ファイ選択ダイアログを呼び出して変数を使い写真を表示 73 if (SelectFolderDialog(ref FolderName)) 74 { 75 //既にデータがあれば消す 76 if (FolderView.Items != null) 77 { 78 FolderView.Items.Clear(); 79 } 80 81 //指定ファイル以降のファイルをすべて取得する 82 IEnumerable<string> files = System.IO.Directory.EnumerateFiles(FolderName, "*", System.IO.SearchOption.AllDirectories); 83 //ファイルを列挙する 84 foreach (string f in files) 85 { 86 FolderView.Items.Add( f , ImageList.Images.Count); 87 } 88 } 89 90 } 91 92 } 93} 94
C#
1namespace Page_Switch 2{ 3 partial class FolderDialog 4 { 5 /// <summary> 6 /// 必要なデザイナー変数です。 7 /// </summary> 8 private System.ComponentModel.IContainer components = null; 9 10 /// <summary> 11 /// 使用中のリソースをすべてクリーンアップします。 12 /// </summary> 13 /// <param name="disposing">マネージ リソースを破棄する場合は true を指定し、その他の場合は false を指定します。</param> 14 protected override void Dispose(bool disposing) 15 { 16 if (disposing && (components != null)) 17 { 18 components.Dispose(); 19 } 20 base.Dispose(disposing); 21 } 22 23 #region コンポーネント デザイナーで生成されたコード 24 25 /// <summary> 26 /// デザイナー サポートに必要なメソッドです。このメソッドの内容を 27 /// コード エディターで変更しないでください。 28 /// </summary> 29 private void InitializeComponent() 30 { 31 this.components = new System.ComponentModel.Container(); 32 System.Windows.Forms.ListViewItem listViewItem1 = new System.Windows.Forms.ListViewItem(""); 33 System.Windows.Forms.ListViewItem listViewItem2 = new System.Windows.Forms.ListViewItem(""); 34 System.Windows.Forms.ListViewItem listViewItem3 = new System.Windows.Forms.ListViewItem(""); 35 System.Windows.Forms.ListViewItem listViewItem4 = new System.Windows.Forms.ListViewItem(""); 36 System.Windows.Forms.ListViewItem listViewItem5 = new System.Windows.Forms.ListViewItem(""); 37 System.Windows.Forms.ListViewItem listViewItem6 = new System.Windows.Forms.ListViewItem(""); 38 System.Windows.Forms.ListViewItem listViewItem7 = new System.Windows.Forms.ListViewItem(""); 39 System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FolderDialog)); 40 this.SelectFolderBtn = new System.Windows.Forms.Button(); 41 this.FolderView = new System.Windows.Forms.ListView(); 42 this.ImageList = new System.Windows.Forms.ImageList(this.components); 43 this.SuspendLayout(); 44 // 45 // SelectFolderBtn 46 // 47 this.SelectFolderBtn.Location = new System.Drawing.Point(21, 16); 48 this.SelectFolderBtn.Name = "SelectFolderBtn"; 49 this.SelectFolderBtn.Size = new System.Drawing.Size(89, 32); 50 this.SelectFolderBtn.TabIndex = 0; 51 this.SelectFolderBtn.Text = "フォルダ選択"; 52 this.SelectFolderBtn.UseVisualStyleBackColor = true; 53 this.SelectFolderBtn.Click += new System.EventHandler(this.SelectFolderBtn_Click); 54 // 55 // FolderView 56 // 57 this.FolderView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 58 | System.Windows.Forms.AnchorStyles.Left) 59 | System.Windows.Forms.AnchorStyles.Right))); 60 this.FolderView.Items.AddRange(new System.Windows.Forms.ListViewItem[] { 61 listViewItem1, 62 listViewItem2, 63 listViewItem3, 64 listViewItem4, 65 listViewItem5, 66 listViewItem6, 67 listViewItem7}); 68 this.FolderView.LargeImageList = this.ImageList; 69 this.FolderView.Location = new System.Drawing.Point(21, 67); 70 this.FolderView.Name = "FolderView"; 71 this.FolderView.Size = new System.Drawing.Size(620, 285); 72 this.FolderView.SmallImageList = this.ImageList; 73 this.FolderView.TabIndex = 1; 74 this.FolderView.UseCompatibleStateImageBehavior = false; 75 // 76 // ImageList 77 // 78 this.ImageList.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("ImageList.ImageStream"))); 79 this.ImageList.TransparentColor = System.Drawing.Color.Transparent; 80 this.ImageList.Images.SetKeyName(0, "apple.jpg"); 81 this.ImageList.Images.SetKeyName(1, "NewYork.jpg"); 82 this.ImageList.Images.SetKeyName(2, "Hydrangeas.jpg"); 83 this.ImageList.Images.SetKeyName(3, "Jellyfish.jpg"); 84 this.ImageList.Images.SetKeyName(4, "Koala.jpg"); 85 this.ImageList.Images.SetKeyName(5, "Tulips.jpg"); 86 this.ImageList.Images.SetKeyName(6, "Penguins.jpg"); 87 this.ImageList.Images.SetKeyName(7, "Chrysanthemum.jpg"); 88 this.ImageList.Images.SetKeyName(8, "Desert.jpg"); 89 this.ImageList.Images.SetKeyName(9, "Lighthouse.jpg"); 90 // 91 // FolderDialog 92 // 93 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); 94 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 95 this.Controls.Add(this.FolderView); 96 this.Controls.Add(this.SelectFolderBtn); 97 this.Name = "FolderDialog"; 98 this.Size = new System.Drawing.Size(660, 366); 99 this.ResumeLayout(false); 100 101 } 102 103 #endregion 104 105 private System.Windows.Forms.Button SelectFolderBtn; 106 private System.Windows.Forms.ListView FolderView; 107 private System.Windows.Forms.ImageList ImageList; 108 } 109} 110


回答2件
あなたの回答
tips
プレビュー