关闭

c++ builder 中实现listview 里面 combobox,edit,datatimerpiker可以编辑

标签: listviewc++pairfunctionnullexception
2120人阅读 评论(0) 收藏 举报
 
 
5月22日
c++ builder 中实现listview 里面 combobox,edit,datatimerpiker可以编辑
最近由于项目需要,自己封装了一个类,实现了listview 里面的combobox,edit,datetimerpiker可以
编辑的实现.如下
//---------------------------------------------------------------------------
#ifndef class_listviewH
#define class_listviewH
//---------------------------------------------------------------------------
#endif
 
#include <ComCtrls.hpp>
#include <StdCtrls.hpp>
#include <Classes.hpp>
#include <Controls.hpp>
class    listview_function
{
        public:
                TListView *ListView1;   //listview的对象
                TForm * Form1;  //主要是用于activecontrol

                TListItem     *Item; //用户选择的第几行的item
                int  ncol_number;       //listview的总列数目
                int  ncol_dbclick;      //双击的时候所在的列
                int  ncol_modify;       //用来记录是否修改的列
                int  ncol_dbclick_notuse[10]; // 用来记录 双击的时候所在的列,这些列没有反应的
                int  ncol_dbclick_notuse_number;//数目
 
                typedef struct
                {
                TComboBox *ComboBox1;
                TEdit *Edit1;
                TDateTimePicker *DateTimePicker1;
                int ncol; //列
                }paris;       //一个配对
                paris pair[20];
                int  paris_number; //配对数目

                int    nWidth;
                TRect  Rect;
                TPoint Pos;  //右键双击的变量

                void  init_listview(TListView *ListView,TForm * Form);   //初始化
                void  set_modify_ncol(int ncol);//设置修改的列
                void  set_ncol_unvisable(int ncol);//设置某一列不可见
                void  set_ncol_dbclick_notuse(int ncol); //设置某一列双击的时候不反映

                void   right_dbclick(); // 双击的函数
                int  is_dbclick_is_notuse(int ncol);
                void  set_pairs(TComboBox *ComboBox1,int col);      //设置关联关系
                void  set_pairs(TEdit *Edit1,int col);
                void  set_pairs(TDateTimePicker *DateTimePicker1,int col);
                void exit_paris();  //退出的时候
                void exit_Combox(TComboBox *ComboBox1);
                void exit_TEdit(TEdit *Edit1);
                void exit_TDateTimePicker(TDateTimePicker *DateTimePicker1);
                void set_activecontrol_ComboBox(TComboBox *ComboBox1);
               
                void set_activecontrol_TEdit(TEdit *Edit1);
                void set_activecontrol_TDateTimePicker(TDateTimePicker *DateTimePicker1);
               
                void ontimer();
                void set_paris_activecontrol_unvisable();
        private:
                void __fastcall TheTimerOnTimer(TObject *Sender);

};
/*
使用方法
定义一个类的变量   listview_function    a
1:初始化 listview和form
   init_listview(TListView *ListView,TForm * Form);
2:设置列的关联
        void  set_ComboBox_pairs(TComboBox *ComboBox1,int col);      //设置关联关系
        void  set_TEdit_pairs(TEdit *Edit1,int col);
        void  set_TDateTimePicker_pairs(TDateTimePicker *DateTimePicker1,int col);
3:可设置某一些列双击不反映   或不可以看见
      void  set_ncol_dbclick_notuse(int ncol); //设置某一列双击的时候不反映
      void  set_ncol_unvisable(int ncol);//设置某一列不可见
4:在listview的rdouble事件里面
    void   right_dbclick(); // 双击的函数

5:在控件的退出里面
     void exit_ComboBox_TEdit_TDateTimePicker();  //退出的时候

6:去除 拖动的时候,还在园地的
  void   ontimer()

*/
 
 
//---------------------------------------------------------------------------

#pragma hdrstop
#include "class_listview.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)

void  listview_function::init_listview(TListView *ListView,TForm * Form)
{
        ListView1=ListView;
        Form1=Form;
        ncol_number=ListView1->Columns->Count;
        ncol_dbclick=-1;
        ncol_dbclick_notuse_number=0;
        paris_number=0;
}
void  listview_function::set_modify_ncol(int ncol)//设置修改的列
{
        ncol_modify=ncol;
}
void  listview_function::set_ncol_unvisable(int ncol)
{
        ListView1->Column[ncol]->Width=0;
}
void  listview_function::set_ncol_dbclick_notuse(int ncol)
{
          ncol_dbclick_notuse_number++;
          ncol_dbclick_notuse[ncol_dbclick_notuse_number]=ncol;
}
void  listview_function::right_dbclick()  // 双击的函数
{
       // TListItem     *Item1=NULL; //用户选择的第几行的item

        int    nX;
        int    nCol;
        Pos  = ListView1->ScreenToClient(Mouse->CursorPos);
        Item = ListView1->GetItemAt(Pos.x, Pos.y);
        if(Item==NULL)
           return ;
 
        nX = -GetScrollPos(ListView1->Handle, SB_HORZ);
        for(nCol=0; nCol<ncol_number; nX+=nWidth, nCol++)
        {
                nWidth = ListView_GetColumnWidth(ListView1->Handle, nCol);
                        if (Pos.x <= nX + nWidth)
                         break;
        }
        ncol_dbclick=nCol;
        if (is_dbclick_is_notuse(ncol_dbclick)||ncol_dbclick==ncol_number)
                return ;

        if (nX < 0)
        {
                nWidth += nX;
                nX = 0;
         }
        Rect = Item->DisplayRect(drBounds);
        Pos.x = nX;
        Pos.y = Rect.Top;
        MapWindowPoints(ListView1->Handle, Form1->Handle, (POINT*)&Pos, 1);  //是窗体的handle么

         for(int i=1;i<=paris_number;i++)
        if(pair[i].ncol==ncol_dbclick)
            {
              if(pair[i].ComboBox1!=NULL)
               set_activecontrol_ComboBox(pair[i].ComboBox1);
              else if(pair[i].Edit1!=NULL)
               set_activecontrol_TEdit(pair[i].Edit1);
              else if(pair[i].DateTimePicker1!=NULL)
              set_activecontrol_TDateTimePicker(pair[i].DateTimePicker1);
        }
       
}

int  listview_function::is_dbclick_is_notuse(int ncol) //判断   双击的时候所在的列 是否可以 反应
{
         for(int i=1;i<=ncol_dbclick_notuse_number;i++)
                  if(ncol_dbclick_notuse[ncol_dbclick_notuse_number]==ncol)
                       return  1;
         return 0;
}
void  listview_function::set_pairs(TComboBox *ComboBox1,int col)      //设置关联关系
{
            paris_number++; //配对数目
            int  i=paris_number;
            pair[i].ncol=col;
            pair[i].ComboBox1=ComboBox1;
            pair[i].Edit1=NULL;
            pair[i].DateTimePicker1=NULL;
            pair[i].ComboBox1->Visible=false;
}
 
void  listview_function::set_pairs(TEdit *Edit1,int col)
{
            paris_number++; //配对数目
            int  i=paris_number;
            pair[i].ncol=col;
            pair[i].ComboBox1=NULL;
            pair[i].Edit1=Edit1;
            pair[i].DateTimePicker1=NULL;
            pair[i].Edit1->Visible=false;
 
}

void  listview_function::set_pairs(TDateTimePicker *DateTimePicker1,int col)
{
            paris_number++; //配对数目
            int  i=paris_number;
            pair[i].ncol=col;
            pair[i].ComboBox1=NULL;
            pair[i].Edit1=NULL;
            pair[i].DateTimePicker1=DateTimePicker1;
            pair[i].DateTimePicker1->Visible=false;
 
}

void listview_function::exit_paris() //退出的时候
{
    for(int i=1;i<=paris_number;i++)
        if(pair[i].ncol==ncol_dbclick)
            {
              if(pair[i].ComboBox1!=NULL)
                exit_Combox(pair[i].ComboBox1);
              else if(pair[i].Edit1!=NULL)
                exit_TEdit(pair[i].Edit1);
              else if(pair[i].DateTimePicker1!=NULL)
              exit_TDateTimePicker(pair[i].DateTimePicker1);
        }
}
void  listview_function::exit_Combox(TComboBox *ComboBox1)
{
    ComboBox1->Visible = false;
    if (ncol_dbclick == 0)
        Item->Caption = ComboBox1->Text;
    else
    {
        while(Item->SubItems->Count < ncol_dbclick)
           Item->SubItems->Add("");
        Item->SubItems->Strings[ncol_dbclick-1] =ComboBox1->Text;
    }

}
void listview_function::exit_TEdit(TEdit *Edit1)
{
    Edit1->Visible = false;
    if (ncol_dbclick == 0)
        Item->Caption = Edit1->Text;
    else
    {
        while(Item->SubItems->Count < ncol_dbclick)
           Item->SubItems->Add("");
        Item->SubItems->Strings[ncol_dbclick-1] = Edit1->Text;
    }
}

void listview_function::exit_TDateTimePicker(TDateTimePicker *DateTimePicker1)
{

    DateTimePicker1->Visible = false;
    if (ncol_dbclick == 0)
        Item->Caption =DateTimePicker1->Date;
    else
    {
        while(Item->SubItems->Count < ncol_dbclick)
           Item->SubItems->Add("");
        Item->SubItems->Strings[ncol_dbclick-1] =DateTimePicker1->Date;
    }

}
void listview_function::set_activecontrol_ComboBox(TComboBox *ComboBox1)
{
                ComboBox1->Visible=true;
                Form1->ActiveControl= ComboBox1;
                ComboBox1->SetBounds(Pos.x, Pos.y-1, nWidth, Rect.Height()+2);
                ComboBox1->Text = ncol_dbclick==0 ? Item->Caption : Item->SubItems->Strings[ncol_dbclick-1];
}
void listview_function::set_activecontrol_TEdit(TEdit *Edit1)
{
                Edit1->Visible=true;
                Form1->ActiveControl= Edit1;
                Edit1->SetBounds(Pos.x, Pos.y-1, nWidth, Rect.Height()+2);
                Edit1->Text = ncol_dbclick==0 ? Item->Caption : Item->SubItems->Strings[ncol_dbclick-1];
}
void listview_function::set_activecontrol_TDateTimePicker(TDateTimePicker *DateTimePicker1)
{
        DateTimePicker1->Visible = true;
        Form1->ActiveControl  =  DateTimePicker1;
        DateTimePicker1->SetBounds(Pos.x, Pos.y-1, nWidth, Rect.Height()+2);
        try
        {
        DateTimePicker1->Date = ncol_dbclick==0 ? Item->Caption : Item->SubItems->Strings[ncol_dbclick-1];
        }
         catch (Exception &exception)
        {
        }
       
}
void __fastcall listview_function::TheTimerOnTimer(TObject *Sender)
{
        
       MSG *Msg=new MSG();
        //定义消息指针
        WaitMessage();            //等待消息
       if(PeekMessage(Msg, ListView1->Handle, 0, 0, PM_NOREMOVE))
       {
               if(Msg->wParam==1)
               {
                   set_paris_activecontrol_unvisable();
                }
     }

};

void listview_function::ontimer()
{
        TTimer *Timer1= new TTimer(Form1);
        Timer1->Interval=10;
        Timer1->OnTimer=TheTimerOnTimer;
        Timer1->Enabled = true;
}

void  listview_function::set_paris_activecontrol_unvisable()
{

        for(int i=1;i<=paris_number;i++)
      {
              if(pair[i].ComboBox1!=NULL)
                pair[i].ComboBox1->Visible=false;
              else if(pair[i].Edit1!=NULL)
               pair[i].Edit1->Visible=false;
              else if(pair[i].DateTimePicker1!=NULL)
             pair[i].DateTimePicker1->Visible=false;
        }
 

}
 
 
 
使用的例子:
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ComCtrls.hpp>
#include <Buttons.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
        TListView *ListView1;
        TComboBox *ComboBox1;
        TEdit *Edit1;
        TDateTimePicker *DateTimePicker1;

        void __fastcall ListView1DblClick(TObject *Sender);
        void __fastcall ComboBox1Exit(TObject *Sender);
        void __fastcall Edit1Exit(TObject *Sender);
        void __fastcall DateTimePicker1Exit(TObject *Sender);
        void __fastcall FormCreate(TObject *Sender);
private: // User declarations
public:  // User declarations
        __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
 
 
 
 
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "class_listview.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
listview_function  a ;  //一个对象
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ListView1DblClick(TObject *Sender)
{
           a.right_dbclick();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ComboBox1Exit(TObject *Sender)
{
                a.exit_paris();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit1Exit(TObject *Sender)
{
           a.exit_paris();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::DateTimePicker1Exit(TObject *Sender)
{
          a.exit_paris();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
       
        ComboBox1->Items->Add("1");
        ComboBox1->Items->Add("2");
        ComboBox1->Items->Add("3");
        ComboBox1->Items->Add("4");
        ComboBox1->Items->Add("5");
        ComboBox1->Items->Add("6");

        for(int i=0;i<=10;i++)
        {
                ListView1->Items->Insert(i);
                ListView1->Items->Item[i]->Caption="id"+IntToStr(i);
                ListView1->Items->Item[i]->SubItems->Add("combol"+IntToStr(i));
                ListView1->Items->Item[i]->SubItems->Add("text"+IntToStr(i));
                ListView1->Items->Item[i]->SubItems->Add("2006-05-22");
        }
       
        /*****************************************/
        a.init_listview(ListView1,Form1);
        a.set_pairs(ComboBox1,1);
        a.set_pairs(Edit1,2);
        a.set_pairs(DateTimePicker1,3);
        a.ontimer();
        /*****************************************/

}
//---------------------------------------------------------------------------
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2545次
    • 积分:36
    • 等级:
    • 排名:千里之外
    • 原创:0篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章存档