Delphi 복습 코드

35868 단어 Delphi
1. 파일 가능한 경로 가져오기
Path := ExtractFilePath(Application.ExeName);//실행 가능한 파일 경로 TXMLDocument을 가져옵니다.Create(ExtractFilePath(Application.ExeName)+ '001.xml')
2、DisableControls EnableControls
ClientDataSet.DisableControls;
...
for I := 0 to 10000 do
begin
  ClientDataSet.Append;
  ...
  ClientDataSet.Post;
end;...
ClientDataSet.EnableControls
3. XML의 해석
<?xml version="1.0" encoding="utf-8"?>

<root>

  <info id="id1" name="name1" />

  <info id="id2" name="name2" />

</root> 



var

  XMLDoc: TXMLDocument;

  iNode, tmpNode: IXMLNode;

begin

  with ClientDataSet1 do

  begin

    FieldDefs.Add('id',ftString,250);

    FieldDefs.Add('name',ftString,100);

    CreateDataSet;

  end;



  XMLDoc:= TXMLDocument.Create(Owner);

  XMLDoc.FileName:='E:\1.xml';

  XMLDoc.Active:=True;

  iNode := XMLDoc.DocumentElement.ChildNodes.First;

  while iNode<> nil do

  begin

    ClientDataSet1.AppendRecord([iNode.Attributes['id'],iNode.Attributes['name']]);

    iNode:= iNode.NextSibling;

  end;

  XMLDoc.Free;

end;

3. DelPhi는 동적으로ComBoBox에 값을 부여하고ComBox에서 값을 얻는다.
1、          (    、    )    ComBoBox



 cbProjectName.Items.Clear;

   with adoQryProject do

   begin

     First;

     while not Eof do

     begin

       ProjectNameStr := fieldByName('ProjectName').AsString;

       if Trim(ProjectNameStr) <> '' then

         cbProjectName.Items.AddObject(ProjectNameStr, TObject(fieldByName('ProjectID').AsInteger));

       Next;

     end;

   end; 



2、  ComBoBox   



ProjectID:= IInteger(cbProjectName.Items.Objects[cbProjectName.ItemIndex]);

ProjectName:= cbProjectName.Text; 



3、            :



1>、  Combobox   AutoComplete   true  ; 



2>、combobox1.ItemIndex := Combobox1.Items.IndexOf(trim(edit1.text));

4. 이미지 액세스
1var

   PicStream: TMemoryStream;



 if imgBugPic.Picture.Graphic <> nil then

  begin

    PicStream := TMemoryStream.Create; //       

    imgBugPic.Picture.Graphic.SaveToStream(PicStream);

    TBlobField(FieldByName('BUGPic')).LoadFromStream(PicStream);

    PicStream.Free;

  end; 



2var

  PicStream: TMemoryStream; 

 if not FieldByName('BUGPic').IsNull then

      begin

        try

          PicStream := TMemoryStream.Create;

          TBlobField(FieldByName('BUGPic')).SaveToStream(PicStream);

          PicStream.Position := 0;

          imgBugPic.Picture.Bitmap.LoadFromStream(PicStream);

          PicStream.Free;

        except

          ShowMessage('   ,     .');

        end;

      end;

5, 동적 창 만들기
//     

procedure TForm1.btn1Click(Sender: TObject);

begin

With TForm2.Create(Application) do

Try

   ShowModal;

finally

   free;

end;

end;

 

//     

procedure TForm1.btn2Click(Sender: TObject);

begin

  if not assigned(Form2) then

  begin

    Form2:=TForm2.Create(nil);

    Form2.Show;

  end

end;

 

//              FORM2     

procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);

begin

   Form2:=nil;           //Form       

  Action := cafree;     //Form          

end;

6、오류 버퍼링 이상
try

     ....(       )

except

  on e:exception do

     application.MessageBox(pchar(e.Message), '    ', 0);

end;

7. 이달의 첫날과 마지막 날
USER :DateUtils

procedure TForm1.btn1Click(Sender: TObject);

begin

     mmo1.Lines.Add(FormatDateTime('yyyy-mm-dd',StartOfTheMonth(Now)));

     mmo1.Lines.Add(FormatDateTime('yyyy-mm-dd',EndOfTheMonth(Now)));

   //mmo1.Lines.Add(DateTimeToStr(StartOfTheMonth(Now)));

   //mmo1.Lines.Add(DateTimeToStr(EndOfTheMonth(Now)));

end;

8. 판단 폴더, 만들지 않음
procedure TForm1.Button1Click(Sender: TObject);

var

Pach:string;

begin

Pach:='E:\123\223';                //      

if not DirectoryExists(pach) then  //         

  begin

  //     (ForceDirectories       ,       :CreateDirectory)

  ForceDirectories(Pach);

  end

else

  begin

  showmessage('      ');

  end;

end;

9. Edit 주기에 값 부여
var

  i:Integer;

  control:TComponent;

begin

  for i := 0 to 99 do

  begin

    control := FindComponent('Edit'+IntToStr(i));

    if control <> nil then

    begin

      TEdit(control).Text := IntToStr(i);

    end;

  end;

end;

10. 곡선 그리기
TeeChart      



var

  i:Integer;

  Series:array of TFastLineSeries;    //           

begin

  SetLength(Series,MyTHead.Count);

  Chart1.SeriesList.Clear;

  for i:= 0 to MyTHead.Count-1 do  //MyTHead   Tstringlist,  MyTHead          

  begin

    Series[i]:=TFastLineSeries.Create(nil);

    Series[i].ParentChart:= Chart1; //   CHART1       

    Series[i].Title:= MyTHead[i];

  end;

end;

11. 석방 판단
XML := TNativeXml.Create(nil);



if assigned(XML)then

begin

  FreeAndNil(XML);

end;

12. 잠금 제어 CDS 메모리 테이블 XML 값
 with ClientDataSet1 do

    begin

      //EmptyDataSet;

      //Close;

      if FieldDefs.Count<=0 then

      begin

        FieldDefs.Clear;

        FieldDefs.Add('id',ftInteger,0);

        FieldDefs.Add('cname',ftString,100);

        FieldDefs.Add('contact',ftString,100);

        FieldDefs.Add('phone',ftString,100);

        FieldDefs.Add('addr',ftString,100);

        FieldDefs.Add('remark',ftString,100);

        //IndexDefs.Add('PK', 'id', [ixPrimary, ixUnique]);

        CreateDataSet;

      end;

      DisableControls;



     // XML.Clear;

      if not assigned(XML) then

      XML := TNativeXml.CreateName('companies');



      H := TAuthHeader.Create;

      H.UserName := '555' ; //        

      H.PassWord := '555';

      Headers := WebServer as ISOAPHeaders;

      Headers.Send(H);



      retStr :=WebServer.findCompany ;

      XML.ReadFromString(retStr);

      XML.XmlFormat := xfReadable;

      //XML.SaveToFile('D:\com');



     // showmessage(IntToStr(list.Count));

      if not assigned(list) then list:=TList.Create

      else list.Clear;





      XML.Root.NodesByName('company', list);

      //showmessage('2');

      // ClientDataSet1.r

      MergeChangeLog;

      EmptyDataSet;

      listcount := list.Count;

      for i := 0 to listcount - 1 do

      begin

         node := list[i];

         if  node.ReadInteger('id')>0 then

         begin

            Append;

            FieldByName('id').AsInteger := node.ReadInteger('id');

            FieldByName('cname').AsString := node.ReadString('cname');

            FieldByName('addr').AsString := node.ReadString('addr');

            FieldByName('contact').AsString := node.ReadString('contact');

            FieldByName('phone').AsString := node.ReadString('phone');

            FieldByName('remark').AsString := node.ReadString('remark');

            Post;

         end;

         //DisPose(list[i]);

      end;

       EnableControls;

       Open;



    end;

13. 창 드래그
procedure TMain_Frm.imgbdMouseMove(Sender: TObject; Shift: TShiftState; X,

  Y: Integer);

begin

    if (ssleft in shift) then

  begin

    releasecapture;

    perform(wm_syscommand, $f012, 0);

  end;

end;

14. 힌트 삭제
if Application.MessageBox('      ,     ?', '    ', MB_OKCANCEL + MB_ICONQUESTION)=mrOk then

begin

  ShowMessage('    OK!');

end;

15. GUID guid 생성
function GetGUID: string;

var

  Guid: TGUID;

begin

  CreateGUID(Guid);

  Result := GUIDToString(Guid);

end;

16. 검측 전화 번호
function TMain_frm.IsMobileNumber(num: string): boolean;

begin

  Result := False;

  if length(trim(Num)) <> 11 then

    Exit;

  if ((copy(num, 1, 2) <> '13') and (copy(num, 1, 2) <> '15')) and (copy(num, 1, 2) <> '18') and (copy(num, 1, 2) <> '14') and (copy(num, 1, 2) <> '17'))
)
)
then Exit; try StrToInt(copy(num, 3, 9)); Result := True; except end; end;

17. DBGrid가 표시하는 내용 변경
procedure TAppInterface_frm.dbgrd1DrawColumnCell(Sender: TObject;   const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);

var

  str:string;

begin

  if (Column.Field <>nil) and (Column.FieldName = 'isencrypt') then

  begin

    if Column.Field.IsNull then exit;

    case integer(Column.Field.value)  of

       0:str:='  ';

       1:str:='  ';

       2:str:='Header';

    end;

    (Sender as TDBGrid).Canvas.TextRect(Rect,Rect.Left,Rect.Top,str);

  end;



end;
procedure TMaindlg.ADOQueryDisplayIsUsedGetText(Sender: TField; var Text: String; DisplayText: Boolean);

beginif  ADOQueryDisplay.FieldByName('Sex').AsString='0' then

          text:=' '

      else

          text:=' ';

end;

DrawDataCell(ARect, DrawColumn.Field, AState);//는 셀 Dbgrid에서 데이터를 채울 때 이벤트 DrawColumnCell(Arect, ACol, DrawColumn, Astate)을 트리거하는 경우입니다.//는 Dbgrid가 각 열을 생성할 때 이벤트를 트리거합니다.
18. Label은 자동으로 줄을 바꾸어 Autosize=false;WordWrap=true 재설정;
19. ListBox 기본 사용 방법:
 )  

ListBox1.Add(EditBox1.Text);//     



Items.Append('Append');// Add



ListBox1.insert(0,EditBox1.Text);//     





//TcxImageList              

lbxAppSupply.InsertItem(i,node.ReadString('mname')+' '+node.ReadString('name'),Random(2));

//   values 

lbxAppSupply.Values[i] := node.ReadString('id');



imglist1.Items[imglist1.ItemIndex];//  

imglist1.Values[imglist1.ItemIndex];// 



//    

for I := 0 to imglist1.Count - 1 do

  begin

    if imglist1.Selected[I] then

    begin

      mmo1.Text := mmo1.Text+'Selected[i]:'+imglist1.Items[i]+#13#10;

      mmo1.Text := mmo1.Text+'Values[i]:'+imglist1.Values[i]+#13#10;

    end;   

  end;  





 )  



ListBox1.DeleteSelected;//          



imglist1.Items.Delete(imglist1.ItemIndex);//     

//TcxImageList 

imglist1.Selected[imglist1.Items.IndexOf('      ')]:= True;



ShowMessage(imglist1.Items[imglist1.ItemIndex]);







List      



1)ItemIndex:    list    0  ,     -1



2)imglist1.Items[imglist1.ItemIndex]:        



3)Items.Strings[imglist1.ItemIndex]=Items[imglist1.ItemIndex]



4)GetCount         0  ,          

좋은 웹페이지 즐겨찾기