We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Unable to change gradient panel colors at run time

How can you change the colors in a gradient panel at run time? I have tried the following so far with no success: gradPanel.GradientColors.SetValue(Color.Red, 0) gradPanel.GradientColors(0) = Color.Red Thanks Charles

3 Replies

DJ Davis Jebaraj Syncfusion Team March 14, 2003 09:34 PM UTC

Hi Charles, Setting the GradientColors property again with just the color you want to change works: this.gradientPanel1.GradientColors = new System.Drawing.Color[] {Color.AliceBlue , this.gradientPanel1.GradientColors[1]}; The above case has a GradientPanel with 2 colors and I am changing the first one alone. Regards, Davis

CW Charles Wolfersberger March 15, 2003 01:07 AM UTC

Thanks for the help! One more question, though. I have a gradient panel with three colors. When I change any one of the gradient colors (using the code you provided), the panel's gradient color array automatically changes back to a 2-color array/gradient. (The panel's color array is originally set to three colors through the panel's property page.) Is there a way to maintain the gradient panel's original color array legnth and manipulate the colors in such an array? Thanks Charles

CW Charles Wolfersberger March 31, 2003 06:20 PM UTC

Never mind. I brushed up on my array skills and figured it out. In VB.Net the following sets the gradient for multiple colors: Dim arrColorArray() As System.Drawing.Color ={Color.Red, Color.Green, Color.Blue, ...} GradientPanel.GradientColors() = arrColorArray If you set individual colors in the GradientPanel's GradientColor array property AND the GradientColor array holds more than two colors, then you cannot set individual colors within the GradientColor array. The reason is that any time you set a color in the GradientColor's array, the control automatically resets the array to hold two elements(colors) regardless of how many items you originally add to the GradientColor array. Thus, if you write something like this (like I did)--GradientPanel.GradientColors(2) = Color.Red--you will get an out of bounds error when writing to the array because the control automatically reset the array to hold two elements. The efficient solution to this problem seem to be creating a seperate color array (Dim arrColors(2) As Color) and then, whenever you change a color in that array, simply update the GradientColors array (GradientColors() = arrColors). Hope this is helpful! Charles

Live Chat Icon For mobile
Up arrow icon