方法: Windows フォーム ImageList コンポーネントを使用してイメージを追加または削除する
Windows フォーム ImageList コンポーネントには、通常、コントロールに関連付けられる前にイメージが設定されます。 ただし、イメージ リストをコントロールに関連付けた後で、イメージを追加および削除できます。
手記
イメージを削除するときは、関連付けられているコントロールの ImageIndex プロパティがまだ有効であることを確認します。
プログラムで画像を追加するには
イメージ リストの Images プロパティの Add メソッドを使用します。
次のコード例では、イメージの場所に設定されたパスは、[マイ ドキュメント] フォルダー
です。 この場所は、Windows オペレーティング システムを実行しているほとんどのコンピューターにこのフォルダーが含まれると想定できるために使用されます。 この場所を選択すると、最小限のシステム アクセス レベルを持つユーザーがアプリケーションをより安全に実行することもできます。 次のコード例では、ImageList コントロールが既に追加されているフォームが必要です。 Public Sub LoadImage() Dim myImage As System.Drawing.Image = _ Image.FromFile _ (System.Environment.GetFolderPath _ (System.Environment.SpecialFolder.Personal) _ & "\Image.gif") ImageList1.Images.Add(myImage) End Sub
public void addImage() { // Be sure that you use an appropriate escape sequence (such as the // @) when specifying the location of the file. System.Drawing.Image myImage = Image.FromFile (System.Environment.GetFolderPath (System.Environment.SpecialFolder.Personal) + @"\Image.gif"); imageList1.Images.Add(myImage); }
public: void addImage() { // Replace the bold image in the following sample // with your own icon. // Be sure that you use an appropriate escape sequence (such as // \\) when specifying the location of the file. System::Drawing::Image ^ myImage = Image::FromFile(String::Concat( System::Environment::GetFolderPath( System::Environment::SpecialFolder::Personal), "\\Image.gif")); imageList1->Images->Add(myImage); }
キー値を持つイメージを追加する。
キー値を受け取るイメージ リストの Images プロパティの Add メソッドのいずれかを使用します。
次のコード例では、イメージの場所に設定されたパスは、[マイ ドキュメント] フォルダー
です。 この場所は、Windows オペレーティング システムを実行しているほとんどのコンピューターにこのフォルダーが含まれると想定できるために使用されます。 この場所を選択すると、最小限のシステム アクセス レベルを持つユーザーがアプリケーションをより安全に実行することもできます。 次のコード例では、ImageList コントロールが既に追加されているフォームが必要です。 Public Sub LoadImage() Dim myImage As System.Drawing.Image = _ Image.FromFile _ (System.Environment.GetFolderPath _ (System.Environment.SpecialFolder.Personal) _ & "\Image.gif") ImageList1.Images.Add("myPhoto", myImage) End Sub
public void addImage()
{
// Be sure that you use an appropriate escape sequence (such as the
// @) when specifying the location of the file.
System.Drawing.Image myImage =
Image.FromFile
(System.Environment.GetFolderPath
(System.Environment.SpecialFolder.Personal)
+ @"\Image.gif");
imageList1.Images.Add("myPhoto", myImage);
}
プログラムですべてのイメージを削除するには
Remove メソッドを使用して 1 つのイメージを削除する
または
Clear メソッドを使用して、イメージ リスト内のすべてのイメージをクリアします。
' Removes the first image in the image list ImageList1.Images.Remove(myImage) ' Clears all images in the image list ImageList1.Images.Clear()
// Removes the first image in the image list.
imageList1.Images.Remove(myImage);
// Clears all images in the image list.
imageList1.Images.Clear();
キーで画像を削除するには
RemoveByKey メソッドを使用して、キーによって 1 つのイメージを削除します。
' Removes the image named "myPhoto" from the list. ImageList1.Images.RemoveByKey("myPhoto")
// Removes the image named "myPhoto" from the list.
imageList1.Images.RemoveByKey("myPhoto");
関連項目
.NET Desktop feedback