C++Builder中的一些公共函数

 

//C++Builder中的一些公共函数

//根据SQL语句返回记录集

int __fastcall TGlobalFun::GetRecordCount(AnsiString sql)

{

     int ret=0;

      ptquery->Active = false;

     ptquery->SQL->Text =  sql;

     try

     {

              ptquery->Open();

              ret= ptquery->RecordCount;

     }catch (Exception &e)

     {

              Application->ShowException(&e);

              ret = 0;

     }

      ptquery->Active = false;

     return ret;

}

 

//将表中的数据填充到下拉列表框中

 

void __fastcall TGlobalFun::FillComBox(TComboBox *cmb,AnsiString tableName,AnsiString colName)

{

     if(tableName == "" || colName == "") return;

     AnsiString sql;

     sql = "select distinct  "+colName+"  as col from "+tableName;

     ptquery->Active = false;

     ptquery->SQL->Text =  sql;

     ptquery->Open();

     if(ptquery->RecordCount<1) return;

     //Ìî³ä

     cmb->Clear();

     cmb->Items->Add("");

     ptquery->RecNo = 1;

     do

     {

       if(ptquery->FieldByName("col")->AsString != "")

        cmb->Items->Add(ptquery->FieldByName("col")->AsString);

        ptquery->Next();

     }

     while(!ptquery->Eof);

     ptquery->Active = false;

   return;

}

 

//应用举例

//调用全局函数FillComBoxcmbSafeItem为下拉列表框的name , P_Safety为表名,SafeItem / /为列名

g_Global.FillComBox(cmbSafeItem,"P_Safety","SafeItem");

 

对于下拉列表框还有个属性stytle:设置下拉列表的样式,比如只能选择等

 

//检查数据的合法性:

 

//控制txtCarId控件只能输入数字和退格 需要引进#include <Qt.hpp>头文件

void __fastcall TFrm_Permit::txtCarIdKeyPress(TObject *Sender, char &Key)

{

   if(( Key >= Key_0)&&(Key <= Key_9) || (Key =='\b'))

   {

   }

   else

   {

       Key = Key_Escape;

   }

}

 

//控制txtBuyMoney控件只能输入数字和退格和小数点

void __fastcall TFrm_Permit::txtBuyMoneyKeyPress(TObject *Sender,

      char &Key)

{

   if (( Key >= Key_0)&&(Key <= Key_9) || (Key == '.') ||(Key =='\b' ))

   {

   }

   else

   {

       Key = Key_Escape;

   }

}

 

//函数CheckEmpty

bool    __fastcall TGlobalFun::CheckEmpty(TWinControl *com,AnsiString str,const int param)

 {

    bool ret=true;

    AnsiString value="";

    int len=0;

    //获取控件中要检验的值

 

    if(AnsiString(com->ClassName())== "TEdit")

     {

        TEdit *pt = (TEdit *)com;

        value = pt->Text;

        len   = pt->MaxLength/2;

     } else

     if(AnsiString(com->ClassName())== "TComboBox")

     {

       TComboBox *cb = (TComboBox *)com;

        value = cb->Text;

        len   = cb->MaxLength/2;

     }

      //检查是否为空

       if ((param & _EMPTY) == 0x01){

            if(value == "")

            {

                MessageBox(NULL,(str+"不能为空!").c_str(),"提示",MB_OK|MB_ICONERROR);

               if(AnsiString(com->Parent->ClassName()) != "TForm")

                  com->Parent->Show();

                com->SetFocus();

                return false;

            }

        }

        if(value == "") return true;

        //检查是否是数字

        if ((param & _NUM) == 0x02)

        {

            try{

                 StrToInt(value);

            }catch(...)

            {

                MessageBox(NULL,(str+"请用数字输入!").c_str(),"提示",MB_OK|MB_ICONERROR);

                if(AnsiString(com->Parent->ClassName()) != "TForm")

                  com->Parent->Show();

                com->SetFocus();

                return false;

            }

        }

        //检查是否float类型

        if ((param & _FLOAT) == 0x04)

        {

            try{

                 StrToFloat(value);

            }catch(...)

            {

                MessageBox(NULL,(str+"输入不正确!").c_str(),"提示",MB_OK|MB_ICONERROR);

                if(AnsiString(com->Parent->ClassName()) != "TForm")

                  com->Parent->Show();

                com->SetFocus();

                return false;

            }

        }

        //检查数据是否为Date类型

        if ((param & _DATE) == 0x08)

        {

            try{

                 StrToDate(value);

            }catch(...)

            {

                MessageBox(NULL,(str+"请用正确的日期格式输入!").c_str(),"提示",MB_OK|MB_ICONERROR);

                if(AnsiString(com->Parent->ClassName()) != "TForm")

                  com->Parent->Show();

                com->SetFocus();

                return false;

            }

        }

         //检查长度是否合法,根据控件的maxlength

        if ((param & _LENGTH) == 0x10)

        {

            if(WideString(value).Length() > len)

            {

                MessageBox(NULL,(str+"输入长度不合法!").c_str(),"提示",MB_OK|MB_ICONERROR);

                if(AnsiString(com->Parent->ClassName()) != "TForm")

                  com->Parent->Show();

                com->SetFocus();

                return false;

            }

        }

    return ret;

 }

 

//应用举例

    if(g_Global.CheckEmpty(txtItemMoney,"±£ÏÕ½ð¶î",_EMPTY)==false) return;

if(g_Global.CheckEmpty(txtBeginTime,"ÆðʼÈÕÆÚ",_DATE)==false) return;

 

//获得TableName表中FieldsName字段的新编号

int     __fastcall TGlobalFun::GetNewID(AnsiString TableName,AnsiString FieldsName)

{

    AnsiString  strSQL;

    int         mNewID = 0,mID;

 

    strSQL = "select " + FieldsName + " from " + TableName +" where DepID = '" + g_Global.g_SysInfor.DepID + "' order by " + FieldsName + " Asc";

    ptquery->Active     = false;

    ptquery->SQL->Text  =  strSQL;

    try

    {

          ptquery->Open();

          if(ptquery->RecordCount>0)

          {

                ptquery->RecNo = 1;

                mNewID = 0;

                do

                {

                    mID = ptquery->FieldByName(FieldsName)->AsInteger;

                    if (mNewID != mID)

                        break;

                    mNewID++;

                    ptquery->Next();

                }

                while(!ptquery->Eof);

          }

    }

    catch(...)

    {;}

   ptquery->Active = false;

   return mNewID;

}

 

posted on 2005-08-30 16:45  痛苦并快乐着  阅读(1263)  评论(0编辑  收藏  举报

导航