StringGrid的单元格编辑开始,单元格编辑状态结束
StringGrid 表格和其他的三方的表格中都没有单元格编辑开始,单元格编辑状态结束的事件;
因此在编程时非常不方便,我增加了这两个事件,并请请大家帮忙检查一下,另外当表格失去焦点时
编辑结束没有加上必须在程序中处理,希望高手增加上,谢了!
组件代码片段:
<jhStringGrid.h>文件
class PACKAGE TjhStringGrid : public TStringGrid
{
typedef TStringGrid Inherit; //方便直接调用祖先方法常加保存祖先类型的变量
typedef void __fastcall (__closure *celldatachanged)(TObject *Sender,int ACol,int ARow,int state); //定义事件的参数
private:
bool editing ;
celldatachanged oncelleditstart; //单元格进入编辑状态;
celldatachanged oncelleditend; //单元格编辑状态结束
protected:
DYNAMIC void __fastcall MouseDown(Controls::
TMouseButton Button, Classes::TShiftState Shift, int X, int Y); //重载鼠标按下动态函数
virtual bool __fastcall SelectCell(int ACol, int ARow); //重载选中单元格虚函数;
DYNAMIC void __fastcall KeyPress(char &Key); //重载键按下动态函数;
public:
__published:
__property celldatachanged OnCellEditStart={read=oncelleditstart,write=oncelleditstart}; //单元格进入编辑状态;
__property celldatachanged OnCellEditEnd={read=oncelleditend,write=oncelleditend}; //单元格编辑状态结束
};
//---------------------------------------------------------------------------
#endif
<jhstringGrid.cpp>文件
static inline void ValidCtrCheck(TjhStringGrid *)
{
new TjhStringGrid(NULL);
}
//---------------------------------------------------------------------------
__fastcall TjhStringGrid::TjhStringGrid(TComponent* Owner) :TStringGrid(Owner)
{
editing=false; //初始是没有进入编辑状态
}
//------------------------------------------------------------------------------------------------
void __fastcall TjhStringGrid::MouseDown(Controls::TMouseButton Button, Classes::TShiftState Shift, int X, int Y)
{
Inherit::MouseDown(Button, Shift,x, y) ;
if( this->EditorMode && !this->editing) //当鼠标按下,表格允许编辑同时表格处于非编辑态时发出开始编辑事件
{
editing=true;
if (oncelleditstart)
oncelleditstart(this,this->Col,this->Row,0);
}
else
editing=false;
}
//------------------------------------------------------------------------------
void __fastcall TjhStringGrid::KeyPress(char &Key)
{
char Akey=Key;
Inherit::KeyPress(Key);
// 编辑开始
if(this->EditorMode&&!editing ) //当有键按下,表格允许编辑同时表格处于非编辑态时发出开始编辑事件
{
editing=true;
if (oncelleditstart)
oncelleditstart(this,this->Col,this->Row,0);
return;
}
//编辑结束
if(Akey==13&& editing) //当是回车键同时表格处于编辑状态时,发出编辑结束事件
{ editing=false;
if(oncelleditend)
oncelleditend(this,this->Col,this->Row,0);
}
}
//--------------------------------------------------
bool __fastcall TjhStringGrid::SelectCell(int ACol, int ARow)
{
bool f=Inherit::SelectCell(ACol,ARow);
if(this->EditorMode&&editing) //当表格处于编辑状态,选择了其他单元格发出编辑结束事件
{
editing=false;
if(oncelleditend)
this->oncelleditend(this,this->Col,this->Row,0);
}
editing=false;
return f;
}