在listbox中
1
2
3
4
5
3     //如何得到这个3的行号?
6有两个“3”请问如何得到最后一个”3“的行号??

解决方案 »

  1.   

    自己循环判断:
    function TForm1.GetLastIndex(s: string): Integer;
    var i: Integer;
    begin
      for i := ListBox1.Count - 1 downto 0 do
        if ListBox1.Items[i] = '3' then Break;
      Result := i;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
      ShowMessage(IntToStr(GetLastIndex('3')));
    end;
      

  2.   

    上面的'3'改为s,这样:
    function TForm1.GetLastIndex(s: string): Integer;
    var i: Integer;
    begin
      for i := ListBox1.Count - 1 downto 0 do
        if ListBox1.Items[i] = s then Break;
      Result := i;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
      ShowMessage(IntToStr(GetLastIndex('3')));
    end;
      

  3.   

    var i,n:integer;
    begin
    n:=listbox1.Items.Count;
    i:=n-1;
    while i>0 do
      begin
      if listbox1.Items.Strings[i]='3' then
         begin
         n:=i;
         i:=0;
         end;
      i:=i-1;
      end;
    end;
    n为所求值
      

  4.   

    var i,n:integer;
    begin
    n:=listbox1.Items.Count;
    i:=n-1;
    while i>0 do
      begin
      if listbox1.Items.Strings[i]='3' then
         begin
         n:=i;
         i:=0;
         end;
      i:=i-1;
      end;
    end;主要是循环,如果某一项的值为3,记下这一项的行号
      

  5.   

    不是这么麻烦吧,就是WGYKING的方法呀!一句话就搞定了
      

  6.   

    看来只有循环做了,没有.Net中的LastIndexOf方法