var
  Stocks:string;
  
procedure InitIni;
var
  lst:TStrings;
begin
  Stocks:=GetSetting('Stocks','sh000001,sz399001');
  lst:=TStringList.create;
  try
    lst.text:=StrToStrings(Stocks,',');
    chksh.checked:=lst.IndexOf('sh000001')>=0;
    chksz.checked:=lst.IndexOf('sz399001')>=0;
    chknew.checked:=lst.IndexOf('sh000017')>=0;
    chksmall.checked:=lst.IndexOf('sz399101')>=0;
    chkhs300.checked:=lst.IndexOf('sh000300')>=0;
    chksh180.checked:=lst.IndexOf('sh000010')>=0;
    chksh50.checked:=lst.IndexOf('sh000016')>=0;
    chksz100r.checked:=lst.IndexOf('sz399004')>=0;
    
    edtRowCount.text:=GetSetting('RowCount','3');
    edtCommission.text:=GetSetting('Commission','0.27');
    edtCalico.text:=GetSetting('Calico','0.1');
    edtOtherfee.text:=GetSetting('Otherfee','6');
  finally
    lst.free;
  end;
end;

{  sh000001,上证指数
  sz399001,深证成指
  sh000017,新综指
  sz399101,中小板指
  sh000300,沪深300
  sh000010,上证180
  sh000016,上证50
  sz399004,深证100R}
  
procedure SaveIni;
var
  lst:TStrings;
  i:integer;
begin
  lst:=TStringList.create;
  try
    lst.text:=StrToStrings(Stocks,',');
    i:=lst.IndexOf('sh000001');
    if chksh.checked and (i<0) then 
    begin
      lst.add('sh000001');
    end else if not chksh.checked and (i>=0) then 
    begin
      lst.delete(i);
    end;
    i:=lst.IndexOf('sz399001');
    if chksz.checked and (i<0) then 
    begin
      lst.add('sz399001');
    end else if not chksz.checked and (i>=0) then 
    begin
      lst.delete(i);
    end;
    i:=lst.IndexOf('sh000017');
    if chknew.checked and (i<0) then 
    begin
      lst.add('sh000017');
    end else if not chknew.checked and (i>=0) then 
    begin
      lst.delete(i);
    end;
    i:=lst.IndexOf('sz399101');
    if chksmall.checked and (i<0) then 
    begin
      lst.add('sz399101');
    end else if not chksmall.checked and (i>=0) then 
    begin
      lst.delete(i);
    end;
    i:=lst.IndexOf('sh000300');
    if chkhs300.checked and (i<0) then 
    begin
      lst.add('sh000300');
    end else if not chkhs300.checked and (i>=0) then 
    begin
      lst.delete(i);
    end;
    i:=lst.IndexOf('sh000010');
    if chksh180.checked and (i<0) then 
    begin
      lst.add('sh000010');
    end else if not chksh180.checked and (i>=0) then 
    begin
      lst.delete(i);
    end;
    i:=lst.IndexOf('sh000016');
    if chksh50.checked and (i<0) then 
    begin
      lst.add('sh000016');
    end else if not chksh50.checked and (i>=0) then 
    begin
      lst.delete(i);
    end;
    i:=lst.IndexOf('sz399004');
    if chksz100r.checked and (i<0) then 
    begin
      lst.add('sz399004');
    end else if not chksz100r.checked and (i>=0) then 
    begin
      lst.delete(i);
    end;
    Stocks:=StringsToStr(lst.text,',');
    SetSetting('Stocks',Stocks);
    SetSetting('RowCount',edtRowCount.text);
    SetSetting('Commission',edtCommission.text);
    SetSetting('Calico',edtCalico.text);
    SetSetting('Otherfee',edtOtherfee.text);
  finally
    lst.free;
  end;
end;

procedure OnSettingInit;
begin
  Stocks:='';
  BeginUpdate;
  InitIni;
  EndUpdate;
end;

//--- 按下设置窗口的确定按钮时，触发此事件 ---
//通过设置返回值来改变是否允许确认,当result为False时不关闭设置窗口
function OnSettingClose(iResult);
var
  i:integer;
begin
  result:=true;
  try
    i:=StrToInt(edtRowCount.text);
    if (i<3) or (i>5) then
      raise('');
  except
    showmsg('无效的条数！');
    result:=false;
  end;
  
  if result then SaveIni;
end;

