C++ Tutorial: Change the Color and the Font of the Label

   In previous tutorial we have changed the background color of our main form. Today we are going to continue learning Dialog components. In this tutorial we will change the font of the label using Font dialog component and the color of the text using Color Dialog, which we used in previous tutorial. Preparations    The view of form is very simple. It consists of a label control and two button controls. First button will modify the font of the label and the other will modify the foreground color of the same label. Also add Color dialog component and Font dialog component by dragging them on form. You can post different captions on controls. In label post some text and change the text of buttons on “Change font” and “Change color” to distinguish them :) The view of my form: Capture
Code    Here you can see the code of Button Click event of the first button:
  1.         private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
  2.                                  fontDialog1->ShowHelp=true;
  3.                                  if(fontDialog1->ShowDialog()==::System::Windows::Forms::DialogResult::OK)
  4.                                  {
  5.                                          label1->Font=fontDialog1->Font;
  6.                                  }
  7.                          }
   First of all we need to enable help window of our Font Dialog component. In this help window you can change main font parameters such as font family, font size, font style, choose effects: strikeout or underline. Then you need to process the click on the button “OK”. This event we catch in the “if” block. In this block you change the font of the Label control .
  1.         private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
  2.                                  colorDialog1->ShowHelp=true;
  3.                                  if ( colorDialog1->ShowDialog() == ::System::Windows::Forms::DialogResult::OK )
  4.                                         {
  5.                                                 label1->ForeColor= colorDialog1->Color;
  6.                                         }
  7.                          }
      Just the same you need to do with Color Dialog component: you need to enable help window and process the event of clicking “OK” button to change the color of the text. If do you have any questions, you are welcome!!

Add new comment