Vertical Scrollbar color does not change
Asked Answered
B

2

0

I am a newbie in C sharp. I created a vertical scrollbar (VScrollBar). I wanted to change the color of the scrollbar's Backcolor. As it is inherited from Control, when i changed the color it does not take any effect. In the InitializeComponents()->

this.vScrollBar1 = new System.Windows.Forms.VScrollBar();
this.vScrollBar1.Location = new System.Drawing.Point(472, -41);
this.vScrollBar1.Name = "vScrollBar1";
this.vScrollBar1.Size = new System.Drawing.Size(17, 80);
this.vScrollBar1.TabIndex = 15;
this.panel1.Controls.Add(vScrollBar1);

In the Constructor->

this.vScrollBar1.BackColor= Color.Black;     //<--here is the back color property
this.Invalidate();

Any Suggestions?

Bumf answered 13/9, 2014 at 10:6 Comment(5)
Please post your codeChiaroscuro
I Added the lines after initializecompotents()-> 1.vScrollBar.BackColor=Color.Black; 2.vScrollBar.Invalidate();Bumf
In the future write the code in the question. It will be easy for us to read it and you will be not down voted. Hope my answer helps you.Demicanton
@mybirthname: Its my first post in stackoverflow. I don't know the customs. I will keep in mind your advice. Thank you. :)Bumf
If my answer is ok for you can accept it :)Demicanton
B
1

Not as simple as you might think, sorry :(

Where does the Backcolor property come from?

You have to understand that System.Windows.Forms.VScrollBar inherits from System.Windows.Forms.ScrollBar that inherits from System.Windows.Forms.Control that got a Backcolor property.

Why doesn't it work?

The System.Windows.Forms.ScrollBar is just a wrapper around the control provided by the Win32 API. Changing the Scrollbar Backcolor is not included there.

What to do?

Basicly you have to inherit from System.Windows.Forms.Control and create your own scrollbar control.

Articles: http://www.codeproject.com/Articles/41869/Custom-Drawn-Scrollbar

Yep, that is totally a reason why people prefer WPF.

Bejarano answered 16/9, 2014 at 5:10 Comment(2)
I totally agree with you that, people prefer WPF now a days. But, my question is now, if Button inherits from control and the backcolor is changeable, then why not scrollbar? And thank you for your link, i made simple scrollbar using paint on my own though.Bumf
Like any other control, you can handle the WM_CTLCOLOR message for the scrollbar control too. You can set the background color and it works - kind of. When the control draws the scrollbar components, it ignores the color set by the WM_CTLCOLOR handler and uses the system color instead.Bejarano
A
0

See my answer mentioning a custom ScrollBar control that supports custom background colors and themes https://mcmap.net/q/1015377/-change-the-background-color-of-a-vscrollbar

Antipathy answered 5/9, 2022 at 17:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.