Board index » cppbuilder » TStringGrid ?

TStringGrid ?


2005-10-10 05:27:59 AM
cppbuilder100
Hello,
How can use TStringGrid like ValuListEditor that have a pick up list ?
Best Reagrds,
m.t
 
 

Re:TStringGrid ?

"Sasan" < XXXX@XXXXX.COM >wrote:
Quote

How can use TStringGrid like ValuListEditor that have a pick
up list ?
You need to replace the TStringGrid's TInplaceEdit with a
subclassed version of TInplaceEdit that supports a drop
down list.
To do this, you'll need to subclass the TStringGrid as well so
that you can override the TStringGrid's CreateEditor method so
that you can make the grid use your subclassed version of
TInplaceEdit.
BCB6 has a TInplaceEditList which will do what you need. It's
a bit weak but it works. The only thing is that you'll need to
do some work with it if you have a drop down list that might
exceed the screen boundries.
If you don't have BCB6, search the archives for it. You can
find the complete source for TInplaceEditList there. The
following is untested so if I missed something that you can't
figure out, just ask.
TMyGrid *pGrid = NULL;
//-------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
pGrid = new TThemedGrid( this );
pGrid->Parent = this;
pGrid->Left = 10;
pGrid->Top = 10;
pGrid->OnGetEditStyle = GetEditStyle;
pGrid->OnGridButtonClick = GridButtonClick;
pGrid->OnGetDropDownItems = GetDropDownItems;
}
//-------------------------------------------------------------
__fastcall TForm1::~TForm1()
{
//
}
//-------------------------------------------------------------
TEditStyle __fastcall TForm1::GetEditStyle(TObject *Sender, int ACol, int ARow )
{
switch( ACol )
{
case 1: return esEllipsis;
case 2: return esPickList;
default: return tesSimple;
}
}
//-------------------------------------------------------------
void __fastcall TForm1::GridButtonClick(TObject *Sender, int ACol, int ARow)
{
ShowMessage( "Grid Button Click" );
}
//-------------------------------------------------------------
void __fastcall TForm1::GetDropDownItems(TObject *Sender, int ACol, int ARow, TStrings *pList )
{
pList->Clear();
pList->Add( "Test of a very wide line to exceed the cell's width" );
for( int x = 2; x < 41; ++x ) pList->Add( "Line " + IntToStr(x) );
}
//-------------------------------------------------------------
//-------------------------------------------------------------
#ifndef MyGridH
#define MyGridH
//-------------------------------------------------------------
#include <Classes.hpp>
#include <Grids.hpp>
//-------------------------------------------------------------
typedef TEditStyle __fastcall (__closure *TOnGetEditStyle)(TObject*, int, int );
typedef void __fastcall (__closure *TOnGridButtonClick)(TObject*, int, int);
typedef void __fastcall (__closure *TOnGetDropDownItems)(TObject *Sender, int, int, TStrings*);
//-------------------------------------------------------------
class TMyGrid : public TStringGrid
{
__published:
void __fastcall EditButtonClick( int ACol, int ARow );
void __fastcall GetPickListItems( int ACol, int ARow, TStrings *pStrings );
protected:
TInplaceEdit* __fastcall CreateEditor();
private:
TOnGetEditStyle FOnGetEditStyle;
TOnGridButtonClick FOnGridButtonClick;
TOnGetDropDownItems FOnGetDropDownItems;
public:
__fastcall TMyGrid( TComponent* Owner );
__fastcall ~TMyGrid();
__property TOnGetEditStyle OnGetEditStyle = { read = FOnGetEditStyle, write = FOnGetEditStyle };
__property TOnGridButtonClick OnGridButtonClick = { read = FOnGridButtonClick, write = FOnGridButtonClick };
__property TOnGetDropDownItems OnGetDropDownItems = { read = FOnGetDropDownItems, write = FOnGetDropDownItems };
};
//-------------------------------------------------------------
#endif
//-------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "MyGrid.h"
#pragma package(smart_init)
//-------------------------------------------------------------
__fastcall TMyGrid::TMyGrid(TComponent* Owner) : TStringGrid( Owner )
{
// set the parent to anything while testing or dynamically allocating to
// avoid AV 'no parent'
// Otherwise remove
Parent = dynamic_cast<TWinControl*>( Owner );
}
//-------------------------------------------------------------
__fastcall TMyGrid::~TMyGrid()
{
//
}
//-------------------------------------------------------------
TInplaceEdit* __fastcall TMyGrid::CreateEditor()
{
TInplaceEditList *pInplaceEditList = new TInplaceEditList( this );
pInplaceEditList->OnEditButtonClick = EditButtonClick;
pInplaceEditList->OnGetPickListItems = GetPickListItems;
return pInplaceEditList;
}
//-------------------------------------------------------------
TEditStyle __fastcall TMyGrid::GetEditStyle( int ACol, int ARow )
{
if( FOnGetEditStyle ) return FOnGetEditStyle( this, ACol, ARow );
else return esSimple;
}
//-------------------------------------------------------------
void __fastcall TMyGrid::EditButtonClick( int ACol, int ARow )
{
if( FOnEditButtonClick ) FOnEditButtonClick( this, ACol, ARow );
}
//-------------------------------------------------------------
void __fastcall TMyGrid::GetPickListItems( int ACol, int ARow, TStrings *pStrings )
{
if( FOnGetDropDownItems ) FOnGetDropDownItems( this, ACol, ARow, pStrings );
}
//-------------------------------------------------------------
~ JD