Robert

Robert

  • NA
  • 9
  • 17.2k

Drawing inside panel and scrool problem

Oct 24 2009 8:32 AM
Hello, I'm drawing some stuff inside panel (it's not important)
 private void panel1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Pen p1 = new Pen(Color.Black, 5);
            int yL = 40;
            foreach(string ds in Enum.GetNames(typeof(DashStyle)))
            {
                if (ds != "Custom")
                {
                    foreach(string lc in Enum.GetNames(typeof(LineCap))) {
                        p1.DashStyle = (DashStyle)(Enum.Parse(typeof(DashStyle),ds));    
                        p1.StartCap = (LineCap)(Enum.Parse(typeof(LineCap),lc));
                        p1.EndCap = (LineCap)(Enum.Parse(typeof(LineCap), lc));
                        g.DrawLine(p1, 20, yL, 120, yL);
                        g.DrawString("DashStyle:"+ds+"  LineCap:"+lc, new Font("Arial", 10), Brushes.Black, 140, yL-8);
                        yL+=20;
                    }
                }
            }
            p1.Dispose();
        }

The min, max size of form is fixed.

How to add possibility to scroll vertically and horizontally, so I can see everything what has been painted?

I tried for the panel : this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
and for the form:
this.AutoScrollMinSize = new Size(this.panel1.Width, this.panel1.Height);
but this isn't gonna to work, it returns size of panel before paint operation, after paint panel'll be longer.
I can set manually this.AutoScrollMinSize = new Size(999999,99999) but this is pointless too, because scrolling will be too much.

How to do it automatically, so this.AutoScrollMinSize will know what is the size of Panel after drawing, I can't do it manually because in this example I don't know how big this panel should be?

Answers (6)