Click here to Skip to main content
15,886,065 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
How to change the combobox's scrollbar's background color in C# of winform? (not in webform)?

Cound you give me an example? Thank all in advance!
Posted

I don't think that this can be done programatically.

Using the Windows API you can get a COMBOBOXINFO structure which will get you a handle for some parts, the edit window for example or the list, but not for the scrollbar. AFAIK you would need a handle in order to change the colour.

I have found several articles/blogs that show how to substitute a different control for the standard dropdown list so presumably you could design your own control with a scrollbar, also of your own design, and substitute it into the ComboBox.

It would be a lot of work though just to change the scrollbar colours.
 
Share this answer
 
Here is a basic control that i put together in a few minutes but you will need to make properties like private int selctedindex and tie into listbox selected index and so forth but a decent start

using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;

namespace YourNameHere
{
    public class AdvancedComboBox : Panel
    {
        private System.Windows.Forms.TextBox textBox1 = new TextBox();
        private System.Windows.Forms.Panel panel1 = new Panel();
        private System.Windows.Forms.ListBox listBox1 = new ListBox();

        public AdvancedComboBox()
        {
            InitializeComponent();
            listBox1.Visible = false;

            listBox1.Items.AddRange(new object[] { "Apple", "Pear", "Orange", "Grape", "Lemon", "Lime" });
            panel1.BackgroundImage = setButtonOff();
        }

        private void InitializeComponent()
        {
            this.Controls.AddRange(new Control[] { textBox1, panel1, listBox1 });
            this.panel1.Location = new System.Drawing.Point(128, 59);
            
            this.panel1.Size = new System.Drawing.Size(15, 20);            
            this.panel1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseDown);
            this.panel1.MouseLeave += new System.EventHandler(this.panel1_MouseLeave);
            this.panel1.MouseHover += new System.EventHandler(this.panel1_MouseHover);
            this.listBox1.FormattingEnabled = true;
            this.listBox1.Location = new System.Drawing.Point(28, 79);
            this.listBox1.Size = new System.Drawing.Size(115, 95);
            this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
            this.listBox1.MouseLeave += new System.EventHandler(this.listBox1_MouseLeave);

            this.textBox1.Location = new System.Drawing.Point(27, 59);            
            this.textBox1.Size = new System.Drawing.Size(100, 20);
            
        }

        private void listBox1_MouseLeave(object sender, EventArgs e)
        {
            listBox1.Visible = false;
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox1.Text = listBox1.SelectedItem.ToString();
            listBox1.Visible = false;
        }
        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            if (listBox1.Visible)
            { listBox1.Visible = false; }
            else { listBox1.Visible = true; }
        }
        private void panel1_MouseHover(object sender, EventArgs e)
        {
            panel1.BackgroundImage = setButtonOver();
        }
        private Image setButtonOff()
        {
            string base64String = "iVBORw0KGgoAAAANSUhEUgAAABIAAAASCAIAAADZrBkAAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAARUlEQVQokWP8//8/A4ng1duPjGRoY2BgYCJDz6g2bIAFqygjIyOmIHJUYbcNMzLRRHA6Elkdpin4/AZRjTUZjSYuamkDAD0zINGCHX3yAAAAAElFTkSuQmCC";

            byte[] base64byte = Convert.FromBase64String(base64String);
            MemoryStream ms = new MemoryStream(base64byte);
            return Image.FromStream(ms);
        }

        private void panel1_MouseLeave(object sender, EventArgs e)
        {

            panel1.BackgroundImage = setButtonOff();
        }

        private Image setButtonOver()
        {
            string base64String = "iVBORw0KGgoAAAANSUhEUgAAABIAAAASCAIAAAGuqymWAAAABGdBTUEAALGPC/xhBQAAAAZiS0dEAP8A/wD/oL2nkwAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAAd0SU1FB9sDGQA2Npg6IGUAAAB0SURBVCjPY1z54z8DAwMDAwMTAwNDOAcjAwMDI1wMxoJQjCgUHDAxoAIUeSYsKtFtQVeGbgE+y/DJQewhbCZOuQd/cdunwEySW5ABC1YXQhzBhMZHZlArXIgNazKNHDTaWHBJIAc7cgATFSThHNgVDImQBAAVrjj7k1ImuAAAAABJRU5ErkJggg==";

            byte[] base64byte = Convert.FromBase64String(base64String);
            MemoryStream ms = new MemoryStream(base64byte);
            return Image.FromStream(ms);
        }
    }
}
 
Share this answer
 
Comments
charles henington 24-Mar-11 21:42pm    
My Apologies i misread you want the actual scroll bar like in html scrollbar colorization i simply gave you a method to change the color of the drop down button thats to be clicked if you like your more than welcome to try that out and see how you like it and maybe come in handy in future again sorry about the misunderstanding

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900