ColorDialog in C#

In this tutorial, I will teach you how to use a ColorDialog in C#. This method has the ability to select a color in a textbox. It shows a form with different colors that you can select to change your text and background color. See the procedure below to see how it works.

Creating Application

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application for c#. ps1

Step 2

Add a Textbox, button and a colordialog into the form. Do the form just like shown below. ps2

Step 3

Press F7 to open the code editor. In the code editor, create a method for changing the colors of your text and background inside the textbox.
  1.  
  2. private void change_forecolor()
  3. {
  4. // 'SET THE COLORS THAT YOU HAVE SELECTED TO MATCH
  5. // 'THE CURRENTLY COLORS THAT WAS USED IN THE RICHTEXTBOX.
  6. colorDialog1.Color = richTextBox1.ForeColor;
  7. // 'IT ALLOWS YOU TO CREATE CUSTOM COLORS.
  8. colorDialog1.AllowFullOpen = true;
  9.  
  10. // 'THE BASIC COLORS WILL DISPLAY.
  11. colorDialog1.AnyColor = true;
  12.  
  13. // 'THE DIALOG BOX WILL DISPLAY WITH THE CUSTOM COLOR SETTINGS, SIDE OPEN, AS WELL.
  14. colorDialog1.FullOpen = false;
  15.  
  16. // 'SOLID COLORS ARE ALLOWED.
  17. colorDialog1.SolidColorOnly = true;
  18.  
  19. if (colorDialog1.ShowDialog() == DialogResult.OK)
  20. {
  21. richTextBox1.ForeColor = colorDialog1.Color;
  22. }
  23.  
  24. //'RESET ALL THE COLORS IN THE DIALOG BOX.
  25. colorDialog1.Reset();
  26.  
  27.  
  28. }
  29.  
  30. private void background_color()
  31. {
  32. // 'SET THE COLORS THAT YOU HAVE SELECTED TO MATCH
  33. // 'THE CURRENTLY COLORS THAT WAS USED IN THE RICHTEXTBOX.
  34. colorDialog1.Color = richTextBox1.BackColor;
  35.  
  36. // 'IT ALLOWS YOU TO CREATE CUSTOM COLORS.
  37. colorDialog1.AllowFullOpen = true;
  38.  
  39. // 'THE BASIC COLORS WILL DISPLAY.
  40. colorDialog1.AnyColor = true;
  41. // 'THE DIALOG BOX WILL DISPLAY WITH THE CUSTOM COLOR SETTINGS, SIDE OPEN, AS WELL.
  42. colorDialog1.FullOpen = false;
  43. // 'SOLID COLORS ARE ALLOWED.
  44. colorDialog1.SolidColorOnly = true;
  45.  
  46. if (colorDialog1.ShowDialog() == DialogResult.OK)
  47. {
  48. richTextBox1.BackColor = colorDialog1.Color;
  49. }
  50. // 'RESET ALL THE COLORS IN THE DIALOG BOX.
  51. colorDialog1.Reset();
  52.  
  53. }

Step 4

Write the following code to execute the method that you have created when the button is clicked.
  1.  
  2. private void button1_Click(object sender, EventArgs e)
  3. {
  4. change_forecolor();
  5. }
  6.  
  7. private void button2_Click(object sender, EventArgs e)
  8. {
  9. background_color();
  10. }
For any questions about this article. You can contact me @ Email – [email protected] Mobile No. – 09305235027 – TNT Or feel free to comment below.

Add new comment