在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
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 /> <info /> </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赋值,从ComBoBox中取值 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、图片的存取 1、存图片到数据库 var 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; 2、从数据库中读取图片并显示 var 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中填充数据时触发该事件
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开始,所以可以作为插入序号 20、Query字段属性 显示 添加 procedure TfrmYiSheng.qry1isUploadGetText(Sender: TField; var Text: string; DisplayText: Boolean); begin inherited; if Sender.AsInteger = 1 then Text := '是 ' else Text := '否 '; end; procedure TfrmYiSheng.qry1isUploadSetText(Sender: TField; const Text: string); begin inherited; if Text = '是 ' then Sender.AsInteger := 1 else Sender.AsInteger := 0; end; 21、inc() dec() inc有两种用法: 一个是inc(integer),就是加1 比如 i := 100; inc(i); 就是i变为101 另一个是inc(integer,integer)就是加n 比如 i:=100; inc(i,50); 就是i变成150 --------------------------------- Dec是递减函数 i:=100; dec(i); i就变成99了,如果是dec(i,30)的话,那么i=100-30=70 22、 ChangeFileExt function ChangeFileExt(const FileName, Extension: string): string; var I: Integer; begin I := LastDelimiter('.' + PathDelim + DriveDelim,Filename); if (I = 0) or (FileName[I] <> '.') then I := MaxInt; Result := Copy(FileName, 1, I - 1) + Extension; end; 比如: s:=changefileext('f:\123.txt','.ini'); showmessage(s);//f:\123.ini 23、Ini 文件读写 procedure TfrmMain.FormCreate(Sender: TObject); var S: string; Ini: TIniFile; begin S := ChangeFileExt(Application.ExeName,'.ini'); Ini := TIniFile.Create(S); try edReadFilename.Text := Ini.ReadString('Files','Read',''); edWriteFilename.Text := Ini.ReadString('Files','Write',''); finally Ini.Free; end; end; procedure TfrmMain.FormDestroy(Sender: TObject); var S: string; Ini: TIniFile; begin S := ChangeFileExt(Application.ExeName,'.ini'); Ini := TIniFile.Create(S); try Ini.WriteString('Files','Read',edReadFilename.Text); Ini.WriteString('Files','Write',edWriteFilename.Text); finally Ini.Free; end; end; 24、有序类型 Dec 将例程中的参数值递减1或一个特定的值,其中特定值可在
第二个可选参数中定义
Inc 将例程中的参数值增加1或一个特定的值
Odd 如果参数为奇数返回真
Pred 根据参数在其数据类型定义中的序列,返回参数值的前驱值
Succ 返回参数值的后继值
Ord 返回参数值在其数据类型值集合中的序号
Low 返回参数对应的有序数据类型的最小取值
High 返回参数对应的有序数据类型的最大取值
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论