TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# ImageList Use: Windows Forms

ImageList provides a container for image data. The control is not visible directly. It is instead referenced from other controls such as ListView, which acquire the images from index values into the ImageList.

Info: The ImageList is simple to create. We add images manually or dynamically.

Add ImageList. To begin, let's add an ImageList control to your Windows Forms program in Visual Studio by double-clicking on the ImageList entry in the Toolbox. The ImageList will appear in the tray of the designer at the bottom.

Next: Try right-clicking on the ImageList instance and select Properties. From there, you can add images manually in the dialog.

ListView. How can you reference the ImageList in other controls such as the ListView? First, create the ImageList. Then, find the LargeImageList and SmallImageList properties on the ListView.

Instance: Please select the ImageList instance you created as the value of these properties.

Then: You can use the ListView to specify the index of the images inside your ImageList.

ListView
Add images. You can add any image to the ImageList dynamically by invoking the Add method. This method has several overloaded versions and you can call any of them. Here we look at code that uses the Add method.

Here: We have a list of file names, and then add each as an Image object using the Image.FromFile method to read the data.

Info: The Form1_Load event handler is used to make sure the code is run at startup of the application.

Example of using Add method on ImageList: C# using System; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // Add these file names to the ImageList on load. string[] files = { "image.png", "logo.jpg" }; var images = imageList1.Images; foreach (string file in files) { // Use Image.FromFile to load the file. images.Add(Image.FromFile(file)); } } } }
Summary. By providing a container for images, the ImageList control gives Windows Forms programs another level of abstraction for managing images. You can reuse the same image in multiple places or with multiple controls.

Finally: You can add images manually through Visual Studio or dynamically through C# code using the ImageList.

© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf