Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

c# panel gradient mode

Posted on 2011-03-12
9
Medium Priority
?
1,366 Views
Last Modified: 2013-12-17
Hi ,

I have a panel whose background color can be gradient color eg. blue.
How can I do it?

Thanks,
JT
0
Comment
Question by:jtran007
9 Comments
 
LVL 6

Expert Comment

by:Webmonkey
ID: 35119259
Well, a [typical] gradient is a fade from one color to another.  Assuming that this is what you were looking for, you would need to look for a property where you can assign the secondary color (the color at the end of the gradient.  Although, I don't remember seeing that as an option for a Panel.  I've only seen this as part of the Chart controls.
0
 

Author Comment

by:jtran007
ID: 35119397
Hi,

I use one of the sample code from this website:
      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);
            }
        }
 

Open in new window


However I don't see my panel changing color in gradient mode. Could you explain why?

Thanks,
JT
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 35119554
Did you wire up the Paint() event to your handler?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 6

Expert Comment

by:Webmonkey
ID: 35119883
This is just the definition of a method.  In order for your page to use this method, you'll need to tell the appropriate event handler to call this method.

If you are using Visual Studio, open the page's design view.  Once there select the panel itself, then open the properties tab (normally a "pullout" menu on the right).  From there locate the icon at the top of the properties panel that has a lightening bolt on it - click it.  This will allow you to see the events exposed by this object, as well as any events that have been "wired up" to a specific method.  Locate the "paint" event, and add "panel1_Paint" as it's value.
0
 

Author Comment

by:jtran007
ID: 35121455
Hi,

You are right. It is working ; but in the panel I have a text box whose background color is
shown in solid color. How can I make this text box color gradient too: same as the panel?

Thanks,
JT
0
 
LVL 8

Accepted Solution

by:
dericstone earned 1000 total points
ID: 35122817
Do you really want a TextBox control with a gradient background? Because that would make it difficult for users to understand that there is a field to be changed. If you mean to have a Label control with text in it, you can set the background to gradient as follows.
public Form1()
    {
      InitializeComponent();

      this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
      this.BackColor = Color.Transparent;
    }

Open in new window

0
 
LVL 6

Expert Comment

by:Webmonkey
ID: 35153296
but in the panel I have a text box whose background color is shown in solid color. How can I make this text box color gradient too

In all fairness, that should be posted as a separate question.
0
 

Author Closing Comment

by:jtran007
ID: 35284774
Thanks,
JT
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question