Home > DeveloperSection > Forums > C# winform How to Add Color in ListBox
Tech Sys
Tech Sys

Total Post:15

Points:105
Posted on    October-13-2014 1:15 AM

 .NET C# 
Ratings:


 4 Reply(s)
 1248  View(s)
Rate this:
hi,

could anybody help the code for listbox color add (like add fill rectangle with name).
plz ref below image


thanks in advance


Kamlakar Singh
Kamlakar Singh

Total Post:194

Points:1396
Posted on    October-13-2014 5:31 AM

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

 

        }


Tech Sys
Tech Sys

Total Post:15

Points:105
Posted on    October-13-2014 6:05 AM

thanks Kamlakar,

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

rgds


John Smith

Total Post:102

Points:720
Posted on    October-13-2014 7:25 AM

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

Tech Sys
Tech Sys

Total Post:15

Points:105
Posted on    November-05-2014 4:38 AM

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


Don't want to miss updates? Please click the below button!

Follow MindStick