请问大家,
我在winform里面添加了一个panel,设置成autoscroll,
然后在Panel里面放一个treeview不能滚动的,当treeview里面的数据多了以后,panel就会出现滚动条。问题:我选择treeview的一个节点以后,然后滚动到底部,当treeview再次获得焦点的时候(比如先让treeview失去焦点,然后点击下面的treeview的node,)每次点击都是treeview滚动到顶部,然后第二次点击才能选中节点。哪位高手能解释一下吗

解决方案 »

  1.   

    AUTOSCROLL 属性为TRUE后 当会自动混动到可以限制子控件的位置,但要注意如果显示不下子控件会显示到子控件的开始位置。
     当你选择NODE先是PANEL会滚动到TREE的0,0的位置。
      

  2.   

    对对,确实如此,那么如何避免这种情况呢?就是不让它滚动到0,0的位置,其实我的原始需求是做一个treeview的自定义滚动条,实现的原理就是把这个treeview放到panel里面,然后控制panel的滚动条。
      

  3.   

    谢谢了,解决掉了,I found my solution at: http://seewinapp.blogspot.com/2005/09/is-your-autoscroll-too-auto.htmlSo in order to keep the section that you scrolled to, in view and not have it scroll back to the upperleft corner of the control that you entered you must perform some kind of hack using the BeginInvoke method because it won't work in the events that handle the change of focus:
    Quote:
    That depends, when would you set it? If you set it in one of the events related to the change of focus, it just doesn't work. The reason is because the AutoScroll behavior is occuring after your event handlers.
    So I did that and it works. I just subscribe to the Enter event of each ScheduleBar I create on the ScheduleControl and implement this code.
    Code:void ScheduleBar_Enter(object sender, EventArgs e)
    {
      if(Parent is Panel) // My AutoScroll is set on the panel that contains this (schedule)control for now.
      {
        Point p = (this.Parent as Panel).AutoScrollPosition;
        AutoScrollPositionDelegate del = new AutoScrollPositionDelegate(SetAutoScrollPosition);
        Object[] args = { this.Parent as Panel, p };
        BeginInvoke(del, args);
      }
    }private void SetAutoScrollPosition(ScrollableControl sender, Point p)
    {
       p.X = Math.Abs(p.X);
       p.Y = Math.Abs(p.Y);
       sender.AutoScrollPosition = p;
    }