Combo Box in a String Grid


Setup

  1. Start Borland C++ Builder with the default form
  2. Add a StringGrid control and add a combo box to the string grid. You can position it anywhere
     
  3. On the form, click the combo box and, on the Object Inspector, double-click the value side of the Items property.
  4. Enter Teen, Adult, Senior, each on its own line, and click OK.
  5. Implement the OnCreate event of the form, the OnClick event of the StringGrid, and the OnChange event of the combo box, as follows:
     
    //---------------------------------------------------------------------------
    #include <vcl.h>
    #pragma hdrstop
    
    #include "Unit1.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm1 *Form1;
    //---------------------------------------------------------------------------
    __fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
    {
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::FormCreate(TObject *Sender)
    {
        StringGrid1->Cells[0][0] = "First Name";
        StringGrid1->Cells[1][0] = "Last Name";
        StringGrid1->Cells[2][0] = "Gender";
        StringGrid1->Cells[3][0] = "Mbr Lvl";
        StringGrid1->Cells[4][0] = "Email Address";
        StringGrid1->Cells[5][0] = "Home Phone";
        StringGrid1->Cells[6][0] = "Cell Phone";
        StringGrid1->Cells[7][0] = "Fee";    
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::StringGrid1Click(TObject *Sender)
    {
        if( StringGrid1->Col == 3 )
        {
            TRect Recto     = StringGrid1->CellRect(StringGrid1->Col, StringGrid1->Row);
            ComboBox1->Top  = StringGrid1->Top;
            ComboBox1->Left = StringGrid1->Left;
            ComboBox1->Top  = ComboBox1->Top + Recto.Top + StringGrid1->GridLineWidth;
            ComboBox1->Left = ComboBox1->Left + Recto.Left + StringGrid1->GridLineWidth + 1;
            ComboBox1->Height = (Recto.Bottom - Recto.Top) + 1;
            ComboBox1->Width  = Recto.Right - Recto.Left;
            ComboBox1->Visible = True;
        }
        else
            ComboBox1->Visible = False;
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::ComboBox1Change(TObject *Sender)
    {
        StringGrid1->Cells[StringGrid1->Col][StringGrid1->Row] =
         ComboBox1->Items->Strings[ComboBox1->ItemIndex];
    }
    //---------------------------------------------------------------------------
  6. Test the application

 

 


Copyright © 2003-2007 FunctionX, Inc.