Delphi 不重覆的字串列 TStringList 试作

Delphi 不重覆的字串列 TStringList 试作

环境:Delphi RAD 10.4
动机:
不重覆字串对於Python是很简单的,只须把List 丢给Set( )就能自动排序、去掉重覆者。
今天实作一个Delphi的类似功能,应用的是:
List.Sorted := True;
List.Duplicates := dupIgnore;

画面:
https://ithelp.ithome.com.tw/upload/images/20210817/20111373Cd77ScZPlw.jpg

程序目地: 让comboBox1 从TXT档载入,可在edit1新增,程序结束再存回TXT档。
其中主要的功能包含:
TStringList.LoadFromFile
TStringList.SaveToFile
TString.Duplicates
说明:
文字档filterHx.Txt 必需放在 Win32\Debug内 ,因为RAD10 会把EXE档放在这里。

全部程序码:

unit testStringList01;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    ComboBox1: TComboBox;
    function sUnique(List:TStringList; newOne: string):TStringList;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button1Click(Sender: TObject);
    procedure Edit1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  uString : TStringList;
implementation

{$R *.dfm}

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  uString.SaveToFile('filterHx.txt');
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  i : integer;
begin
   uString := TStringList.Create;
   //--- 编码时,此档应放在 win32\debug内
   uString.LoadFromFile('filterHx.txt');
   comboBox1.Items.Assign(uString);
end;

//--- TStringList 重整 排序 不重覆
function TForm1.sUnique(List:TStringList; newOne: string):TStringList;
begin
  //--- Ignore if duplicated
  List.Sorted := True;
  List.Duplicates := dupIgnore;

  List.Add(newOne);
  result := List;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  tmp : string;
  i : integer;
begin
  tmp := edit1.Text;
  if length(tmp) <>0 then begin
     sUnique(uString,tmp);
     comboBox1.Items.Assign(uString);
   end;
end;

procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  if Key = 13 then Button1Click(nil);
end;

end.

<<:  安装 elementary OS 6.0 与呒虾米

>>:  [ JS个人笔记 ] Event Loop事件循环—DAY11

30-19 之 Domain Layer - Repository

接下来我们要来谈谈,应该不少人常听到的『 Repository 』这个东东,目前我先将他放在 3-T...

[机派X] Day2 - 树莓派碰上 Ubuntu

引言 今天是机派X系列文章的第二天,这篇文章终於要进入正题了! 首先,我们要在树莓派上安装 Linu...

2.1 Design System - 制作的工具

「有些事现在不做,一辈子都不会做了」~练习曲 求学时,体育老师让我们看这部电影并身体力行的带我们骑...

D3 Django 资料夹结构与设定说明

今天来了解一下目前整个资料夹架构跟档案设定 移动到Django_project资料夹在powersh...