[Back To Directory]

Using RGB() to Change Colors:

1. Open Borland C++ Builder.
2. Place a Button on the form.
3. Place 3 Edit boxes on the form in a row, make them blank with a width of 40 each.
4. Place a Panel on the form.
5. Double click Button1.
6. Type the following red code in the Button1Click function:

//This is what the Button1Click function should look like:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
   int nR = 0, nG = 0, nB = 0;

   nR = atoi(Edit1->Text.c_str());
   nG = atoi(Edit2->Text.c_str());
   nB = atoi(Edit3->Text.c_str());

   Panel1->Color = (TColor)RGB(nR, nG, nB);
}
//---------------------------------------------------------------------------


7. Run the program, type a value from 0 to 255 in each of the edit boxes. Click the button. The panel will change to the RGB value you entered. Edit1 = RED, Edit2 = GREEN and Edit3 = BLUE. There is no error trapping here so be sure that you enter only numbers.

Note: atoi() converts a char to an int and .c_str() converts an AnsiString to a char.



[Back To Directory]