打印本文 打印本文 关闭窗口 关闭窗口
Delphi控件的拿来主义(二)
作者:武汉SEO闵涛  文章来源:敏韬网  点击数2141  更新时间:2009/4/23 18:40:07  文章录入:mintao  责任编辑:mintao
Export) then

  FBeforeExport(Self);

 //set the counter to zero

 C:=0;

 Temp:=ShortDateFormat;

 ShortDateFormat:=FDateFormat;

{*************************以下是文本数据导入的核心代码部分,也是我要关心的部分**************************}

 FDataset.DisableControls;

 while (not Eof(FFile)) and (not FStop) do

  begin

   //read from CSV

   Readln(FFile,RecordString);

   //注意,这里好像差了一点什么东西

   //add new record

   try

    FDataset.Append;

    for i:=1 to CountMapItems do

     if Uppercase(GetMapItem(i,D)) <> Uppercase(FIgnoreStr) then

     case FDataset.FieldByName(GetMapItem(i,D)).DataType of

      ftInteger:

       FDataset.FieldByName(GetMapItem(i,D)).AsInteger:=StrToIntDef(Trim(GetCSVRecordItem(i,RecordString)),FDefaultInt);

      ftFloat:

       begin

        try

         F:=StrToFloat(Trim(GetCSVRecordItem(i,RecordString)));

        except

         F:=FDefaultInt;

        end;

        FDataset.FieldByName(GetMapItem(i,D)).AsFloat:=F;

       end;

      else

       if FTrimData then

        FDataset.FieldByName(GetMapItem(i,D)).AsString:=Trim(GetCSVRecordItem(i,RecordString))

       else

        FDataset.FieldByName(GetMapItem(i,D)).AsString:=GetCSVRecordItem(i,RecordString);

     end;

    //post record

    FDataset.Post;

   except

    on E:Exception do

     if not FSilentExport then

      raise

     else

      if Assigned(FExportError) then

       begin

        FExportError(Self,E.Message,C,ErrorResponse);

        if ErrorResponse = pgcsvAbort then

         Break;

       end;

   end;

   if Assigned(FOnAddRecord) then

    FOnAddRecord(Self);

   if Assigned(FExportProgress) then

    FExportProgress(Self, C, FStop);

   Inc(C);

  end;

 FDataset.EnableControls;

{*************************以上是文本数据导入的核心代码部分**************************}

 if Assigned(FAfterExport) then

  FAfterExport(Self);

 //close table if nessecary

 if FAutoOpen then

  begin

   if Assigned(FBeforeCloseTable) then

    FBeforeCloseTable(Self);

   FDataset.Close;

   if Assigned(FAfterCloseTable) then

    FAfterCloseTable(Self);

  end;

 //close CSV file

 CloseFile(FFile);

 //disallocate buffer

 FreeMem(Buffer);

 ShortDateFormat:=Temp;

 //free cache

 for i:=FFieldCache.Count - 1 downto 0 do

  Dispose(FFieldCache.Items[i]);

 FFieldCache.Free;

end;

   (这么长!看得我眼都花了.好在找到了我所关心的核心代码,其他的?别管了吧,我这个懒惰的人.)差的是什么呢?很明显,我们希望把这个时候的RecordString开放出去,经过处理之后再回来进行导入的操作.所以,这里差的只是一个事件处理过程,在这个过程中,我们需要通过Delphi把RecordString的值传出去让用户处理.

    既然没有提供这个接口,那就自己动手添加了。由于需要传参数,这个事件不能用标准的TNotifyEvent来定义,而需要重新声明。

 新的事件声明和事件属性如下:

type

 ...

  TPgCSVRegulateStrEvent  = procedure (Sender : TObject; var ARecordString: string) of object;

 ...

TPgCSV = class(TComponent)

Published

   property RegulateString   : TPgCSVRegulateStrEvent read FRegulateString write FRegulateString;

  //写完后别忘了按一下ctrl+shift+c

 ...

End;

 

  好了,现在可以在我刚才注释的地方写事件调用方法的程序了.

...

 while (not Eof(FFile)) and (not FStop) do

  begin

   //read from CSV

   Readln(FFile,RecordString);

  //xm4014''''s modification

   if Assigned(FRegulateString) then

    FRegulateString(self,RecordString);

  //add new record

   try

    FDataset.Append;  

 ...

重新编译包文件通过后,你就会在TPgCSV控件的事件页面中发现RegulateString这个事件,双击它就可以添加你的处理代码了。这样一来,无论文本数据中有什么样怪异的字符或格式,我们都通过这个事件处理预先过滤一编,让控件放心的处理导入的操作。简单吧(也太简单了点,居然还写了这么长!汗)

但是,这样就行了吗?

 

上一页  [1] [2] [3] 

打印本文 打印本文 关闭窗口 关闭窗口