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

📄 main.pas

📁 自己做的通讯录程序
💻 PAS
📖 第 1 页 / 共 4 页
字号:
     result := true;
     exit;
   end;
end;

procedure TForm1.NImportClick(Sender: TObject);
var
  i, j, count, nMaxGroup, GroupIDnum: Integer;
  f: TIniFile;
  p: PTItem;
  AQ: TADOQuery;
  GroupID: Integer;
  GroupName, strName: String;
  ListItem: TListItem;
  bGroup, bPersonExist: Boolean;
  bUpdateDB: Boolean;  //是否使用外来的数据覆盖数据库中的相同名字的数据
begin
  if MessageBox(Handle, '如果有与数据库中相同名字的人员,则覆盖现有的数据吗?',
                '覆盖吗?', MB_YESNO or MB_ICONQUESTION) = IDYES then
     bUpdateDB := true
  else
     bUpdateDB := false;


  AQ := TADOQuery.Create(self);
  AQ.Connection := ADOConnection1;

  nMaxGroup := -1;
  for i:=0 to Length(GroupIDs)-1 do
     if nMaxGroup < GroupIDs[i] then
       nMaxGroup := GroupIDs[i];

  if OpenDialog1.Execute then
  begin
    f := TIniFile.Create(OpenDialog1.FileName);
    //编组数据
    count := f.ReadInteger('GROUPNUMBER', 'Number', 0);
    for i:=0 to count-1 do
    begin
      GroupName := f.ReadString('Group'+IntToStr(i+1), 'GroupName', '');
      //看看已有的组别中有没有这个组
      bGroup := false;
      for j:=0 to clbGroup.Items.Count-1 do
        if GroupName = clbGroup.Items[j] then
          bGroup := true;
      //如果没有,则加入
      if not(bGroup) then
      begin
        nMaxGroup := nMaxGroup + 1;
        GroupID := nMaxGroup;
        strSQL := Format('Insert INTO GroupList(GroupID, GroupName) Values(%d, ''%s'')',
                         [GroupID, GroupName]);
        AQ.SQL.Clear;
        AQ.SQL.Add(strSQL);
        AQ.ExecSQL;

        SetLength(GroupIDs, Length(GroupIDs)+1);
        GroupIDs[Length(GroupIDs)-1] := GroupID;
        clbGroup.Items.Add(GroupName);
      end;
    end;

    //人员信息
    count := f.ReadInteger('RECORDNUMBER', 'Number', 0);
    for i:=0 to count-1 do
    begin
      strName := f.ReadString(IntToStr(i+1), 'Name', '');

      bPersonExist := PersonExist(strName);
      if bPersonExist then
      begin
        if not(bUpdateDB) then  //如果不覆盖就算了,否则要删除原来的数据
           continue
        else   //要删除原来的人员
        begin
          for j:=0 to NameList.Items.Count-1 do
            if strName = pTItem(NameList.Items[j].Data).Name then
            begin
              //NameList.Items.Delete(j);
              //DeleteItem(j);
              p := NameList.Items[j].Data;
              ListItem := NameList.Items[j];
              break;
            end;
        end;
      end
      else begin
        new(p);
        maxid         := maxid + 1;
        p.ID          := maxid;
      end;

      p.Name        := strName;
      p.sex         := f.ReadString(IntToStr(i+1), 'sex', '');
      p.Telphone    := f.ReadString(IntToStr(i+1), 'Telphone', '');
      p.Email       := f.ReadString(IntToStr(i+1), 'Email', '');
      p.QQ          := f.ReadString(IntToStr(i+1), 'QQ', '');
      p.City        := f.ReadString(IntToStr(i+1), 'City', '');
      p.Birthday    := f.ReadString(IntToStr(i+1), 'Birthday', '');
      p.BirthShow   := GetBirthShow(p.Birthday);
      p.UnitName    := f.ReadString(IntToStr(i+1), 'UnitName', '');
      p.HomeAddress := f.ReadString(IntToStr(i+1), 'HomeAddress', '');
      p.PostAddress := f.ReadString(IntToStr(i+1), 'PostAddress', '');
      p.UseLevel    := f.ReadInteger(IntToStr(i+1),'UseLevel', 1);
      p.Remark      := f.ReadString(IntToStr(i+1), 'Remark', '');
      p.UserName    := m_CurUser;

      //人员编组信息
      GroupIDNum := f.ReadInteger(IntToStr(i+1),'GroupIDNumber', 0);
      SetLength(p.GroupIDs, GroupIDNum);
      for j:=0 to GroupIDNum-1 do
        p.GroupIDs[j] := f.ReadInteger(IntToStr(i+1), 'GroupID'+IntToStr(j+1), 0);

      if not(bPersonExist) then
      begin
        p.ItemState   := isNew;
        ListItem      := NameList.Items.Add;
        ListItem.Data := p;
      end
      else
        p.ItemState   := isUpdated;

      //在下方的数据列表中显示新加的记录
      ShowItemInList(ListItem);
    end;
    f.Free;
  end;

  AQ.Free;
end;

procedure TForm1.ShowItemInList(ListItem: TListItem);
var
  strGroup: string;
  i, j: Integer;
  p: pTItem;
begin
  p := ListItem.Data;

  ListItem.Caption := p.Name;
  ListItem.SubItems.Clear;

  //电话
  if clbContent.Checked[0] then
     ListItem.SubItems.Add(p.Telphone);

  //分组
  if clbContent.Checked[1] then
  begin
    strGroup := '';
    for i:=0 to Length(p.GroupIDs)-1 do
      for j:=0 to Length(GroupIDs)-1 do
        if p.GroupIDs[i] = GroupIDs[j] then
        begin
          strGroup := strGroup + clbGroup.Items[j] + '-';
        end;
    if Length(strGroup) > 0 then
      Delete(strGroup, Length(strGroup), 1);
    ListItem.SubItems.Add(strGroup);
  end;

  //所在城市
  if clbContent.Checked[2] then
     ListItem.SubItems.Add(p.City);

  //单位
  if clbContent.Checked[3] then
     ListItem.SubItems.Add(p.UnitName);

  //性别
  if clbContent.Checked[4] then
     ListItem.SubItems.Add(p.sex);

  //e-mail
  if clbContent.Checked[5] then
     ListItem.SubItems.Add(p.Email);

  //QQ号
  if clbContent.Checked[6] then
     ListItem.SubItems.Add(p.QQ);

  //家庭住址
  if clbContent.Checked[7] then
     ListItem.SubItems.Add(p.HomeAddress);

  //联系住址
  if clbContent.Checked[8] then
     ListItem.SubItems.Add(p.PostAddress);

  //生日
  if clbContent.Checked[9] then
     ListItem.SubItems.Add(p.BirthShow);

  //常用级别
  if clbContent.Checked[10] then
     ListItem.SubItems.Add(IntToStr(p.UseLevel));

  //备注
  if clbContent.Checked[11] then
     ListItem.SubItems.Add(p.Remark);
end;

procedure TForm1.NDeleteItemClick(Sender: TObject);
var
  nIndex: Integer;
  ListItem, ListItem2: TListItem;
begin
  ListItem := NameList.Selected;

  while ListItem <> nil do
  begin
    nIndex := NameList.Items.IndexOf(ListItem);
    ListItem2 := NameList.GetNextItem(ListItem, sdAll, [isSelected]);
    DeleteItem(nIndex);
    ListItem := ListItem2;
  end;

  NameListChange(nil, NameList.Selected, ctState);
end;

procedure TForm1.NHideItemClick(Sender: TObject);
var
  nIndex: Integer;
  ListItem, ListItem2: TListItem;
begin
  ListItem := NameList.Selected;

  while ListItem <> nil do
  begin
    nIndex := NameList.Items.IndexOf(ListItem);
    ListItem2 := NameList.GetNextItem(ListItem, sdAll, [isSelected]);
    NameList.Items.Delete(nIndex);
    ListItem := ListItem2;
  end;

  NameListChange(nil, NameList.Selected, ctState);
end;

procedure TForm1.btUpdateListClick(Sender: TObject);
var
  i: Integer;
begin
  CreateColumns();
  for i:=0 to NameList.Items.Count-1 do
    ShowItemInList(NameList.Items[i]);
end;

procedure TForm1.NameListChange(Sender: TObject; Item: TListItem;
  Change: TItemChange);
var
  i, j: Integer;
  p: pTItem;
begin
  if (NameList.Items.Count=0)or(NameList.Selected = nil) then
  begin
    ClearAllData();
    Exit;
  end;

  StatusBar1.Panels[1].Text := Format('第 %d 人', [NameList.ItemIndex+1]);

  p := NameList.Selected.Data;
  //显示人员信息
  Name.Text         := p.Name;
  QQ.Text           := p.QQ;
  City.Text         := p.City;
  Email.Text        := p.Email;
  Telphone.Text     := p.Telphone;
  SplitBirthday(p.Birthday);
  unitname.Text     := p.unitname;
  HomeAddress.Text  := p.HomeAddress;
  PostAddress.Text  := p.PostAddress;
  Remark.Text       := p.Remark;

  i := -1;
  repeat
    i := i + 1;
  until (i>=sex.Items.Count)or(sex.Items[i]=p.sex);
  if i<sex.Items.Count then
    sex.ItemIndex := i;

  i := -1;
  repeat
    i := i + 1;
  until (i>=UseLevel.Items.Count)or(UseLevel.Items[i]=IntToStr(p.UseLevel));
  if i<UseLevel.Items.Count then
    UseLevel.ItemIndex := i;

//  AQuery.Close;
  //显示组别信息
  for i:=0 to clbGroup.Items.Count-1 do
    clbGroup.Checked[i] := False;

  for j:=0 to Length(p.GroupIDs)-1 do
  begin
    i := -1;
    repeat
      i := i + 1;
    until ( i=Length(GroupIDs) )or(GroupIDs[i] = p.GroupIDs[j]);
    if (i<Length(GroupIDs)) then
      clbGroup.Checked[i] := True;
  end;
end;

procedure TForm1.btQueryTelClick(Sender: TObject);
var
  i: Integer;
begin
  i := NameList.Items.Count-1;
  while i >= 0 do
  begin
    if ( Pos(editQueryTel.Text, pTItem(NameList.Items[i].Data).Telphone)=0 )
       AND not(ItemModified(NameList.Items[i])) then  //没有被修改的记录才可以隐藏起来
    begin
      NameList.Items.Delete(i);
    end;
    i := i-1;
  end;
  NameListChange(nil, NameList.Selected, ctState);
end;

procedure TForm1.TimerSetCountTimer(Sender: TObject);
begin
  StatusBar1.Panels[2].Text := Format('共 %d 人', [NameList.Items.Count]);
end;

procedure TForm1.NCreateViewClick(Sender: TObject); //创建查询结果表
var
  strSQL, strCol: String;
  i,j,count: Integer;
  ListItem: TListItem;
  ADOQuery: TADOQuery;
begin
  ADOQuery := TADOQuery.Create(nil);
  ADOQuery.Connection := ADOConnection1;
  //表的名称是:"result"
  try
    with ADOQuery do
    begin
      SQL.Clear;
      SQL.Add('Delete From result');
      ExecSQL;
    end;
  except
  end;
  strCol := 'Name ';

  //电话
  if clbContent.Checked[0] then
    strCol := strCol + ',Telphone';

  //分组
  if clbContent.Checked[1] then
    strCol := strCol + ',GroupName';

  //所在城市
  if clbContent.Checked[2] then
    strCol := strCol + ',City';

  //单位
  if clbContent.Checked[3] then
    strCol := strCol + ',UnitName';

  //性别
  if clbContent.Checked[4] then
    strCol := strCol + ',sex';

  //e-mail
  if clbContent.Checked[5] then
    strCol := strCol + ',Email';

  //QQ号
  if clbContent.Checked[6] then
    strCol := strCol + ',QQ';

  //家庭住址
  if clbContent.Checked[7] then
    strCol := strCol + ',HomeAddress';

  //联系住址
  if clbContent.Checked[8] then
    strCol := strCol + ',PostAddress';

  //生日
  if clbContent.Checked[9] then
    strCol := strCol + ',Birthday';

  //常用级别
  if clbContent.Checked[10] then
    strCol := strCol + ',UseLevel';

  //把查询结果数据插入表 result
  for i:=0 to NameList.Items.Count-1 do
  begin
    strSQL := Format('Insert INTO result(%s) Values(''', [strCol]);
    ListItem := NameList.Items[i];
    //生成要插入的数据
    strSQL := strSQL + ListItem.Caption + '''';
    //最后一列“备注”是不要选的
    count := ListItem.SubItems.Count;
    if NameList.Columns[count].Caption = '备注' then
      count := count-1;
    for j:=0 to count-1 do
       strSQL := strSQL + ',''' + ListItem.SubItems[j] + '''';

    strSQL := strSQL + ')';
    ADOQuery.SQL.Clear;
    ADOQuery.SQL.Add(strSQL);
    ADOQuery.ExecSQL;
  end;

  ShowMessage('查询结果表"result"创建完毕!');
  ADOQuery.Free;
end;

procedure TForm1.brReQueryClick(Sender: TObject);
begin
  if ifModify() then
  begin
    if MessageBox(Handle, PChar('有记录被修改,如果重新检索数据,将丢失这些修改'+#13+#10+'要继续吗?'),
                  '要继续吗', MB_YESNO or MB_ICONQUESTION) = IDNO then
       exit;
  end;

  MakeAllEnabled(self, False);

⌨️ 快捷键说明

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