You would set the AutoScrollPosit ion for this.
After my signature is a simple application that demonstrates the idea.
--
Bob Powell [MVP]
Visual C#, System.Drawing
Ramuseco Limited .NET consulting
http://www.ramuseco.com
Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
using System;
using System.Drawing;
using System.Drawing. Drawing2D;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Data;
namespace CenterScrollPan el
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows. Forms.Form
{
private System.Windows. Forms.Panel panel1;
private System.Windows. Forms.Button button1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.Componen tModel.Containe r components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeCompo nent();
//
// TODO: Add any constructor code after InitializeCompo nent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Disp ose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.panel1 = new System.Windows. Forms.Panel();
this.button1 = new System.Windows. Forms.Button();
this.SuspendLay out();
//
// panel1
//
this.panel1.Aut oScroll = true;
this.panel1.Aut oScrollMinSize = new System.Drawing. Size(1024, 768);
this.panel1.Loc ation = new System.Drawing. Point(8, 8);
this.panel1.Nam e = "panel1";
this.panel1.Siz e = new System.Drawing. Size(272, 208);
this.panel1.Tab Index = 0;
this.panel1.Pai nt += new
System.Windows. Forms.PaintEven tHandler(this.p anel1_Paint);
//
// button1
//
this.button1.Lo cation = new System.Drawing. Point(16, 232);
this.button1.Na me = "button1";
this.button1.Ta bIndex = 1;
this.button1.Te xt = "Recenter";
this.button1.Cl ick += new System.EventHan dler(this.butto n1_Click);
//
// Form1
//
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(292, 266);
this.Controls.A dd(this.button1 );
this.Controls.A dd(this.panel1) ;
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayo ut(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run (new Form1());
}
private void panel1_Paint(ob ject sender, System.Windows. Forms.PaintEven tArgs
e)
{
e.Graphics.Tran sform=new
Matrix(1,0,0,1, this.panel1.Aut oScrollPosition .X,this.panel1. AutoScrollPosit ion.Y);
Pen p=new Pen(Color.Red,3 );
e.Graphics.Draw Line(p,512,374, 512,394);
e.Graphics.Draw Line(p,502,384, 522,384);
p.Dispose();
}
private void button1_Click(o bject sender, System.EventArg s e)
{
this.panel1.Aut oScrollPosition =new Point(
this.panel1.Aut oScrollMinSize. Width/2-this.panel1.Cli entRectangle.Wi dth/2,
this.panel1.Aut oScrollMinSize. Height/2-this.panel1.Cli entSize.Height/2);
}
}
}
"Sharon" <Sh*****@newsgr oups.nospam> wrote in message
news:0B******** *************** ***********@mic rosoft.com...
I'm using the Panel control that contains a PictureBox control (for
implementing the http://www.codeproject.com/cs/miscctrl/PictureBox.asp).
The Panel is set to AutoScroll = true.
I wish to scroll the appearing vertical and horizontal scrolls bars of the
Panel to the middle of their scrolling range so the contained PictureBox
will
be centered inside the Panel.
But I could not find a way to get the scrolls bars for doing that.
Can anybody tell me how can I do that?
---------
Thanks
Sharon