我在一个窗体上放了一个TREEVIEW控件和一些查询按件。开始树是合拢或无序的,我想作一个查询功能,想查到哪个项目,对应的树节点就展开并高亮显示!如何实现?????实现了就结帖!!!

解决方案 »

  1.   

    如果你能保证你的节点的text都是唯一的,那么你可以遍历树就可以了,找到之后,将其Select,Expand!!!
    如果节点的Text不是唯一的,那么你得使用Node的Data属性,事先为每一个节点建立一个唯一的标示,然后按照上面的方法去做!!!
      

  2.   

    我是这样作的,可还是不行。我想让他像鼠标点上去一样的效果!
    Function Tpbinfo_Form.GetNodeItem(sCode:string):integer;//遍历获得item
    var i,iCount,val:integer;
    tmp:string;
    begin
    Result:=0;
    iCount:=Treeview1.Items.Count;
    if iCount=0 then exit;
    val:=0;
    for i:=0 to iCount-1 do
    begin
    Tmp:=Treeview1.Items.Item[i].Text;
    if Tmp=sCode then val:=i else val:=0;
    end;
    result:=val;
    end;
    procedure Tpbinfo_form.Button5Click(Sender: TObject);//假设有个按键调用
    var
     str:string;
    begin
    str:='6200';
    treeview1.Items.Item[getnodeitem(str)].Focused ;//我试过用SETFORCUS,也不行
    end;
      

  3.   

    treeview1.Items.Item[getnodeitem(str)].Focused ;//我试过用SETFORCUS,也不行
    应该改为:
    iPos : Integer
    iPos := getnodeitem(str)
    treeview1.Items.Item[iPos].Selected = True
    treeview1.Items.Item[iPos].Expand
      

  4.   

    函数有问题,这样修改:我是这样作的,可还是不行。我想让他像鼠标点上去一样的效果!
    Function Tpbinfo_Form.GetNodeItem(sCode:string):integer;//遍历获得  item
      var i,iCount,val:integer;
      tmp:string;
      IsFound:Boolean;
    begin
      Result:=-1;
      iCount:=Treeview1.Items.Count;
      if iCount=0 then exit;
      val:=-1  //初始化为-1,因为0也是一个实际的节点,不妥
      i:=-1;
      IsFound:=false;
      while (not found) and i<= iCount-1 do
      begin
        Tmp:=Treeview1.Items.Item[i].Text;
        if Tmp=sCode then
        begin
          val:=i;
          IsFound:=true;
        end;
        inc(i);
      end;
      result:=val;
    end;
    procedure Tpbinfo_form.Button5Click(Sender: TObject);//假设有个按键调用
    var
      str:string;
      Index:Integer;
    begin
      str:='6200';
      Index:=GetNodeItem(Str)
      if Index>=0 then
      begin
        treeview1.Items.Item[Index].Selected:=true; 
        treeview1.Items.Item[Index].Expand;
      end;
    end;