• We just launched and are currently in beta. Join us as we build and grow the community.

Change Background Color using ColorDialog in C#

jasontsullins

Token Swap Specialist
J Rep
0
0
0
Rep
0
J Vouches
0
0
0
Vouches
0
Posts
71
Likes
52
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 250 XP
Today in C#, I will teach you how to create a program that changes background color using ColorDialog in C#.

Now, let's start this Color Dialog tutorial!

1. Let's start with creating a Windows Form Application for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application.

2. Next, add only one Button named Button1 and labeled it as "Change Background Color". Next, put a ColorDialog as a dialog box control in your toolbox.

colordesign_1.png


3. Add ColorDialog from the toolbox:

colortool.png


4. Now put add this code for your code module. This code is for Button1_Click and this will trigger to open the colordialog and after choosing the color, it will be the color of your background in the Form.

  1. public

    void

    Button1_Click(

    System

    .

    Object

    sender, System

    .

    EventArgs

    e)
  2. {
  3. ColorDialog1.

    ShowDialog

    (

    )

    ;
  4. this

    .

    BackColor

    =

    ColorDialog1.

    Color

    ;
  5. }

ColorDialog1.

ShowDialog

(

)

- is our syntax for opening the color dialog box. It will look like this one when you click the button.

colorout1.png


When picking or choosing a color from the color dialog box, the background color of the from will change as we have the code Me.

BackColor

=

ColorDialog1.

Color

. This means that the value of the color you choose in you colordialog will be equal to the background color of your form.

Now, click the Ok from your ColorDialog. It will change the back color of your form like this:

colorout2.png

Full source code:
  1. using

    System.Diagnostics

    ;
  2. using

    System

    ;
  3. using

    System.Windows.Forms

    ;
  4. using

    System.Collections

    ;
  5. using

    System.Drawing

    ;
  6. using

    Microsoft.VisualBasic

    ;
  7. using

    System.Data

    ;
  8. using

    System.Collections.Generic

    ;



  9. namespace

    ChangeBackground_Color
  10. {
  11. public

    partial

    class

    Form1
  12. {
  13. public

    Form1(

    )
  14. {
  15. InitializeComponent(

    )

    ;

  16. }


  17. public

    void

    Button1_Click(

    System

    .

    Object

    sender, System

    .

    EventArgs

    e)
  18. {
  19. ColorDialog1.

    ShowDialog

    (

    )

    ;
  20. this

    .

    BackColor

    =

    ColorDialog1.

    Color

    ;
  21. }
  22. }

  23. }

Press F5 to run the program.

Best Regards,

Engr. Lyndon Bermoy

IT Instructor/System Developer/Android Developer/Freelance Programmer

If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]

Add and Follow me on Facebook: https://www.facebook.com/donzzsky

Visit and like my page on Facebook at: https://www.facebook.com/BermzISware


Download
You must upgrade your account or reply in the thread to view hidden text.
 

442,401

317,942

317,951

Top