Question : Draw gradient on panel control

Hi guys,

I need some sample code for painting a gradient onto a standard Windows Forms panel control (Visual Studio 2005). If possible, I want to avoid creating a custom panel control. I've read those posts on the internet and I'm really just looking for the minimal code to create a gradient on a particular panel control on a particular form.

Can anyone help me with this?

Thanks,
Kevin McElhiney

Answer : Draw gradient on panel control

You actually can...but you should be using the Graphics supplied FOR YOU in the Paint() event via "e.Graphics".
(see code below)

Then, you just set the BackColor of your GroupBoxes to Transparent (from code at run-time or at design-time in the IDE):
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
        private void Form1_Load(object sender, EventArgs e)
        {
            groupBox1.BackColor = Color.Transparent;
            groupBox2.BackColor = Color.Transparent;
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            Control control = (Control)sender;
            Graphics g = e.Graphics;
            g.Clear(Color.White);
            Color startColor = Color.FromArgb(106, 127, 156);
            Color endColor = Color.FromArgb(177, 197, 218);
            using (LinearGradientBrush darkBrush = new LinearGradientBrush(control.ClientRectangle, startColor, endColor, LinearGradientMode.Vertical))
            {
                g.FillRectangle(darkBrush, control.ClientRectangle);
            }
        }
 
Gradient Background visible thru the GroupBoxes
Gradient Background visible thru the GroupBoxes
 
Random Solutions  
 
programming4us programming4us