C# winform How to Add Color in ListBox

Total Post:15

Points:105
C# 
 2172  View(s)
Ratings:
Rate this:
hi,

could anybody help the code for listbox color add (like add fill rectangle with name).
plz ref below image
C# winform How to Add Color in ListBox

thanks in advance
  1. Post:194

    Points:1396
    Re: C# winform How to Add Color in ListBox

    try this:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Collections;
    using System.IO;
     
    namespace listboxcolor
    {
        public partial class Form1 : Form
        {
            private System.Collections.Specialized.StringCollection folderCol;
     
            private System.Windows.Forms.ImageList ilLarge;
            private System.Windows.Forms.ImageList ilSmall;
            private System.Windows.Forms.ListView lwFilesAndFolders;
            private System.Windows.Forms.Label lblCurrentPath;
            public Form1()
            {
                InitializeComponent();
                folderCol = new System.Collections.Specialized.StringCollection();
                CreateHeadersAndFillListView();
                PaintListView();
     
            }
            private void CreateHeadersAndFillListView()
            {
                ColumnHeader colHead;
     
                colHead = new ColumnHeader();
                colHead.Text = "S/N";
                this.lwFilesAndFolders.Columns.Add(colHead);
     
                colHead = new ColumnHeader();
                colHead.Text = "Color Name";
                this.lwFilesAndFolders.Columns.Add(colHead);
     
                colHead = new ColumnHeader();
                colHead.Text = "Color View";
                this.lwFilesAndFolders.Columns.Add(colHead);
            }
     
            private void PaintListView()
            {
                ListViewItem lvi;
                ListViewItem.ListViewSubItem lvsi;
     
                try
                {
     
                    string[] color = {"Gray"};
                    for (int i = 0; i <= color.Length; i++)
                    {
                        lvi = new ListViewItem();
                        lvi.Text = "" + i;
     
                        lvsi = new ListViewItem.ListViewSubItem();
                        lvsi.Text = color[i];
                        lvi.SubItems.Add(lvsi);
     
                        lvsi = new ListViewItem.ListViewSubItem();
                        lvi.SubItems.Add(lvsi);
     
                        this.lwFilesAndFolders.Items.Add(lvi);
     
                        lwFilesAndFolders.Items[0].UseItemStyleForSubItems = false;
                        lwFilesAndFolders.Items[0].SubItems[2].BackColor = Color.Gray;
     
                    }
                }
                catch (System.Exception err)
                {
                    MessageBox.Show("Error: " + err.Message);
                }
     
                this.lwFilesAndFolders.View = View.Details;
               
            }
        }
    }

     

    Form1.Designer.cs:

    private void InitializeComponent()
            {
                //      System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
                this.lblCurrentPath = new System.Windows.Forms.Label();
                this.ilLarge = new System.Windows.Forms.ImageList();
                this.ilSmall = new System.Windows.Forms.ImageList();
                this.lwFilesAndFolders = new System.Windows.Forms.ListView();
                this.SuspendLayout();
     
                this.lblCurrentPath.Location = new System.Drawing.Point(16, 8);
                this.lblCurrentPath.Name = "lblCurrentPath";
                this.lblCurrentPath.Size = new System.Drawing.Size(528, 16);
                this.lblCurrentPath.TabIndex = 3;
                //
                // ilLarge
                //
                this.ilLarge.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
                this.ilLarge.ImageSize = new System.Drawing.Size(32, 32);
                //      this.ilLarge.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("ilLarge.ImageStream")));
                this.ilLarge.TransparentColor = System.Drawing.Color.Transparent;
                //
                // ilSmall
                //
                this.ilSmall.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
                this.ilSmall.ImageSize = new System.Drawing.Size(16, 16);
                //      this.ilSmall.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("ilSmall.ImageStream")));
                this.ilSmall.TransparentColor = System.Drawing.Color.Transparent;
                //
                // lwFilesAndFolders
                //
                this.lwFilesAndFolders.LargeImageList = this.ilLarge;
                this.lwFilesAndFolders.Location = new System.Drawing.Point(16, 32);
                this.lwFilesAndFolders.MultiSelect = false;
                this.lwFilesAndFolders.Name = "lwFilesAndFolders";
                this.lwFilesAndFolders.Size = new System.Drawing.Size(400, 216);
                this.lwFilesAndFolders.SmallImageList = this.ilSmall;
                this.lwFilesAndFolders.TabIndex = 0;
                this.lwFilesAndFolders.View = System.Windows.Forms.View.List;
                //
                // Form1
                //
                this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                this.ClientSize = new System.Drawing.Size(552, 293);
                this.Controls.AddRange(new System.Windows.Forms.Control[] { this.lblCurrentPath, this.lwFilesAndFolders });
                this.Name = "Form1";
                this.Text = "ListView";
                this.ResumeLayout(false);
     
            }

      Modified On Apr-06-2018 04:19:25 AM
  1. Post:15

    Points:105
    Re: C# winform How to Add Color in ListBox

    thanks Kamlakar,


    But this is not my require,i want add like draw fill rectangle list add in listbox

    rgds

  1. Post:106

    Points:748
    Re: C# winform How to Add Color in ListBox

    Please explain your requirement....and paste your code here...
    Let us see your code.

  1. Post:15

    Points:105
    Re: C# winform How to Add Color in ListBox

    Hi John Smith


    i dont know how to implimented.actually iam new
    so plz help the code.how to user add color to listbox.give us any example which is nearest to my requirement or help us same what i need.

    thanks

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!