⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 我的小记.txt

📁 我有使用DELPHI编程中用到的记下来的一些小技巧能为你提供点小小的帮助
💻 TXT
📖 第 1 页 / 共 2 页
字号:
判断是不有edit或combobox是否有空的函数

Function TextIsNull: Boolean;
function TForm1.TextIsNull: Boolean;//判断信息是不是为空
var
  i: Integer;
begin
  Result := False;
  For i := 0 to ControlCount-1 do
    if Controls[i] is TEdit then
    begin
      if Trim(TEdit(Controls[i]).Text)='' then
      begin
        Result := True;
        Exit;
      end;
    end
    else if Controls[i] is TComboBox then
    begin
      if Trim(TComboBox(Controls[i]).Text)='' then
      begin
        Result := True;
        Exit;
      end;
    end;
end;
-----------------------------
删除dbgrid中选定数据的方法:
begin
      adotable1.Delete;
      adotable1.first;
若要追加和修改信息:adotable1.Append;adotable1.post;
------------------------------------------------
将数据写入库:
procedure Tclassmanageform.SaveToTable;
begin
    ClassmanageForm.adotable1.edit;
    ClassmanageForm.adotable1.FieldByName('班级名').asstring:=edit1.text;
    ClassmanageForm.adotable1.FieldByName('入学时间').asdatetime:=StrToDate(edit9.text);
    ClassmanageForm.adotable1.FieldByName('所在院系').AsString:=edit3.text;
    ClassmanageForm.adotable1.FieldByName('班长').asstring:=edit4.text;
    ClassmanageForm.adotable1.fieldbyname('班长联系电话').asstring:=edit5.text;
    //ClassmanageForm.adotable1.FieldByName('班主任').asstring:=edit6.text;
    //ClassmanageForm.adotable1.FieldByName('班主任联系电话').asstring:=edit7.text;
    ClassmanageForm.adotable1.FieldByName('操作员').asstring:=mainform.username;
    ClassmanageForm.adotable1.FieldByName('最后修改时间').asdatetime:=date();
    ClassmanageForm.adotable1.FieldByName('所学专业').asstring:=edit2.text;
    ClassmanageForm.adotable1.FieldByName('班级人数').asinteger:=StrToInt(edit8.text);
    ClassmanageForm.adotable1.Post;
end;
------------------------------------------------
显示一个提示对话框:
application.MessageBox('提示','您确定要删除该班级吗?',mb_iconinformation+mb_yesno);
如要点‘是’的话,可以写为:
if (application.MessageBox('提示','您确定要删除该班级吗?',mb_iconinformation+mb_yesno))

=IdYes then
……
mb_iconinformation:提示图标 mb_iconwarning:警告图标    
------------------------------------------------
判断一个内容框中的值是否为空,为空时‘确定’按纽不可用:
  if (length(edit1.Text)>1) and (length(edit2.Text)>1) and (length(edit3.Text)>1) and
      (length(edit4.Text)>1) and (length(edit5.Text)>1) and (length(edit8.Text)>1) then
     bitbtn1.Enabled:=true
  else
    bitbtn1.Enabled:=false;
------------------------------------------------
进入一开始就读出数据库的信息并显示到EDIT中(写一个函数):
procedure Tclassmanageform.LoadFromTable;
begin
   ClassmanageForm.edit1.text:=adotable1.FieldByName('班级名').asstring;
   ClassmanageForm.edit9.text:=DatetoStr(adotable1.FieldByName('入学时间').asdatetime);
   ClassmanageForm.edit3.text:=adotable1.FieldByName('所在院系').AsString;
   ClassmanageForm.edit4.text:=adotable1.FieldByName('班长').asstring;
   ClassmanageForm.edit5.Text:=adotable1.fieldbyname('班长联系电话').asstring;
   //ClassmanageForm.edit6.text:=adotable1.FieldByName('班主任').asstring;
   //ClassmanageForm.edit7.Text:=adotable1.FieldByName('班主任联系电话').asstring;
   ClassmanageForm.edit2.text:=adotable1.FieldByName('所学专业').asstring;
   ClassmanageForm.edit8.text:=IntToStr(adotable1.FieldByName('班级人数').asinteger);
end;
------------------------------------------------
如何实现数据库的备份与恢复:
备份:
procedure TBackupFrm.BitBtn2Click(Sender: TObject);
begin
if Edit1.Text= ' then
begin
Showmessage('无选择要保存的文件名');
exit;
end;
try
try
dmData.adoQryTmp.Active:= false;
dmData.adoQryTmp.SQL.Clear;
dmData.adoQryTmp.SQL.Add('BACKUP DATABASE [dzyl] TO DISK = ''+edit1.text+'' WITH INIT');
dmData.adoQryTmp.ExecSQL;
finally
begin
dmData.adoQryTmp.Active:= false;
Showmessage('数据库备份成功!');
end;
end;
except
on e:exception do
begin
ShowMessage('数据库备份失败!');
end;
end;
end;
---------------------------
恢复
procedure TBackupFrm.BitBtn4Click(Sender: TObject);
begin
  if Edit2.Text = ' then
  begin
    showmessage('未选择要恢复的数据库文件!');
    exit;
  end;
  with dmData do
  begin
    try
      adocmmd.CommandText:='use master';
      adocmmd.Execute;
      adocmmd.CommandText:='alter database dzyl set offline with rollback immediate';
      adocmmd.Execute;
      adocmmd.CommandText:='restore database dzyl from disk= ''+edit2.Text+'' with 

recovery ';
      adocmmd.Execute;
      adocmmd.CommandText:=' alter database dzyl set online with rollback immediate';
      adocmmd.Execute;
      showmessage('数据库恢复成功!');
      application.Terminate;
    except
      on e:exception do
      begin
        showmessage('数据库恢复失败!'+e.Message);
      end;

  end;
end;
其中dmData.adoQryTmp连接的是系统MASTER数据库,备份还原之前应该关闭要备份还原的AdoConn数据库

连接AdoConn.Connected:=False;
------------------------------------------------
另一方法:
备份如下:
        try
            backupString := 'BACKUP DATABASE [Paper] TO  DISK = N''+edit1.Text+'' WITH  

INIT ,  NOUNLOAD ,  NAME = N'Paper 备份',  NOSKIP ,  STATS = 10,  NOFORMAT';
            adoquery1.Close;
            adoquery1.SQL.Clear;
            adoquery1.SQL.Add(backupString);
            ADOQuery1.Prepared;
            adoquery1.ExecSQL;
            application.MessageBox('备份成功。','提示',0);
        except
            application.MessageBox('备份出错!请重新备份数据。','出错',0);
        end;
---------------------------
还原如下:
            if opendialog1.Execute then
            begin
                try
                    adoquery1.Close();
                    adoquery1.SQL.Clear;
                    adoquery1.SQL.Add('use master');
                    adoquery1.Prepared;
                    adoquery1.ExecSQL;
                    restorestring := 'RESTORE DATABASE [Paper] FROM  DISK = 

N''+opendialog1.FileName+'' WITH  FILE = 1,  NOUNLOAD ,  STATS = 10,  REPLACE,RECOVERY';
                    self.ADOCommand1.CommandText := restoreString;
                    adocommand1.Execute;
                    application.MessageBox('还原数据成功','提示',0);

                    adoquery1.Close();
                    adoquery1.SQL.Clear;
                    adoquery1.SQL.Add('use paper');
                    adoquery1.Prepared;
                    adoquery1.ExecSQL;          
                except
                    application.MessageBox('还原数据出错!请重新还原,并停止一切的数据操作!

','提示',0);
                end;
            end;
------------------------------------------------
判断一个输入框中字符的合法性:
在keypress事件中写入:
  if not(key in ['0'..'9',#8,#13]) then
    begin
    key:=#0;
    showmessage('请您正确填写电话号码!');
    end;
------------------------------------------------
使用combobox时,让拉动菜单不可修改:
将style属性设为csdropdownlist。
------------------------------------------------
如何使得在使用pagecontrol组件时,按快捷键就会跳到相应Tabsheet中去?
首先,把form窗体的keypreview设为true,然后在form的onkeydown事件中写:
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  case key of
          VK_F1agecontrol1.Pages[0].Show;
          VK_F2agecontrol1.Pages[1].show;
          VK_F3agecontrol1.Pages[2].Show;
  end;
end;
------------------------------------------------
给应用程序设置全局快捷键,本程序设置了 2 个快捷键,无论现在的焦点在哪个控件上,按 Ctrl+R 和 

Ctrl+Q 都会调用对应的过程。
    
unit Unit1;
interface
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
type
  TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
protected
procedure hotykey(var msg: TMessage); message WM_HOTKEY;
  end;
var
Form1: TForm1;
id, id2: Integer;
implementation
{$R *.DFM}
    
procedure TForm1.hotykey(var msg: TMessage);
begin
  if (msg.LParamLo=MOD_CONTROL) and (msg.LParamHi=81) then
     begin
       ShowMessage(’Ctrl + Q ’);
     end;
  if (msg.LParamLo=MOD_CONTROL) and (msg.LParamHi=82) then
     begin
       ShowMessage(’Ctrl + R ’);
     end;
end;
    
procedure TForm1.FormCreate(Sender: TObject);
  begin
    id :=GlobalAddAtom(’hotkey’);
    RegisterHotKey(handle, id, mod_control, 81);
    id2 :=GlobalAddAtom(’hotkey2’);
    RegisterHotKey(handle, id2, mod_control, 82);
  end;
    
procedure TForm1.FormDestroy(Sender: TObject);
  begin
    UnRegisterHotKey(handle,id);
    UnRegisterHotKey(handle,id2);
  end;
------------------------------------------------
怎样实现在使用treeview时,当点击一个分支时,从数据库中读出相应数据:
if xsda_form.TreeView1.Items[1].Selected then
  begin
     xsda_form.Caption:='学生档案';
     xsda_form.Caption:=xsda_form.Caption+'('+xsda_form.TreeView1.Items[0].Text+'  

'+xsda_form.TreeView1.Items[xi_shuxue].Text+xsda_form.TreeView1.Items[ji_02].Text+'级)';
     xsda_form.Query1.SQL.Clear;
     xsda_form.Query1.SQL.Add('select *');
     xsda_form.Query1.SQL.Add('from shuxuexi_xsda.db');
     xsda_form.Query1.SQL.Add('where Rxsj=''+xsda_form.TreeView1.Items[ji_02].Text+'');
     xsda_form.Query1.Active;
     xsda_form.Query1.Open;
  end;
------------------------------------------------
如何在面板上判断所指对象为某控件时,都会统一做出响应:
procedure TMainForm.Button1Click(Sender: TObject);
begin
if Sender = Button1 then     //此处也可以看是否为edit1啊,其他一些控件 
  AboutBox.Caption := 'About ' + Application.Title 
else 
  AboutBox.Caption := '; 
AboutBox.ShowModal;
end;
------------------------------------------------
如何把鼠标的移动区域限制在(100,100,200,200)
var rect:TRect;
begin
rect.Left:=100;
rect.Top:=100;
rect.Bottom:=200;
rect.Right:=200;
windows.ClipCursor(@rect);

下面恢复鼠标的移动区域
windows.ClipCursor(0);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -