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

ColorDialog in C#

torqman

Guild Leader
T Rep
0
0
0
Rep
0
T Vouches
0
0
0
Vouches
0
Posts
126
Likes
197
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
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#.
creating_application_c_3.png

Step 2

Add a Textbox, button and a colordialog into the form. Do the form just like shown below.
2019-05-22_3.png

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. private

    void

    change_forecolor(

    )
  2. {
  3. // 'SET THE COLORS THAT YOU HAVE SELECTED TO MATCH
  4. // 'THE CURRENTLY COLORS THAT WAS USED IN THE RICHTEXTBOX.
  5. colorDialog1.

    Color

    =

    richTextBox1.

    ForeColor

    ;
  6. // 'IT ALLOWS YOU TO CREATE CUSTOM COLORS.
  7. colorDialog1.

    AllowFullOpen

    =

    true

    ;

  8. // 'THE BASIC COLORS WILL DISPLAY.
  9. colorDialog1.

    AnyColor

    =

    true

    ;

  10. // 'THE DIALOG BOX WILL DISPLAY WITH THE CUSTOM COLOR SETTINGS, SIDE OPEN, AS WELL.
  11. colorDialog1.

    FullOpen

    =

    false

    ;

  12. // 'SOLID COLORS ARE ALLOWED.
  13. colorDialog1.

    SolidColorOnly

    =

    true

    ;

  14. if

    (

    colorDialog1.

    ShowDialog

    (

    )

    ==

    DialogResult.

    OK

    )
  15. {
  16. richTextBox1.

    ForeColor

    =

    colorDialog1.

    Color

    ;
  17. }

  18. //'RESET ALL THE COLORS IN THE DIALOG BOX.
  19. colorDialog1.

    Reset

    (

    )

    ;


  20. }

  21. private

    void

    background_color(

    )
  22. {
  23. // 'SET THE COLORS THAT YOU HAVE SELECTED TO MATCH
  24. // 'THE CURRENTLY COLORS THAT WAS USED IN THE RICHTEXTBOX.
  25. colorDialog1.

    Color

    =

    richTextBox1.

    BackColor

    ;

  26. // 'IT ALLOWS YOU TO CREATE CUSTOM COLORS.
  27. colorDialog1.

    AllowFullOpen

    =

    true

    ;

  28. // 'THE BASIC COLORS WILL DISPLAY.
  29. colorDialog1.

    AnyColor

    =

    true

    ;
  30. // 'THE DIALOG BOX WILL DISPLAY WITH THE CUSTOM COLOR SETTINGS, SIDE OPEN, AS WELL.
  31. colorDialog1.

    FullOpen

    =

    false

    ;
  32. // 'SOLID COLORS ARE ALLOWED.
  33. colorDialog1.

    SolidColorOnly

    =

    true

    ;

  34. if

    (

    colorDialog1.

    ShowDialog

    (

    )

    ==

    DialogResult.

    OK

    )
  35. {
  36. richTextBox1.

    BackColor

    =

    colorDialog1.

    Color

    ;
  37. }
  38. // 'RESET ALL THE COLORS IN THE DIALOG BOX.
  39. colorDialog1.

    Reset

    (

    )

    ;

  40. }

Step 4

Write the following code to execute the method that you have created when the button is clicked.

  1. private

    void

    button1_Click(

    object

    sender, EventArgs e)
  2. {
  3. change_forecolor(

    )

    ;
  4. }

  5. private

    void

    button2_Click(

    object

    sender, EventArgs e)
  6. {
  7. background_color(

    )

    ;
  8. }

For any questions about this article. You can contact me @
Email – [email protected]
Mobile No. – 09305235027 – TNT
Or feel free to comment below.

 

449,093

322,160

322,169

Top