articles

Home / DeveloperSection / Articles / Gradient Color for Form in C#

Gradient Color for Form in C#

AVADHESH PATEL 18405 20-Jul-2012

In computer graphics, a color gradient (sometimes called a color ramp or color progression) specifies a range of position-dependent colors, usually used to fill a region. For example, many window managers allow the screen background to be specified as a gradient. The colors produced by a gradient vary continuously with position, producing smooth color transitions.

There are few steps to generate gradient color for form in c#

Step 1- Take a form and drag – drop textbox and button

Gradient Color for Form in C#

Step 2 – chose first color via button ‘color 1’

Gradient Color for Form in C#

Step 3 – chose second color via button ‘color 2’

Gradient Color for Form in C#

Step 4- selected color fill into textbox

Gradient Color for Form in C#

 Step 5 – Then click on Apply button for generate gradient color for Form1

Gradient Color for Form in C#

Step 6 – you can select custom color also

Gradient Color for Form in C#

Gradient Color for Form in C#

Code:

using System.Drawing.Drawing2D;

using System.Windows.Forms;

using System;

using System.Collections.Generic;

using System.Linq;

 

namespace GradientColor

{

 

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();       

        }

 

        ColorDialog cd = new ColorDialog();

        System.Drawing.Color strcolor1;

        System.Drawing.Color strcolor2;

 

        public void draw(object sender, PaintEventArgs e)

        {

            try

            {

                using (LinearGradientBrush brush = new LinearGradientBrush(ClientRectangle, strcolor1, strcolor2, LinearGradientMode.Vertical))

                {

                    e.Graphics.FillRectangle(brush, ClientRectangle);

                }

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

        }

 

         public void Form1_Resize(object sender, System.EventArgs e)

        {

            Invalidate();

        }

 

         private void button1_Click(object sender, EventArgs e)

         {

             cd.ShowDialog();

             txtColor1.BackColor = cd.Color;

             strcolor1 = cd.Color;

         }

 

        private void button2_Click(object sender, EventArgs e)

        {

            cd.ShowDialog();

            txtcolor2.BackColor = cd.Color;

            strcolor2 = cd.Color;

        }

 

        private void btnApply_Click(object sender, EventArgs e)

        {

            Paint += draw;

            Invalidate();

        }

    }

}


 


Updated 14-Jul-2020
Avadhesh Kumar Patel District Project Manager - Aligarh 14 months work experience in Panchayati Raj Department Sector as District Project Manager & 12 months work experience in IT Sector as Software Engineer. :-)

Leave Comment

Comments

Liked By