Vertical Scrollbar color does not change

纵然是瞬间 提交于 2019-12-13 08:01:00

问题


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?


回答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.



来源:https://stackoverflow.com/questions/25822086/vertical-scrollbar-color-does-not-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!