PictureBox with overriden OnPaint method in C#

Piotr Łużecki picture Piotr Łużecki · Mar 26, 2012 · Viewed 10k times · Source

I would like to paint some small pictures in PictureBox (4 x 32px images in a row) so should i override OnPaint method or i need to make my new component that extends PictureBox? I tried this, which worked in Java, but not here:

        this.pictureBox1 = new System.Windows.Forms.PictureBox()
        {
            protected override void OnPaint(PaintEventArgs e)
            {
               // If there is an image and it has a location, 
               // paint it when the Form is repainted.
                Graphics g = e.Graphics;

                // Draw a string on the PictureBox.
                g.DrawString("Test, is that working?",
                new Font("Arial", 10), System.Drawing.Brushes.Blue, new Point(30, 30));
             }
        }

Full code of InitializeComponent method:

   private void InitializeComponent()
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Tools));
        this.pictureBox1 = new System.Windows.Forms.PictureBox()
        {
            protected override void OnPaint(PaintEventArgs e)
            {
               // If there is an image and it has a location, 
               // paint it when the Form is repainted.
                Graphics g = e.Graphics;

                // Draw a string on the PictureBox.
                g.DrawString("Test, is that working?",
                new Font("Arial", 10), System.Drawing.Brushes.Blue, new Point(30, 30));
             }
        }
        this.vscrollb = new System.Windows.Forms.VScrollBar();
        this.vScrollBar1 = new System.Windows.Forms.VScrollBar();
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
        this.SuspendLayout();
        // 
        // pictureBox1
        // 
        this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Left;
        this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
        this.pictureBox1.InitialImage = null;
        this.pictureBox1.Location = new System.Drawing.Point(0, 0);
        this.pictureBox1.Name = "pictureBox1";
        this.pictureBox1.Size = new System.Drawing.Size(264, 262);
        this.pictureBox1.TabIndex = 0;
        this.pictureBox1.TabStop = false;
        this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
        // 
        // vscrollb
        // 
        this.vscrollb.Location = new System.Drawing.Point(0, 0);
        this.vscrollb.Name = "vscrollb";
        this.vscrollb.Size = new System.Drawing.Size(20, 80);
        this.vscrollb.TabIndex = 0;
        // 
        // vScrollBar1
        // 
        this.vScrollBar1.Dock = System.Windows.Forms.DockStyle.Right;
        this.vScrollBar1.Location = new System.Drawing.Point(267, 0);
        this.vScrollBar1.Name = "vScrollBar1";
        this.vScrollBar1.Size = new System.Drawing.Size(17, 262);
        this.vScrollBar1.TabIndex = 1;
        this.vScrollBar1.Scroll += new System.Windows.Forms.ScrollEventHandler(this.HandleScroll);
        // 
        // Tools
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.BackColor = System.Drawing.Color.Black;
        this.ClientSize = new System.Drawing.Size(284, 262);
        this.Controls.Add(this.vScrollBar1);
        this.Controls.Add(this.pictureBox1);
        this.Name = "Tools";
        this.Text = "Tools";
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
        this.ResumeLayout(false);

    }

Answer

Hans Passant picture Hans Passant · Mar 26, 2012

This just isn't valid C# code. Overriding a virtual method like OnPaint() is fine but you can only do so in a class that's derived from PictureBox. Which works well, after you compile you'll get the new control automatically added to the toolbox so you can put it on a form.

This is however not necessary. You can simply implement the Paint event for the control. You already did, you named it pictureBox1_Paint(). Just move your code there.

Other important pointers: never edit InitializeComponent(). It is auto-generated by the designer. As soon as you modify the design of the form, you'll lose any code you wrote there. It is also a really good way to make the form un-designable, triggering an exception when the form is loaded by the designer. And if you do override OnPaint() then it is important that you call base.OnPaint(). So that the normal PictureBox plumbing continues to work. Including painting the Image and raising the Paint event. Be sure to at least follow a tutorial or read a book about Winforms programming, there will be a lot of trial and mostly error if you don't.