Change Background Color using ColorDialog in C#

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. design 3. Add ColorDialog from the toolbox: design 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. output 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: outputFull 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.  
  10.  
  11.  
  12. namespace ChangeBackground_Color
  13. {
  14. public partial class Form1
  15. {
  16. public Form1()
  17. {
  18. InitializeComponent();
  19.  
  20. }
  21.  
  22.  
  23. public void Button1_Click(System.Object sender, System.EventArgs e)
  24. {
  25. ColorDialog1.ShowDialog();
  26. this.BackColor = ColorDialog1.Color;
  27. }
  28. }
  29.  
  30. }
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

Add new comment