Udostępnij za pośrednictwem


Instrukcje: dodawanie lub usuwanie obrazów za pomocą składnika ImageList formularzy systemu Windows

Składnik windows Forms ImageList jest zwykle wypełniany obrazami, zanim zostanie skojarzony z kontrolką. Można jednak dodawać i usuwać obrazy po skojarzeniu listy obrazów z kontrolką.

Notatka

Po usunięciu obrazów sprawdź, czy właściwość ImageIndex skojarzonych kontrolek jest nadal prawidłowa.

Aby programowo dodać obrazy

  • Użyj metody Add dla właściwości Images listy obrazów.

    W poniższym przykładzie kodu ścieżka ustawiona dla lokalizacji obrazu to folder Moje dokumenty. Ta lokalizacja jest używana, ponieważ można założyć, że większość komputerów z systemem operacyjnym Windows będzie zawierać ten folder. Wybranie tej lokalizacji umożliwia również użytkownikom, którzy mają minimalny poziom dostępu do systemu, bezpieczniej uruchamiać aplikację. Poniższy przykład kodu wymaga, aby formularz z kontrolką ImageList został już dodany.

    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);  
       }  
    

Aby dodać obrazy z kluczową wartością.

  • Użyj jednej z metod Add właściwości Images listy obrazów, która przyjmuje wartość klucza.

    W poniższym przykładzie kodu ścieżka ustawiona dla lokalizacji obrazu to folder Moje dokumenty. Ta lokalizacja jest używana, ponieważ można założyć, że większość komputerów z systemem operacyjnym Windows będzie zawierać ten folder. Wybranie tej lokalizacji umożliwia również użytkownikom, którzy mają minimalne poziomy dostępu do systemu, bezpieczniej uruchamiać aplikację. Poniższy przykład kodu wymaga, aby formularz z kontrolką ImageList został już dodany.

    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);  
}  

Aby programowo usunąć wszystkie obrazy

  • Usuwanie pojedynczego obrazu przy użyciu metody Remove

    , lub

    Użyj metody Clear, aby wyczyścić wszystkie obrazy na liście obrazów.

    ' 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();  

Aby usunąć obrazy według klucza

  • Użyj metody RemoveByKey, aby usunąć pojedynczy obraz według jego klucza.

    ' Removes the image named "myPhoto" from the list.  
    ImageList1.Images.RemoveByKey("myPhoto")  
    
// Removes the image named "myPhoto" from the list.  
imageList1.Images.RemoveByKey("myPhoto");  

Zobacz też