为groupbox,panel。。。加滚动条

 

添加一个vScrollBar控件到目标控件上面

 

       public OrderPayManage()
        {
            InitializeComponent();
            foreach (Control controls in groupbox.Controls)
            {
                if (controls is VScrollBar) continue;
                controls.Tag = controls.Location.Y;
            }
        }

 

  private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
        {
            foreach (Control controls in groupbox.Controls)
            {
                if (controls is VScrollBar) continue;
                controls.Location = new Point(controls.Location.X, (int)controls.Tag - e.NewValue);
            }
        }

 

将panel的AutoScroll的属性设为ture就可以显示滚动条了了
panel的滚动条随鼠标滑轮滚动而移动

private void MessageDetail_Load(object sender, EventArgs e)
        {
            //注册事件 
            this.MouseWheel += new MouseEventHandler(FormSample_MouseWheel); 
        }

        ///   <summary> 
        ///   滚动方法 
        ///   </summary> 
        ///   <param   name= "sender "> </param> 
        ///   <param   name= "e "> </param> 
        void FormSample_MouseWheel(object sender, MouseEventArgs e)
        {
            //获取光标位置 
            Point mousePoint = new Point(e.X, e.Y);
            //换算成相对本窗体的位置 
            mousePoint.Offset(this.Location.X, pnlMessage.Location.Y);
            //判断是否在panel内 
            if (pnlMessage.RectangleToScreen(this.DisplayRectangle).Contains(mousePoint))
            {
                //滚动 
                pnlMessage.AutoScrollPosition = new Point(0, pnlMessage.VerticalScroll.Value - e.Delta);
            }
        } 




将panel的滚动条致最低端
pnlMessage.VerticalScroll.Value = pnlMessage.VerticalScroll.Maximum;

如果不行就将以上代码使用2次

 

 

richtextbox滚动条致对低端

 //选择richtextbox中内容的最后一个字节 
this.richTextBox1.Select(this.richTextBox1.Text.Length, 0);
 //设置滚动到当前位置
this.richTextBox1.ScrollToCaret();

 

 

你可能感兴趣的:(object,scroll)