unit Unit2;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes,
  System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
  FMX.Controls.Presentation, FMX.StdCtrls, FMX.Layouts, FMX.edit,
  System.IOUtils;

type
  TForm2 = class(TForm)
    ScaledLayout1: TScaledLayout;
    VertScrollBox1: TVertScrollBox;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    procedure Button1Click(Sender: TObject);
    procedure myClick(Sender: TObject);
    procedure myClick1(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button2Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;
  editm: array of tedit;
  checkm: array of tcheckbox;

implementation

const
  iHtoH = 40; // 行间距
  iWtoW = 10; // 列间距
  // iPerLine = 1; //每行控件个数
  iWidth = 360; // 控件宽度

var
  mylist: TStringlist;
  b: integer;
  FileName: string;

{$R *.fmx}
{$R *.LgXhdpiPh.fmx ANDROID}

procedure TForm2.Button1Click(Sender: TObject); // 新增
var
  i, j: integer;
begin
  i := b; // 从当前记录开始添加
  j := 20 + b * iHtoH;

  checkm[i] := tcheckbox.Create(Self.VertScrollBox1);
  with checkm[i] do
  begin
    Name := 'check' + InttoStr(i);
    Parent := Self.VertScrollBox1;
    Top := j + 5; // 该属性可设置，但不起作用
    Left := iWtoW; // 该属性可设置，但不起作用
    position.x := Left;
    position.y := Top;
    Text := '';
    // Width:=iWidth;
    onchange := myClick;
    Visible := true;

  end;

  editm[i] := tedit.Create(Self.VertScrollBox1);
  with editm[i] do
  begin
    Name := 'Edit' + InttoStr(i);
    Parent := Self.VertScrollBox1;
    Top := j; // 该属性可设置，但不起作用
    Left := iWtoW * 6; // 该属性可设置，但不起作用
    position.x := Left;
    position.y := Top;
    Text := 'Edit' + InttoStr(i); // edit为空
    Width := iWidth;
    onclick := myClick1;
    Visible := true;

  end;

  // j:=j+iHtoH;
  // i:=i+1;
  b := b + 1;

end;

procedure TForm2.Button2Click(Sender: TObject); // 删除
var
  i: integer;
  j: integer;
begin

  j := 0;
  for i := b - 1 downto 0 do
  begin
    if checkm[i].ischecked = true then
    begin
      editm[i].Visible := false; // 不显示
      checkm[i].Visible := false;
      j := j + 1;
    end;

  end;
  showmessage('删除后如不保存，再次打开后可恢复删除记录');
end;

procedure TForm2.Button3Click(Sender: TObject); // 保存
var
  F: TextFile; // 定义文本文件变量
  i: integer;
  s: string;
begin

  AssignFile(F, FileName); // 将磁盘上的文本文件与变量建立关联
  Rewrite(F); // 写文件
  for i := 0 to b - 1 do
  begin
    if checkm[i].Visible = true then // 判断是否已删除
    begin
      if checkm[i].ischecked = true then // 判断是否已选择
      begin
        s := '√ ' + editm[i].Text;
        writeln(F, s);
      end
      else
      begin
        s := '- ' + editm[i].Text;
        writeln(F, s);
      end;
    end;
  end;

  closefile(F);

end;

procedure TForm2.Button4Click(Sender: TObject);
begin
  close;
end;

procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
// form2关闭
var
  i: integer;
begin

  for i := b - 1 downto 0 do
  begin

    editm[i].free; // 清空控件数组
    checkm[i].free;

  end;

  mylist.free; // 释放字符串数组变量

  VertScrollBox1.height := 593; // 恢复滚动区高度
  //恢复允许屏幕旋转
  Application.FormFactor.Orientations := [TFormOrientation.Portrait, TFormOrientation.Landscape,TFormOrientation.InvertedPortrait,TFormOrientation.InvertedLandscape];

end;

procedure TForm2.FormShow(Sender: TObject); // form2显示
var
  F: TextFile; // 定义文本文件变量
  i, j: integer;
  s, s1, s2: string;
begin
  //禁止横屏
  Application.FormFactor.Orientations := [TFormOrientation.Portrait, TFormOrientation.InvertedPortrait];

  VertScrollBox1.height := 593; // 恢复VertScrollBox控件默认高度
  FileName := System.IOUtils.Tpath.GetSharedDocumentsPath +
    '/mycalendar/calendar.txt';
  j := 20; // 首行位置

  if fileexists(FileName) then

  begin

    mylist := TStringlist.Create;

    AssignFile(F, FileName); // 关联文件
    Reset(F);

    while not Eof(F) do
    begin
      Readln(F, s);
      mylist.add(s);
    end;

    closefile(F);




    // mylist.LoadFromFile(FileName);{读文件到mylist}

    b := mylist.Count; { 获得mylist的count }
    setlength(editm, b + 20); // 多留20个数组位置，供手动添加
    setlength(checkm, b + 20);

    for i := 0 to b - 1 do
    begin

      s1 := copy(mylist.strings[i], 1, 2); // 取前2个字符
      s2 := copy(mylist.strings[i], 3, 100); // 取第3至末尾字符串

      // showmessage(s2);

      checkm[i] := tcheckbox.Create(Self.VertScrollBox1);
      with checkm[i] do
      begin
        Name := 'check' + InttoStr(i);
        Parent := Self.VertScrollBox1;
        Top := j + 5; // 该属性可设置，但不起作用
        Left := iWtoW; // 该属性可设置，但不起作用
        position.x := Left;
        position.y := Top;
        Text := '';
        // Width:=iWidth;
        if s1 = '√ ' then
          ischecked := true
        else
          ischecked := false;
        onchange := myClick;
        Visible := true;
      end;

      editm[i] := tedit.Create(Self.VertScrollBox1);
      with editm[i] do
      begin
        Name := 'Edit' + InttoStr(i);
        Parent := Self.VertScrollBox1;
        Top := j; // 该属性可设置，但不起作用
        Left := iWtoW * 6; // 该属性可设置，但不起作用
        position.x := Left;
        position.y := Top;
        Text := s2; // 读一行文本内容至edit
        Width := iWidth;
        onclick := myClick1;
        Visible := true;

      end;

      Form2.myClick(checkm[i]); // 调用checkbox点击事件，改变文本样式

      j := j + iHtoH;

    end;

    // a:=b;
  end
  else
    b := 0;
end;

procedure TForm2.myClick(Sender: TObject); // checkbox值改变时触发的事件
var
  i: integer;

begin

  i := StrToIntdef(copy(tcheckbox(Sender).Name, 6, 2), 0); // 取checkbox控件序号

  if checkm[i].ischecked = true then
  begin
    editm[i].styledsettings := editm[i].styledsettings - [TStyledSetting.style,
      TStyledSetting.FontColor, TStyledSetting.Other];
    editm[i].TextSettings.Font.style := [TFontStyle.fsstrikeout]; // 添加删除线
    editm[i].TextSettings.FontColor := TAlphaColors.gray; // 设置字体为灰色
  end
  else
  begin
    editm[i].styledsettings := editm[i].styledsettings - [TStyledSetting.style,
      TStyledSetting.FontColor, TStyledSetting.Other];
    editm[i].TextSettings.Font.style := []; // 取消删除线
    editm[i].TextSettings.FontColor := TAlphaColors.black; // 设置字体为黑色
  end;

end;

procedure TForm2.myClick1(Sender: TObject); // 点击edit时触发的事件
var
  i: integer;

begin
  i := StrToIntdef(copy(tedit(Sender).Name, 5, 2), 0);

  if (i > 9) and (VertScrollBox1.height > 592) then
  begin
    VertScrollBox1.height := 400;
    VertScrollBox1.scrollby(0, 370 - editm[i].position.y);
    editm[i].setfocus;
  end;

end;

end.
