const
  RowHeight=33;

var
  http,httpImage:TxsHttp;
  xshttp:TxsHttp;
  tmr:TTimer;
  lstText,lstHint,lstUp:TStrings;
  BaseIndex,PageIndex,PageCount,FloatBarIndex:integer;
  EveryPageCount:integer;
  lbls,imgs;
  Stocks,StockNo:string;
  sMoney,sStockDeal:string;
  edtNo:TEdit;
  edtMoney:TEdit;
  edtStockNo:TEdit;
  Commission,Calico,Otherfee:real;
  rbsh,rbsz:TRadioButton;
  frm:TForm;
  GetStockNameValue:string;
  GetStockNameing:boolean;
  ShowChart:boolean;
  sumDeal:real;
  miDel,miEdit:TMenuItem;
  pmenu:TPopupMenu;
  SelIndex:Integer;

//--- PopMenu 相关函数 ----------------------------

function IsExponent(sValue:string):Boolean;
var
  lst:TStrings;
begin
  lst := TStringList.create;
  try
    lst.LoadFromFile(AppPath+'Widgets\8jie Stocks\Stocks.dat');
    lst.text :=UTF8Decode(lst.text);
    if lst.IndexOf(sValue) >= 0 then result:=True
    else Result := False;
  finally
    lst.free;
  end;
end;

//系统默认已经定义了__PopupMenu的组件,只需直接调用即可
function PopMenu_Add(aCaption:string; aFunctionName:string; aTag:integer):TMenuItem;
begin
  result:=nil;
  if trim(aCaption)='' then exit;
  result:=TMenuItem.Create(Widget);
  result.caption:=aCaption;
  result.OnClick:=aFunctionName;
  result.Tag:=aTag;
  __PopupMenu.items.Add(result);
end;

procedure PopMenu_AddChild(aParentMenu:TMenuItem; aCaption:stirng; aFunctionName:string; aTag:integer);
var
  mi:TMenuItem;
begin
  if aParentMenu=nil then exit;
  mi:=TMenuItem.Create(Widget);
  mi.caption:=aCaption;
  mi.OnClick:=aFunctionName;
  mi.Tag:=aTag;
  aParentMenu.add(mi);
end;

procedure PopMenu_Clear;
begin
  __PopupMenu.Items.Clear;
end;
    
procedure PopMenu_Popup;
begin
  __PopupMenu.Popup(Mouse.CursorPos.x,Mouse.CursorPos.y);
end;
//-------------------------------------------------

procedure RefreshPos;
const
  lpos=313;
var
  ipos:integer;
begin
  BeginUpdate;
  imgPreview.visible:=ShowChart;
  imgFlyoutTop.visible:=ShowChart;
  imgFlyoutBottom.visible:=ShowChart;
  lblFloatTitle.visible:=ShowChart;
  imgChart.visible:=ShowChart;

  imgbg.Height:=43+RowHeight*EveryPageCount;
  if ShowChart then
    imgbg.left:=lpos
  else
    imgbg.left:=lpos;
  ipos:=imgbg.left;
  
  imgFloat.left:=ipos+2;
  imgLine.left:=ipos+4;
  imgNav.left:=ipos+26;
  imgAdd.left:=ipos+28;
  imgUp.left:=ipos+57;
  imgDown.left:=ipos+86;
  imgRefresh.left:=ipos+108;
  imgShowChart.left:=ipos+3;
  imgDel.left:=ipos+110;
  lblHint.left:=ipos+10;
  for i:=0 to EveryPageCount-1 do
  begin
    lbls[i].left:=ipos;
    imgs[i].left:=ipos;
  end;

  imgLine.top:=imgbg.height-33;
  imgNav.top:=imgLine.top+6;
  imgAdd.top:=imgLine.top+8;
  imgUp.top:=imgLine.top+8;
  imgDown.top:=imgLine.top+8;
  imgRefresh.top:=imgLine.top+16;
  imgShowChart.top:=imgLine.top+16;
  lblHint.top:=imgLine.top+2;
  
  //flyout set
  imgFlyoutTop.left:=lpos-5;
  imgFlyoutBottom.left:=imgFlyoutTop.left;
  if imgbg.height<imgPreview.height-10 then
  begin
    imgFlyoutBottom.FileName:=WidgetPath+'images\flyoutBgRightBottom1.png';
    imgFlyoutTop.Height:=imgbg.height-5;
    imgFlyoutBottom.top:=imgFlyoutTop.top+imgFlyoutTop.height;
    imgFlyoutBottom.height:=237-imgFlyoutBottom.top;
  end
  else
  begin
    imgFlyoutBottom.FileName:=WidgetPath+'images\flyoutBgRightBottom2.png';
    imgFlyoutTop.Height:=236;
    imgFlyoutBottom.top:=imgFlyoutTop.top+imgFlyoutTop.height;
    imgFlyoutBottom.height:=imgbg.height+5-imgFlyoutTop.Height+6;
    if imgbg.height<=230 then
    begin
      imgFlyoutBottom.height:=imgFlyoutBottom.height+10;
    end;
  end;
  if ShowChart then
  begin
    h:=50+EveryPageCount*RowHeight;
    if h<imgPreview.height then h:=imgPreview.height;
    if h<(imgFlyoutBottom.top+imgFlyoutBottom.height) then h:=imgFlyoutBottom.top+imgFlyoutBottom.height;
    Widget.height:=h;
  end
  else
    Widget.height:=45+EveryPageCount*RowHeight;
  EndUpdate;
  Widget.Show;

  //RefreshSidebarOrder;
end;

procedure SwitchChart;
begin
  ShowChart:=not ShowChart;
  imgFloat.visible:=ShowChart;
  RefreshPos;
end;

//显示股票信息
procedure DrawStock;
var
  aTop:integer;
begin
  aTop:=7;
  BeginUpdate;
  if lstText.count<=0 then 
  begin
    for i:=0 to EveryPageCount-1 do
    begin
      lbls[i].top:=i*RowHeight+aTop;
      lbls[i].visible:=false;
      imgs[i].visible:=false;
    end;
    EndUpdate;
    exit;
  end;
  if BaseIndex>=lstText.count then
  begin
    PageIndex:=0;
    BaseIndex:=0;
  end;
  
  for i:=0 to EveryPageCount-1 do
  if (i+BaseIndex<lstText.count) then
  begin
    lbls[i].caption:=lstText.strings[i+BaseIndex];
    lbls[i].hint:=lstHint.strings[i+BaseIndex];
    imgs[i].NeedRefresh:=true;
    if lstUp.strings[i+BaseIndex]='1' then
    begin
      lbls[i].visible:=true;
      lbls[i].top:=i*RowHeight+aTop+2;
      lbls[i].FontStyle.font.color:=$4A35F0;
      //lbls[i].HotFont:=',,$4A35F0,,,,1';
      imgs[i].visible:=true;
      imgs[i].filename:=WidgetPath+'images\up.png';
    end
    else if lstUp.strings[i+BaseIndex]='-1' then
    begin
      lbls[i].visible:=true;
      lbls[i].top:=i*RowHeight+aTop;
      lbls[i].FontStyle.font.color:=$BCFF3C;
      imgs[i].visible:=true;
      imgs[i].filename:=WidgetPath+'images\down.png';
    end
    else
    begin
      lbls[i].top:=i*RowHeight+aTop+2;
      lbls[i].FontStyle.font.color:=$FFFFFF;
      lbls[i].visible:=true;
      imgs[i].visible:=false;
    end;
  end
  else
  begin
    lbls[i].top:=i*33+aTop;    
    lbls[i].visible:=false;
    imgs[i].visible:=false;
  end;
  imgUp.Enabled:=(BaseIndex>0);
  imgDown.Enabled:=(BaseIndex<lstText.count-EveryPageCount);
  EndUpdate;
end;

//返回相同串的位置
function StrNumber(sCompare:string;sValue:TStrings):Integer;
var
  i:Integer;
  s:string;
begin
  result:=-1;
  for i:= 0 to sValue.count-1 do
  begin
    s:=sValue.strings[i];
    s:=Trim(Copy(s,0,Pos(';',s)-1));
    if s = trim(sCompare) then
    begin
      result:=i;
      break;
    end;
  end;
end;

//分析字符串中的内容，取出有用的信息
function ParseStr(sCompare:string;sValue:TStrings;var fResult:real):Boolean;
var
  i:Integer;
  s:string;
begin
  Result := False;
  fResult:=0;
  for i:= 0 to sValue.Count -1 do
  begin
    s:=sValue.strings[i];
    s:=trim(Copy(s,0,Pos(';',s)-1));
    if s = trim(sCompare) then
    begin
      s:=copy(sValue.strings[i],Pos(';',sValue.strings[i])+1,length(sValue.strings[i]));
      try
        result := True;
        fresult := strtofloat(trim(s));
      except
        result:=false;
        fresult := 0;
      end;
      break;
    end;
  end;
end;

//分析网页内容，取出最新信息
procedure ParseHtml(value:string);
var
  lst,lstItem,lstStocks,lstMoney,lstDeal:TStrings;
  fnew,fend,f,fpercent,ShowDeal:Float;
  fbfmoney,fnowmoney,fdeal:Float;
  bShowProfit:Boolean;
  s1:real;
begin
  lstText.clear;
  lstHint.clear;
  lstUp.clear;
  BeginUpdate;
  lst:=TStringList.create;
  lstItem:=TStringList.create;
  lstStocks:=TStringList.create;
  lstMoney:=TStringList.create;
  lstDeal:=TStringList.Create;
  sumDeal:=0;

  try
    lstStocks.text:=StrToStrings(Stocks,',');
    lstMoney.text:=StrToStrings(sMoney,',');
    lstDeal.text:=StrToStrings(sStockDeal,',');
    lst.text:=value;
//    showmsg(value);
    if lst.count>0 then
      for i:=0 to lst.count-1 do
        lst.strings[i]:=strBetween(lst.strings[i],'"','"');
    //lst.savetofile(WidgetPath+'data.htm');
{0名称	 1今开盘  2昨收盘  3最新价  4最高价  5最低价       8成交量   9成交额                                            30日期    31时间
上证指数,4055.834,4046.392,4001.945,4069.846,4001.324,0,0,73062685,98780799184,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2007-05-15,11:31:17
深证成指,11754.698,11732.079,11653.380,11828.927,11653.241,0.000,0.000,529839202,9979433470.840,0,0.000,0,0.000,0,0.000,0,0.000,0,0.000,0,0.000,0,0.000,0,0.000,0,0.000,0,0.000,2007-05-15,11:31:11
0名称   1最新价 2涨跌额 3涨跌幅
道琼斯,13346.78,20.56,0.15%
纳斯达克,2546.44,-15.78,-0.62%}
    //上证指数
    if lst.count>0 then
    for i:=0 to lst.count-1 do
    begin
      lstItem.text:=StrToStrings(lst.strings[i],',');
      if lstItem.count>=8 then
      begin
        try
          fnew:=0.0; fend:=0.0;
          f:=0.0; fpercent:=0.0;
          fnew:=StringToFloat(lstItem.strings[3],0.0);
          fend:=StringToFloat(lstItem.strings[2],0.0);
          f:=fnew-fend;
          if (fend<>0) and (fnew<>0) then
            fpercent:=(fnew*100-fend*100)/fend
          else
            fpercent:=0;
        except
        
        end;
        if fpercent>0 then 
          lstUp.add('1')
        else if fpercent<0 then
          lstUp.add('-1')
        else lstUp.add('0');
        if fnew=0 then
          s:=format('%s'#13#10'-',[lstItem.strings[0]])
        else
          s:=format('%s'#13#10'%s(%f%s)',[lstItem.strings[0],lstItem.strings[3],fpercent,'%']);
        //showmsg(lstItem.text);
        lstText.Add(s);
	try
	  ShowDeal:=0;
          fnowmoney:=strtofloat(lstItem.strings[3]);
	  bShowProfit:=ParseStr(lstStocks.strings[i],lstmoney,fbfmoney);
	  ParseStr(lstStocks.strings[i],lstdeal,fdeal);
	  ShowDeal:=(fnowmoney*fdeal)-(fbfmoney*fdeal+fbfmoney*fdeal*Commission/100+fbfmoney*fdeal*Calico/100+Otherfee+
	            fnowmoney*fdeal*Commission/100+fnowmoney*fdeal*Calico/100+Otherfee);
          if (fnowmoney*fdeal<=0) then
            showdeal:=0;
	  s1:=trunc(showdeal*100)/100;
          sumdeal:=sumdeal+showdeal;
	except
	  s1:=0;
	end;
        if fnew=0 then
	begin
//          s:=format('%s(%s)'#13#10'涨跌幅:-'#13#10,[lstItem.strings[0],lstStocks.strings[i]]);
	  if bShowProfit then s:= format('%s(%s)'#13#10'涨跌幅:-'#13#10,[lstItem.strings[0],lstStocks.strings[i]])
            +format('涨跌额:-'#13#10'盈利:%.0m'#13#10'---------'#13#10'昨收盘:%s'#13#10'今开盘:%s'#13#10'最新价:%s'#13#10'最高价:%s'#13#10'最低价:%s',
              [lstitem.strings[2],s1,lstitem.strings[1],lstitem.strings[3],lstItem.strings[4],lstItem.strings[5]])
          else s:=format('%s(%s)'#13#10'涨跌幅:-'#13#10,[lstItem.strings[0],lstStocks.strings[i]])
            +format('涨跌额:-'#13#10'---------'#13#10'昨收盘:%s'#13#10'今开盘:%s'#13#10'最新价:%s'#13#10'最高价:%s'#13#10'最低价:%s',
              [lstitem.strings[2],lstitem.strings[1],lstitem.strings[3],lstItem.strings[4],lstItem.strings[5]])
        end else
	begin
//          s:=format('%s(%s)'#13#10'涨跌幅:%f%s'#13#10,[lstItem.strings[0],lstStocks.strings[i],fpercent,'%']);
	  if bShowProfit then s:=format('%s(%s)'#13#10'涨跌幅:%f%s'#13#10,[lstItem.strings[0],lstStocks.strings[i],fpercent,'%'])
            +format('涨跌额:%f'#13#10'盈利:%.m'#13#10'---------'#13#10'昨收盘:%s'#13#10'今开盘:%s'#13#10'最新价:%s'#13#10'最高价:%s'#13#10'最低价:%s'#13#10'成交额:%.0m',
              [f,s1,lstitem.strings[2],lstitem.strings[1],lstitem.strings[3],lstItem.strings[4],lstItem.strings[5],StrToFloat(lstItem.strings[9])])
          else s:=format('%s(%s)'#13#10'涨跌幅:%f%s'#13#10,[lstItem.strings[0],lstStocks.strings[i],fpercent,'%'])
	    +format('涨跌额:%f'#13#10'---------'#13#10'昨收盘:%s'#13#10'今开盘:%s'#13#10'最新价:%s'#13#10'最高价:%s'#13#10'最低价:%s'#13#10'成交额:%.0m',
              [f,lstitem.strings[2],lstitem.strings[1],lstitem.strings[3],lstItem.strings[4],lstItem.strings[5],StrToFloat(lstItem.strings[9])])
        end;
        lstHint.add(s);
      end;
    {end
    begin
      lstUp.add('0');
      lstText.Add('');
      lstHint.add('');}
    end;
    PageCount:=(lstText.count-1) div EveryPageCount+1;
    //if (lstText.count mod EveryPageCount>0) then PageCount:=PageCount+1;
  finally
    lblHint.caption:=format('总盈利:%.0m',[trunc(sumdeal*100)/100]);
    lstStocks.free;
    lstMoney.free;
    lstDeal.free;
    lst.free;
    lstItem.free;
  end;
  //lblInfo.caption:='获取信息成功！'
  DrawStock;
  EndUpdate;
end;

procedure HttpDone(url,result,isString);
begin
  if result>'' then
    ParseHtml(result);
  //showtrayhint('警告','获取信息成功！');
end;

procedure HttpGetError(url, isString);
begin
  //showtrayhint('警告','获取信息失败！');
end;

procedure HttpImageDone(url,result,isString);
begin
  imgChart.FileName:=result;
  //showtrayhint('警告','获取图片成功！');
end;

procedure HttpGetImageError(url, isString);
begin
  //showtrayhint('警告','获取图片失败！'+url);
end;

//查询股票信息，如果查不到说明没有此数据
procedure GetInfo(aParams:string);
begin
  //aurl:='http://hq.sinajs.cn/list=sh000001,sz399001,sh000017,sz399101,sh000300,sh000010,sh000016,sz399004,sh000003,sz399003,sh000012,int_dji,int_nasdaq,int_ftse,int_nikkei,int_hangseng,int_sp500,int_dax30';
  //aurl:='http://hq.sinajs.cn/list=sh000001,sz399001,sh000011,sz399305,sh000017,sz399101,sh000300,sh000010,sh000016,sz399004';
  //aurl:='http://hq.sinajs.cn/list=sh000001,sz399001,sh000017,sz399101,sh000300,sh000010,sh000016,sz399004';
  aurl:='http://hq.sinajs.cn/list='+Stocks;
  //查询股票信息：http://hq.sinajs.cn/list=sh600000
  //如果没查到信息，双引号中间的值为空
  //function GetHTTP(aUrl:string; isString:boolean=true; aParams:string=''):string;
  http.GetHTTP(aurl,true,'');
end;

procedure GetChart;
var
  fn:stirng;
begin
// http://image.sinajs.cn/newchart/min/n/sh000001.gif
// http://image.sinajs.cn/newchart/small/bsh000001.gif
  aurl:='http://image.sinajs.cn/newchart/small/b'+StockNo+'.gif';
  fn:=format('FileName=%simages\chart\%s.gif',[WidgetPath,StockNo]);
  if FileExists(fn) then imgChart.FileName:=fn
    else imgChart.FileName:='';
  httpImage.GetHTTP(aurl,false,fn);
end;

procedure RefreshInfo;
begin
  GetInfo('');
end;

procedure PrevPage;
begin
  PageIndex:=PageIndex-1;
  if PageIndex<0 then PageIndex:=0;
  if BaseIndex<>PageIndex*EveryPageCount then
  begin
    BaseIndex:=PageIndex*EveryPageCount;
    DrawStock;
  end;
end;

procedure NextPage;
begin
  PageIndex:=PageIndex+1;
  if PageIndex>=PageCount-1 then PageIndex:=PageCount-1;
  if BaseIndex<>PageIndex*EveryPageCount then
  begin
    BaseIndex:=PageIndex*EveryPageCount;
    DrawStock;
  end;
end;

procedure tmrOnTimer(sender);
var
  h,m,s:integer;
  dt:TDateTime;
begin
  dt:=frac(now);
  //DecodeTime(now,h,m,s,s);
 {股市时间：早9点半到11点半，下午1点到3点
  09:30 0.3958333333
  11:30 0.4791666666
  13:00 0.5416666666
  15:00 0.625}
  if (0.3958>dt) or (dt>0.625) then
  begin//在工作日之外，改为10分钟刷新一次
    if tmr.Interval<>600000 then tmr.Interval:=600000;
  end
  else if ((0.3958<=dt) and (dt<=0.4792)) or ((0.5416<=dt) and (dt<=0.625)) then
  begin//在刷新时间，改为每秒刷新一次
    if tmr.Interval<>10000 then tmr.Interval:=10000;
  end
  else//午休时间，改为每1分钟刷新一次
  begin
    if tmr.Interval<>60000 then tmr.Interval:=60000;
  end;
  RefreshInfo;
end;

//读取当前Widget的INI设置
procedure InitIni;
begin
  EveryPageCount:=GetSettingInt('RowCount',3);
  Commission:=GetSettingFloat('Commission',0);
  Calico:=GetSettingFloat('Calico',0);
  Otherfee:=GetSettingFloat('Otherfee',0);
  //Stocks:=GetSetting('Stocks','sh000001,sz399001,sh000011');
  Stocks:=GetSetting('Stocks','sh000001,sz399001');
  sMoney:=GetSetting('Money','sz399001;0');
  sStockDeal:=GetSetting('StockDeal','sz399001;0');
{  sh000001,上证指数
  sz399001,深证成指
  sh000017,新综指
  sz399101,中小板指
  sh000300,沪深300
  sh000010,上证180
  sh000016,上证50
  sz399004,深证100R}
end;

//初始化动态菜单
procedure InitMenu;
begin
  pmenu:=TPopupMenu.Create(Widget);
  pmenu.AutoHotkeys:=maManual;


  miDel:=TMenuItem.Create(Widget);
  miDel.Caption:='删除';
  miDel.onclick:='miDelOnClick';
  pmenu.Items.Insert(0,miDel);

  miEdit:=TMenuItem.Create(Widget);
  miEdit.Caption:='编辑';
  miEdit.onclick:='miEditOnClick';
  miEdit.default:=true;
  pmenu.Items.Insert(0,miEdit);
end;

//--- Widget移入边栏事件，当Widget被移到边栏上时会触发此事件 ---
procedure OnWidgetDock;
begin
  BeginUpdate;
  //Widget.Width:=136;
  EndUpdate;
end;

//--- Widget移出边栏事件，当Widget被移出边栏时会触发此事件 ---
procedure OnWidgetUnDock;
begin
  BeginUpdate;
  //Widget.Width:=140;
  EndUpdate;
end;

//--- Widget初始化事件，当打开Widget时会触发此事件 ---
procedure OnWidgetInit;
begin
  ShowChart:=false;
  StockNo:='';
  FloatBarIndex:=0;
  lstText:=TStringList.create;
  lstHint:=TStringList.create;
  lstUp:=TStringList.create;
  PageIndex:=0;
  BaseIndex:=0;
  PageCount:=1;
  SelIndex := -1;
  InitIni;
  initMenu;
  lbls:=[lblInfo0,lblInfo1,lblInfo2,lblInfo3,lblInfo4];
  for i:=0 to EveryPageCount-1 do
  begin
    lbls[i].tag:=i;
    lbls[i].OnMouseEnter:='lblMouseEnter';
    lbls[i].PopupMenu:=pmenu;
    //lbls[i].OnMouseLeave:='lblMouseLeave';
    lbls[i].OnClick:='OnStockClick';
    SetAllowMoveWidget(lbls[i],false);
  end;
  imgs:=[imgState0,imgState1,imgState2,imgState3,imgState4];
  imgDel.OnClick:='imgDelClick';
  //imgAdd.Enabled:=false;
  BeginUpdate;
  RefreshPos;
  if Docking then 
  	OnWidgetDock;
  EndUpdate;

  http:=TxsHttp.Create(nil);
  http.OnHTTPGetFailed:='HttpGetError';
  http.OnHttpGetDone:='HttpDone';
  
  httpImage:=TxsHttp.Create(nil);
  httpImage.OnHTTPGetDone:='HttpImageDone';
  httpImage.OnHTTPGetFailed:='HttpGetImageError';
  
  RefreshInfo;

  tmr:=TTimer.create(Widget);
  tmr.Interval:=10000;
  tmr.OnTimer:='tmrOnTimer';
  //tmr.Enabled:=false;
  AutoAdjustMem:=0;
end;

//--- Widget关闭事件，当关闭Widget时会触发此事件 ---
procedure OnWidgetClose;
begin
  lstUp.free;
  lstHint.free;
  lstText.free;
  if http<>nil then
  begin
  	http.free;
  	http:=nil;
  end;
  if httpImage<>nil then
  begin
    httpImage.free;
    httpImage:=nil;
  end;
  ScriptFinished:=true;
end;

procedure lblMouseLeave(sender);
begin
  if sender<>nil then
  begin
    BeginUpdate;
    //imgDel.visible:=false;
    EndUpdate;
  end;
end;

procedure lblMouseEnter(sender);
begin
  if sender<>nil then
  begin
    BeginUpdate;
    if (0<=imgDel.tag) and (imgDel.tag<=EveryPageCount-1) then
    begin
      lbls[imgDel.tag].NeedRefresh:=true;
      imgDel.top:=sender.top+4;
      imgDel.tag:=sender.tag;
      imgDel.visible:=true;
      SelIndex:=sender.tag;
    end;
    EndUpdate;
  end;
end;

procedure imgDelClick(sender);
var
  lst,lstMoney,lstDeal:TStrings;
  Number:Boolean;
begin
  if (0<=imgDel.tag) and (imgDel.tag<=EveryPageCount-1) then
  begin
    i:=BaseIndex+imgDel.tag;
    if i<lstText.count then
    begin
      lstText.delete(i);
      lstUp.delete(i);
      lstHint.delete(i);
      lst:=TStringList.create;
      lstMoney:=TStringlist.create;
      lstDeal:=TStringlist.create;
      try
        lst.text:=StrToStrings(Stocks,',');
	      lstMoney.text:=strtostrings(sMoney,',');
	      lstDeal.text:=strtostrings(sStockDeal,',');
        //showmsg(lst.text);
	      Number:=StrNumber(lst.strings[i],lstmoney);
	      if Number<>-1 then lstMoney.delete(i);
	      Number:=StrNumber(lst.strings[i],lstDeal)
	      if Number<>-1 then lstDeal.delete(i);
        if i<lst.count then lst.delete(i);
	      sMoney:=StringsToStr(lstMoney.text,',');
	      sStockDeal:=StringsToStr(lstDeal.text,',');
        Stocks:=StringsToStr(lst.text,',');
	      SetSetting('Money',sMoney);
	      SetSetting('StockDeal',sStockDeal);
        SetSetting('Stocks',Stocks);
        PageCount:=(lstText.count-1) div EveryPageCount+1;
      finally
        lst.free;
	      lstmoney.free;
	      lstdeal.free;
      end;
      DrawStock;
    end;
  end;
end;

procedure lblFloatTitleClick1;
begin
  if trim(lblFloatTitle.hint)<>'' then
    ShellExecute(0,'open',lblFloatTitle.hint,'','',1);
end;

procedure OnStockClick(sender);
var
  lst:TStrings;
begin
  lst:=TStringList.create;
  try
    lst.text:=sender.hint;
    if lst.count>0 then
    begin
      s:=StrBetween(lst.strings[0],'(',')');
      StockNo:=s;
      if (FloatBarIndex<>sender.tag) or not ShowChart then
      begin
        BeginUpdate;
        lbls[FloatBarIndex].NeedRefresh:=true;
        imgFloat.top:=sender.top;
        FloatBarIndex:=sender.tag;
        lblFloatTitle.caption:=lst.strings[0];
        lblFloatTitle.FontStyle.Font.Color:=sender.FontStyle.Font.Color;
        lblFloatTitle.hint:=format('http://finance.sina.com.cn/realstock/company/%s/nc.shtml',[s]);
        //lblFloatTitle.FontStyle.font.color:=TxspLabel(sender).FontStyle.font.color;
        
        EndUpdate;
        GetChart;
        if not ShowChart then SwitchChart;
      end;
//      if s<>'' then
//          ShellExecute(0,'open',format('http://finance.sina.com.cn/realstock/company/%s/nc.shtml',[s]),nil,nil,1);
    end;
  finally
    lst.free;
  end;
end;

procedure xsHttpGetDone(url,result,isString);
begin
  GetStockNameValue:=result;
  GetStockNameing:=false;
  if xshttp<>nil then
  begin
  	xshttp.free;
  	xshttp:=nil;
//    showmsg('done free');
  end;
  ScriptFinished:=true;
end;

procedure xsHttpGetError(url, isString);
begin
  GetStockNameing:=false;
  if xshttp<>nil then
  begin
  	xshttp.free;
  	xshttp:=nil;
//    showmsg('error free');
  end;
  ScriptFinished:=true;
end;

//通过股票代码取个股的名称，如果为空表示代码无效
function GetStockName(value:string):string;
//var
//  dt:TDateTime;
begin
  xshttp:=TxsHttp.Create(Widget);
  xshttp.OnHTTPGetFailed:='xsHttpGetError';
  xshttp.OnHTTPGetDone:='xsHttpGetDone';
  GetStockNameValue:='';
  //dt:=now;
  GetStockNameing:=true;
  xshttp.GetHTTP('http://hq.sinajs.cn/list='+value,true,'');
  while GetStockNameing do
  begin
    //sleep(20);
    application.ProcessMessages;
    //if (now-dt)
  end;
  result:=GetStockNameValue;
end;

procedure btnOkClick(sender);
var
  s,str:string;
begin
  if edtNo<>nil then
  begin
    if (length(edtNo.text)<>6) and (frm<>nil) then
    begin
      frm.ModalResult:=0;
      showmsg('无效的股票代码！');
      exit;
    end;
    try
      i:=StrToInt(edtNo.text);
    except
      if (frm<>nil) then
        frm.ModalResult:=0;
      showmsg('无效的股票代码！');
      exit;
    end;
    str:=copy(edtNo.text,1,2);
    if str='60' then rbsh.checked:=true
      else if str='00' then rbsz.checked:=true;  
    if rbsh.checked then s:='sh'
      else s:='sz';
    s:=s+edtNo.text;
    frm.caption:='添加股票(查询股票编号...)';
    s:=GetStockName(s);
    frm.caption:='添加股票';
    s:=strBetween(s,'"','"');
    if s='' then
    begin
      if (frm<>nil) then
        frm.ModalResult:=0;
      showmsg('无效的股票代码！');
    end;
  end;
end;

procedure AddStock;
var
  btnOK,btnCancel:TButton;
  lblNo,lblMoney,lblStockNo:TLabel;
  rdgShowType:TRadioGroup;
  lstMoney,lstDeal:TStrings;
  s1,s2:string;
begin
  frm:=TForm.create(nil);
  frm.width:=230;
  frm.height:=180;
  frm.Position:=poScreenCenter;
  frm.caption:='添加股票';
  if screen.fonts.indexof('微软雅黑')>=0 then
    frm.font.name:='微软雅黑'
  else
    frm.font.name:='宋体';
  frm.font.size:=9;
  frm.BorderStyle:=bsDialog;
  try
    rbsh:=TRadioButton.create(frm);
    rbsh.left:=16; rbsh.top:=16;
    rbsh.parent:=frm;
    rbsh.caption:='沪市';
    rbsh.checked:=true;
    
    rbsz:=TRadioButton.create(frm);
    rbsz.left:=96; rbsz.top:=16;
    rbsz.parent:=frm;
    rbsz.caption:='深市';
    
    lblNo:=TLabel.create(frm);
    lblNo.parent:=frm;
    lblNo.Caption:='请输入股票代码：';
    lblNo.left:=16; lblNo.top:=40;
    
    edtNo:=TEdit.create(frm);
    edtNo.parent:=frm;
    edtNo.left:=120; edtNo.top:=38;
    edtNo.width:=80;
    edtNo.height:=16;
    edtNo.TabOrder:=0;

    lblmoney :=tlabel.create(frm);
    lblmoney.parent := frm;
    lblmoney.caption := '买入价：';
    lblmoney.left := 66; lblmoney.top := 67;

    edtMoney:=TEdit.create(frm);
    edtMoney.parent:=frm;
    edtmoney.left := 120;
    edtmoney.top:= 65;
    edtmoney.width := 80;
    edtmoney.height:=16;
    edtmoney.taborder:=1;

    lblstockno := tlabel.create(frm);
    lblstockno.parent := frm;
    lblstockno.caption := '股票份数：';
    lblstockno.left := 52; lblstockno.top := 94;

    edtstockno:=TEdit.create(frm);
    edtstockno.parent:=frm;
    edtstockno.left := 120;
    edtstockno.top := 92;
    edtstockno.width:=80;
    edtstockno.height:=16;
    edtstockno.taborder:=2;

    btnCancel:=TButton.create(frm);
    btnCancel.caption:='取  消';
    btnCancel.left:=130;
    btnCancel.top:=123;
    btnCancel.parent:=frm;
    btnCancel.ModalResult:=2;
    btnCancel.cancel:=true;
    btnCancel.cancel:=true;
    
    btnOK:=TButton.create(frm);
    btnOK.caption:='确  定';
    btnOK.left:=50;
    btnOK.top:=123;
    btnOK.parent:=frm;
    btnOK.ModalResult:=1;
    btnOk.OnClick:='btnOkClick';
    btnOK.default:=true;
    
    if frm.ShowModal=1 then
    begin
      if rbsh.checked then s:='sh'
        else s:='sz';
      s:=s+edtNo.text;
      s1 :=edtMoney.text;
      s2:=edtstockno.text;

      lst:=TStringList.create;
      lstMoney:=TStringList.create;
      lstDeal:=TStringList.create;
      try
        lst.text:=StrToStrings(Stocks,',');

	      lstmoney.text:=strtostrings(smoney,',');
	      lstdeal.text:=strtostrings(sStockDeal,',');
        i:=lst.indexof(s);
        if i<0 then
        begin
          lst.add(s);
      	  lstMoney.add(s+';'+s1);
      	  lstDeal.add(s+';'+s2);
      	  sMoney:=StringsToStr(lstMoney.text,',');
      	  sStockDeal:=StringsToStr(lstDeal.text,',');
          Stocks:=StringsToStr(lst.text,',');
	        SetSetting('Money',sMoney);
	        SetSetting('StockDeal',sStockDeal);
          SetSetting('Stocks',Stocks);
          RefreshInfo;
        end;
      finally
        lstmoney.free;
	lstdeal.free;
        lst.free;
      end;
    end;
  finally
    frm.free;
  end;
end;

//--- Widget事件，当打开鼠标移到Widget区域内时触发此事件 ---
procedure OnWidgetMouseEnter;
begin
  BeginUpdate;
  imgnav.visible:=true;
  //imgDel.visible:=imgnav.visible;
  imgAdd.visible:=imgnav.visible;
  imgUp.visible:=imgnav.visible;
  imgDown.visible:=imgnav.visible;
  lblHint.visible:=not imgnav.visible;
  EndUpdate;
end;

//--- Widget事件，当打开鼠标移出Widget区域时触发此事件 ---
procedure OnWidgetMouseLeave;
begin
  BeginUpdate;
  imgnav.visible:=false;
  imgDel.visible:=imgnav.visible;
  imgAdd.visible:=imgnav.visible;
  imgUp.visible:=imgnav.visible;
  imgDown.visible:=imgnav.visible;
  lblHint.visible:=not imgnav.visible;
  if ShowChart then SwitchChart;
  
  EndUpdate;
end;

//--- Widget鼠标滚轮事件，当在Widget上进行滚轮操作时会触发此事件 ---
procedure OnWidgetMouseWheel(WheelDelta);
begin
  if WheelDelta>0 then
  begin
    if BaseIndex>0 then 
    begin
      dec(BaseIndex);
      DrawStock;
    end;
  end
  else
  begin
    if BaseIndex<lstText.count-EveryPageCount then
    begin
      inc(BaseIndex);
      DrawStock;
    end;
  end;
  PageIndex:=(BaseIndex div EveryPageCount);
end;

procedure addMenuToStocks(sValue:string);
var
  lst:TStrings;
begin
  if sValue = '' then exit;
  lst := TStringList.Create;
  try
    lst.text:=StrToStrings(Stocks,',');
    lst.add(sValue);
    Stocks:=StringsToStr(lst.text,',');
    SetSetting('Stocks',Stocks);
  finally
    lst.free;
  end;
end;

procedure addIconMenuClick(sender:Tobject);
var
  i:Integer;
begin
//  showmsg(inttostr(TMenuItem(sender).tag));
  case TMenuItem(sender).tag of
    1:AddStock;
    2:addMenuToStocks('sh000001');
    3:addMenuToStocks('sz399001');
    4:addMenuToStocks('sh000017');
    5:addMenuToStocks('sh000010');
    6:addMenuToStocks('sh000016');
    7:addMenuToStocks('sz399101');
    8:addMenuToStocks('sh000300');
    9:addMenuToStocks('sz399004');
    10:addMenuToStocks('sh000002');
    11:addMenuToStocks('sh000003');
    12:addMenuToStocks('sh000011');
    13:addMenuToStocks('sz399002');
    14:addMenuToStocks('sz399003');

    15:addMenuToStocks('sz399200');
    16:addMenuToStocks('sz399190');
    17:addMenuToStocks('sz399210');
    18:addMenuToStocks('sz399150');
    19:addMenuToStocks('sz399137');
    20:addMenuToStocks('sz399134');
    21:addMenuToStocks('sz399120');
    22:addMenuToStocks('sz399139');
    23:addMenuToStocks('sz399230');
    24:addMenuToStocks('sz399130');
    25:addMenuToStocks('sz399132');
    26:addMenuToStocks('sz399138');
    27:addMenuToStocks('sz399170');

    -2:
      begin
        for i:= 2 to 14 do
	begin
	  TComponent(sender).tag := i;
	  addIconMenuClick(sender);
	end;
      end;

    -3:
      begin
        for i:= 15 to 27 do
	begin
	  TComponent(sender).tag := i;
	  addIconMenuClick(sender);
	end;
      end;
  end;
  RefreshInfo;
end;

procedure AddShortcut;
var
  p:TMenuItem;
begin
  PopMenu_Clear;
  PopMenu_Add('添加自定义股票...','addIconMenuClick',1);
  p:=PopMenu_Add('添加证券指数','',0);
  PopMenu_AddChild(p,'添加以下全部证券指数','addIconMenuClick',-2);
  PopMenu_AddChild(p,'-','',0);
  PopMenu_AddChild(p,'添加 上证指数','addIconMenuClick',2);
  PopMenu_AddChild(p,'添加 深证成指','addIconMenuClick',3);
  PopMenu_AddChild(p,'添加 新综指','addIconMenuClick',4);
  PopMenu_AddChild(p,'添加 上证180','addIconMenuClick',5);
  PopMenu_AddChild(p,'添加 上证50','addIconMenuClick',6);
  PopMenu_AddChild(p,'添加 中小板指','addIconMenuClick',7);
  PopMenu_AddChild(p,'添加 泸深300','addIconMenuClick',8);
  PopMenu_AddChild(p,'添加 深证100R','addIconMenuClick',9);
  PopMenu_AddChild(p,'添加 A股指数','addIconMenuClick',10);
  PopMenu_AddChild(p,'添加 B股指数','addIconMenuClick',11);
  PopMenu_AddChild(p,'添加 基金指数','addIconMenuClick',12);
  PopMenu_AddChild(p,'添加 成份A指','addIconMenuClick',13);
  PopMenu_AddChild(p,'添加 成份B指','addIconMenuClick',14);
  p:=PopMenu_Add('添加行业指数','',0);
  PopMenu_AddChild(p,'添加以下全部行业指数','addIconMenuClick',-3);
  PopMenu_AddChild(p,'-','',0);
  PopMenu_AddChild(p,'添加 地产指数','addIconMenuClick',15);
  PopMenu_AddChild(p,'添加 金融指数','addIconMenuClick',16);
  PopMenu_AddChild(p,'添加 服务指数','addIconMenuClick',17);
  PopMenu_AddChild(p,'添加 建筑指数','addIconMenuClick',18);
  PopMenu_AddChild(p,'添加 金属指数','addIconMenuClick',19);
  PopMenu_AddChild(p,'添加 造纸指数','addIconMenuClick',20);
  PopMenu_AddChild(p,'添加 采掘指数','addIconMenuClick',21);
  PopMenu_AddChild(p,'添加 医药指数','addIconMenuClick',22);
  PopMenu_AddChild(p,'添加 综企指数','addIconMenuClick',23);
  PopMenu_AddChild(p,'添加 制造指数','addIconMenuClick',24);
  PopMenu_AddChild(p,'添加 纺织指数','addIconMenuClick',25);
  PopMenu_AddChild(p,'添加 机械指数','addIconMenuClick',26);
  PopMenu_AddChild(p,'添加 IT指数','addIconMenuClick',27);
  PopMenu_Popup;
end;

procedure miDelOnClick(sender);
var
  lst,lstMoney,lstDeal:TStrings;
  Number:Boolean;
begin
  if (0<=SelIndex) and (SelIndex<=EveryPageCount-1) then
  begin
    i:=BaseIndex+SelIndex;
    if i<lstText.count then
    begin
      lstText.delete(i);
      lstUp.delete(i);
      lstHint.delete(i);
      lst:=TStringList.create;
      lstMoney:=TStringlist.create;
      lstDeal:=TStringlist.create;
      try
        lst.text:=StrToStrings(Stocks,',');
	lstMoney.text:=strtostrings(sMoney,',');
	lstDeal.text:=strtostrings(sStockDeal,',');
        //showmsg(lst.text);
	Number:=StrNumber(lst.strings[i],lstmoney);
	if Number<>-1 then lstMoney.delete(Number);
	Number:=StrNumber(lst.strings[i],lstDeal)
	if Number<>-1 then lstDeal.delete(Number);
        if i<lst.count then lst.delete(i);
	sMoney:=StringsToStr(lstMoney.text,',');
	sStockDeal:=StringsToStr(lstDeal.text,',');
        Stocks:=StringsToStr(lst.text,',');
	SetSetting('Money',sMoney);
	SetSetting('StockDeal',sStockDeal);
        SetSetting('Stocks',Stocks);
        PageCount:=(lstText.count-1) div EveryPageCount+1;
      finally
        lst.free;
	lstmoney.free;
	lstdeal.free;
      end;
      DrawStock;
    end;
  end;
end;

//判断股票是否已经存在
function StockIsEffective(sValue:string):Boolean;
var
  i,Number:integer;
  lst:TStrings;
begin
  Number:=BaseIndex+SelIndex;
  result := false;
  lst := TStringList.create;
  try
    lst.text:=StrToStrings(Stocks,',');
    for i:= 0 to lst.count - 1 do
    begin
      if i = Number then Continue;
      if lst.strings[i] = sValue then
      begin
        result := True;
        break;
      end;
    end;
  finally
    lst.free;
  end;
end;

//当股票代码改变时产生的事件
procedure edtNoChange(sender);
var
  s:string;
begin
  s:= Trim(edtNo.text);
  if length(s)=6 then
  begin
    if IsExponent(s) then
    begin
      edtstockno.enabled := False;
      edtmoney.enabled := False;
      edtstockno.color:=$FF000000;
      edtmoney.color:=$FF000000;
    end else
    begin
      edtstockno.enabled := True;
      edtmoney.enabled := True;
      edtstockno.color:=$FFFFFF;
      edtmoney.color:=$FFFFFF;
    end;
  end;
end;

//编辑时的确定
procedure btnEditOkClick(sender);
var
  s,str:string;
begin
  if edtNo<>nil then
  begin
    if (length(edtNo.text)<>6) and (frm<>nil) then
    begin
      frm.ModalResult:=0;
      showmsg('无效的股票代码！');
      exit;
    end;
    try
      i:=StrToInt(edtNo.text);
    except
      if (frm<>nil) then
        frm.ModalResult:=0;
      showmsg('无效的股票代码！');
      exit;
    end;
    str:=copy(edtNo.text,1,2);
    if str='60' then rbsh.checked:=true
      else if str='00' then rbsz.checked:=true;  
    if rbsh.checked then s:='sh'
      else s:='sz';
    s:=s+edtNo.text;
    frm.caption:='修改股票(查询股票编号...)';
    
    if StockIsEffective(s) then
    begin
      showmsg('当前股票已经存在，请重新输入!');
      frm.ModalResult:=0;
      exit;
    end;

    s:=GetStockName(s);
    frm.caption:='修改股票';
    s:=strBetween(s,'"','"');
    if s='' then
    begin
      if (frm<>nil) then
        frm.ModalResult:=0;
      showmsg('无效的股票代码！');
    end;
  end;
end;

procedure miEditOnClick(sender);
var
  btnOK,btnCancel:TButton;
  lblNo,lblMoney,lblStockNo:TLabel;
  rdgShowType:TRadioGroup;
  lstMoney,lstDeal:TStrings;
  s1,s2:string;
  i,Number:Integer;
  sedtno:String;
begin
  if not ((0<=SelIndex) and (SelIndex<=EveryPageCount-1)) then exit;
  i:=BaseIndex+SelIndex;
  frm:=TForm.create(nil);
  frm.width:=230;
  frm.height:=180;
  frm.Position:=poScreenCenter;
  frm.caption:='修改股票';
  if screen.fonts.indexof('微软雅黑')>=0 then
    frm.font.name:='微软雅黑'
  else
    frm.font.name:='宋体';
  frm.font.size:=9;
  frm.BorderStyle:=bsDialog;
  try
    rbsh:=TRadioButton.create(frm);
    rbsh.left:=16; rbsh.top:=16;
    rbsh.parent:=frm;
//    rbsh.Enabled := False;
    rbsh.caption:='沪市';
    rbsh.checked:=true;
    
    rbsz:=TRadioButton.create(frm);
    rbsz.left:=96; rbsz.top:=16;
    rbsz.parent:=frm;
//    rbsz.Enabled := False;
    rbsz.caption:='深市';
    
    lblNo:=TLabel.create(frm);
    lblNo.parent:=frm;
    lblNo.Caption:='请输入股票代码：';
    lblNo.left:=16; lblNo.top:=40;
    
    edtNo:=TEdit.create(frm);
    edtNo.parent:=frm;
    edtNo.left:=120; edtNo.top:=38;
    edtNo.width:=80;
    edtNo.height:=16;
    edtNo.TabOrder:=0;
//    edtno.Enabled := False;

    lblmoney :=tlabel.create(frm);
    lblmoney.parent := frm;
    lblmoney.caption := '买入价：';
    lblmoney.left := 66; lblmoney.top := 67;

    edtMoney:=TEdit.create(frm);
    edtMoney.parent:=frm;
    edtmoney.left := 120;
    edtmoney.top:= 65;
    edtmoney.width := 80;
    edtmoney.height:=16;
    edtmoney.taborder:=1;

    lblstockno := tlabel.create(frm);
    lblstockno.parent := frm;
    lblstockno.caption := '股票份数：';
    lblstockno.left := 52; lblstockno.top := 94;

    edtstockno:=TEdit.create(frm);
    edtstockno.parent:=frm;
    edtstockno.left := 120;
    edtstockno.top := 92;
    edtstockno.width:=80;
    edtstockno.height:=16;
    edtstockno.taborder:=2;

    btnCancel:=TButton.create(frm);
    btnCancel.caption:='取  消';
    btnCancel.left:=130;
    btnCancel.top:=123;
    btnCancel.parent:=frm;
    btnCancel.ModalResult:=2;
    btnCancel.cancel:=true;
    btnCancel.cancel:=true;
    
    btnOK:=TButton.create(frm);
    btnOK.caption:='确  定';
    btnOK.left:=50;
    btnOK.top:=123;
    btnOK.parent:=frm;
    btnOK.ModalResult:=1;
    btnOk.OnClick:='btnEditOkClick';
    btnOK.default:=true;
    //编辑窗口初始化时
    lst:=TStringList.create;
    lstMoney:=TStringList.create;
    lstDeal:=TStringList.create;
    try
      lst.text:=StrToStrings(Stocks,',');

      lstmoney.text:=strtostrings(smoney,',');
      lstdeal.text:=strtostrings(sStockDeal,',');
      s1 := lst.strings[i];
      if Copy(s1,1,2) = 'sh' then rbsh.checked:=True
      else if Copy(s1,1,2) = 'sz' then rbsz.checked := True;
      edtNo.text := Copy(s1,3,Length(s1));
      sedtno:=s1;
      edtno.onchange := 'edtNoChange';
      Number:=StrNumber(lst.strings[i],lstmoney);
      if Number <> -1 then 
      begin
        s2 := lstmoney.strings[Number];
        edtMoney.text := Copy(s2,Pos(';',s2)+1,Length(s2));
      end;
      Number:=StrNumber(lst.strings[i],lstDeal);
      if Number <> -1 then 
      begin
        s2 := lstDeal.strings[Number];
        edtstockno.text := Copy(s2,Pos(';',s2)+1,Length(s2));
      end;
      if Number = -1 then
      begin
        edtmoney.enabled := False;
	edtstockno.enabled := False;
      end;
    finally
      lstmoney.free;
      lstdeal.free;
      lst.free;
    end;
    
    //编辑窗口返回时
    if frm.ShowModal=1 then
    begin
      if rbsh.checked then s:='sh'
        else s:='sz';
      s:=s+edtNo.text;
      s1 :=edtMoney.text;
      s2:=edtstockno.text;
      lst:=TStringList.create;
      lstMoney:=TStringList.create;
      lstDeal:=TStringList.create;
      try
        lst.text:=StrToStrings(Stocks,',');

      	lstmoney.text:=strtostrings(smoney,',');
      	lstdeal.text:=strtostrings(sStockDeal,',');
        if edtMoney.enabled then
        begin
      	  Number:=StrNumber(lst.strings[i],lstmoney);
          if Number<>-1 then
          begin
	          lstMoney.strings[number] := s+';'+s1;
          end 
          else
            lstmoney.add(s+';'+s1);
          sMoney:=StringsToStr(lstMoney.text,',');
      	  SetSetting('Money',sMoney);
      	  Number:=StrNumber(lst.strings[i],lstdeal);
          if Number<>-1 then
          begin
      	    lstdeal.strings[number] := s+';'+s2;
          end 
          else lstdeal.add(s+';'+s2);
	        sStockDeal:=StringsToStr(lstdeal.text,',');
          SetSetting('StockDeal',sStockDeal);
        end else
	      begin
          lstmoney.text:=strtostrings(smoney,',');
          lstdeal.text:=strtostrings(sStockDeal,',');
	        Number:=StrNumber(lst.strings[i],lstmoney);
          if Number<>-1 then
          begin
	          lstMoney.Delete(Number);
          end;
          sMoney:=StringsToStr(lstMoney.text,',');
      	  SetSetting('Money',sMoney);
      	  Number:=StrNumber(lst.strings[i],lstdeal);
          if Number<>-1 then
          begin
	          lstdeal.Delete(Number);
          end;
	        sStockDeal:=StringsToStr(lstdeal.text,',');
          SetSetting('StockDeal',sStockDeal);
	  
	      end;
	      lst.strings[i] := s;
        Stocks:=StringsToStr(lst.text,',');
        SetSetting('Stocks',Stocks);
        RefreshInfo;
      finally
        lstmoney.free;
	      lstdeal.free;
        lst.free;
      end;
    end;
  finally
    frm.free;
  end;
end;