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

C++ Tutorial: Change the back color of the form using Color Dialog component

copycat9999

Quantum Computing Specialist
C Rep
0
0
0
Rep
0
C Vouches
0
0
0
Vouches
0
Posts
65
Likes
118
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
In this tutorial we will learn some useful things about Color Dialog component in C++/CLI and make some practice. We will write a simple program, which will change the back color of the main form of our application. Also we will do some magic with buttons of this dialog :). So, let`s begin.

Preparations

First of all you need to start a new Windows Forms Application project and put on it control called button. Then find the Color Dialog control. It situated in the group of controls called “Dialogs” and drag it on the form. If you want you can rename your button and write something like “Change background color” or something else. Also you don’t need to change the settings of Color Dialog.

Code

Our program will change the back color of the form when we will click on the button. So here the code of the buttonClick function:
  1. private

    :

    System::

    Void

    button1_Click(

    System::

    Object

    ^

    sender, System::

    EventArgs

    ^

    e)

    {

  2. Form^

    myForm=

    this-

    >

    FindForm(

    )

    ;

  3. System::

    Windows

    ::

    Forms

    ::

    DialogResult

    dial;

  4. colorDialog1-

    >

    ShowHelp=

    true

    ;
  5. colorDialog1-

    >

    Color=

    myForm-

    >

    BackColor;
  6. dial=

    colorDialog1-

    >

    ShowDialog(

    )

    ;
  7. if

    (

    dial ==

    ::

    System

    ::

    Windows

    ::

    Forms

    ::

    DialogResult

    ::

    OK

    )
  8. {
  9. myForm-

    >

    BackColor=

    colorDialog1-

    >

    Color;
  10. }
  11. if

    (

    dial ==

    ::

    System

    ::

    Windows

    ::

    Forms

    ::

    DialogResult

    ::

    Cancel

    )
  12. {
  13. MessageBox::

    Show

    (

    "Try another time:)"

    )

    ;
  14. }
  15. }

In this code we make two main things : we get a pointer to our main form and change its color. To do first task, I used FindForm function, which returns a pointer to the form. Next I declare a variable, which will show the answer of our dialog. Then we need enable ShowHelp function of the color dialog and compare we answer of the dialog. If it is OK, we change back color of the form. And If it is Cancel, we show the message below:

untitled-2_1.png


Also we declare the starting color in our dialog in this line “colorDialog1->Color = myForm->BackColor”.

Thanks.


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

452,292

323,341

323,350

Top