MindlessMaik
MindlessMaik

Reputation: 291

applying scrollbars to pictureBox

I have looked at the examples on this and other sites to create this pictureBox. Yet I still can't figure out, why scrollbars are not shown. I guess I am missing a small but important detail :P. The Code below should be fully functional.

using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
    partial class Form2
    {
        private System.ComponentModel.IContainer components = null;
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.panel1 = new System.Windows.Forms.Panel();
            this.SuspendLayout();
            // 
            // panel1
            // 
            //this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel1.Location = new System.Drawing.Point(0, 0);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(300, 300);
            this.panel1.TabIndex = 0;
            this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.mPictureBoxPaint_Paint);   
            // 
            // Form2
            // 
            this.AutoScroll = true;
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(391, 297);
            this.Controls.Add(this.panel1);
            this.Name = "Form2";
            this.Text = "Form2";
            this.ResumeLayout(false);

        }

        private void mPictureBoxPaint_Paint(object sender, PaintEventArgs e)
        {

            Pen blackPen = new Pen(Brushes.Black);
            blackPen.Width = 1.0F;
            blackPen.LineJoin = System.Drawing.Drawing2D.LineJoin.Bevel;

            for (int i = 0; i < 100; i++)
            {
                for (int j = 0; j < 100; j++) 
                {
                    e.Graphics.DrawRectangle(blackPen,
                        new Rectangle(i*20, j*20, 2, 2));
                }
            }


            blackPen.Dispose();
        }
        private System.Windows.Forms.Panel panel1;
    }
}

Edit: Updated my Code(removed this.Controls.AddRange() and scrollbars). Now the pictureBox is fixed to panel, but panel does not recognize, if drawings are outside the borders. So still no scrollbars.

Edit2: Just realised that I dont even need a picturebox so I created a new example which is more easy to follow. If this.panel1.Dock is set, I see all points but no scrollbars, if it is not set I see scrollbars but not all Points. What I need is a Panel which automatically resizes according to the amount of points which should be shown, in a fixed windows size. So all Points are visible AND I have scrollbars.

Upvotes: 0

Views: 4619

Answers (1)

Baas
Baas

Reputation: 46

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        //just write number of Points you require on X-Axis of Y-Axis
        //This will also define how big the Panel gets
        int PointsX = 10;
        int PointsY = 5;
        private void InitializeComponent()
        {
            this.panel1 = new System.Windows.Forms.Panel();
            this.SuspendLayout();
            // 
            // panel1
            // 
            //this.panel1.Dock = DockStyle.Fill;
            this.panel1.Location = new System.Drawing.Point(1, 1);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size((PointsX*20), (PointsY*20));//Was 300,300
            this.panel1.TabIndex = 0;
            this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.mPictureBoxPaint_Paint);
            // 
            // Form2
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.AutoScroll = true;
            this.ClientSize = new System.Drawing.Size(400, 300);
            this.Controls.Add(this.panel1);
            this.Name = "Form2";
            this.Text = "Form2";
            this.ResumeLayout(false);

        }

        private void mPictureBoxPaint_Paint(object sender, PaintEventArgs e)
        {

            Pen blackPen = new Pen(Brushes.Black);
            blackPen.Width = 1.0F;
            blackPen.LineJoin = System.Drawing.Drawing2D.LineJoin.Bevel;

            for (int i = 0; i < PointsX; i++)
            {
                for (int j = 0; j < PointsY; j++) 
                {
                    e.Graphics.DrawRectangle(blackPen,
                        new Rectangle(i*20, j*20, 2, 2));
                }
            }


            blackPen.Dispose();
        }
        private System.Windows.Forms.Panel panel1;


        }
    }

Upvotes: 1

Related Questions