using System; using System.Windows.Forms; public class scrollpanel { public Panel contenedor; public Panel contenido; VScrollBar miscroll; int sc_ancho; public int Top { // desviamos estos valores a los del contenedor get { return this.contenedor.Top; } set { this.contenedor.Top=value; } } public int Left { get { return this.contenedor.Left; } set { this.contenedor.Left=value; } } public int Width { get { return this.contenedor.Width; } set { // hay que ajustar el ancho del panel y el de la barra de desplazamiento this.contenedor.Width=value; this.miscroll.Left=(value>sc_ancho) ? value-sc_ancho : 0; this.miscroll.Width=(value>sc_ancho) ? sc_ancho : value; this.contenido.Left=0; this.contenido.Width=(value>sc_ancho) ? value-sc_ancho : 0; } } public int CWidth { // este es el ancho del contenido get { return this.contenido.Width; } } public int Height { // ajustamos aquí el alto del contenedor get { return this.contenedor.Height; } set { this.contenedor.Height=value; this.miscroll.Height=value; set_maximum(); } } public int CHeight { // y aquí el alto del contenido get { return this.contenido.Height; } set { this.contenido.Height=value; set_maximum(); } } public scrollpanel() { this.contenedor=new Panel(); this.contenido=new Panel(); // creamos ambos paneles this.miscroll=new VScrollBar(); // y la barra de desplazamiento this.miscroll.SmallChange=10; // desplazamiento mínimo (al pulsar fuera del deslizador) this.contenido.Top=0; this.miscroll.Top=0; this.miscroll.Minimum=0; sc_ancho=this.miscroll.Width; // recordamos el ancho genérico de una barra de desplazamiento this.contenedor.Controls.Add(this.contenido); this.contenedor.Controls.Add(this.miscroll); // añadimos ambos widgets al Panel externo this.Width=this.contenedor.Width; this.Height=this.contenedor.Height; this.miscroll.ValueChanged += new EventHandler(scrolled); } private void scrolled(object sender,EventArgs e) { this.contenido.Top = -(this.miscroll.Value); } public void AddControl(Control ncontrol) { contenido.Controls.Add(ncontrol); } private void set_maximum() { this.miscroll.Maximum = this.contenido.Height-1; this.miscroll.LargeChange = this.contenedor.Height; } }