Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Panel array [modified]

Panel array [modified]

Scheduled Pinned Locked Moved C#
graphicsdata-structuresquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    peropata
    wrote on last edited by
    #1

    Hi , suppose I have a class that draws an image on panel on Form1. How do I dynamically create a number of panels on a Form1, each containing a drawing derived from graphics class and at the same time change properties of each image seperatly.

    modified on Tuesday, April 6, 2010 2:43 PM

    W 1 Reply Last reply
    0
    • P peropata

      Hi , suppose I have a class that draws an image on panel on Form1. How do I dynamically create a number of panels on a Form1, each containing a drawing derived from graphics class and at the same time change properties of each image seperatly.

      modified on Tuesday, April 6, 2010 2:43 PM

      W Offline
      W Offline
      WillemM
      wrote on last edited by
      #2

      You can add panels dynamically to a form by doing something similar to the following: Form parentForm; // Set this to the form you want the panels added to

      foreach(var drawing in Drawings)
      {
      MyCustomPanel myPanel = new MyCustomPanel();
      myPanel.Drawing = drawing;

      // Set the location and size to the values you need

      parentForm.Controls.Add(myPanel);
      }

      The trick here is to create a custom control derived from panel that will paint the drawing you assign to it. Here's a basic skeleton to get you on the way

      public class MyCustomPanel: Panel
      {
      public Drawing Drawing { get; set; }

      protected override void OnPaint(object sender,PaintEventArgs e)
      {
      // Draw the image here using e.Graphics
      }
      }

      WM. My blog

      P 1 Reply Last reply
      0
      • W WillemM

        You can add panels dynamically to a form by doing something similar to the following: Form parentForm; // Set this to the form you want the panels added to

        foreach(var drawing in Drawings)
        {
        MyCustomPanel myPanel = new MyCustomPanel();
        myPanel.Drawing = drawing;

        // Set the location and size to the values you need

        parentForm.Controls.Add(myPanel);
        }

        The trick here is to create a custom control derived from panel that will paint the drawing you assign to it. Here's a basic skeleton to get you on the way

        public class MyCustomPanel: Panel
        {
        public Drawing Drawing { get; set; }

        protected override void OnPaint(object sender,PaintEventArgs e)
        {
        // Draw the image here using e.Graphics
        }
        }

        WM. My blog

        P Offline
        P Offline
        peropata
        wrote on last edited by
        #3

        Here is my code: Class for drawing different shapes (selected by iSelectShape):

        public class ShapeDrawing
        {
        private int iSelectShape = 1;

            public int SelectShape
            {
                get { return iSelectShape; }
                set { iSelectShape = value; }
            }
        
            public void DrawShape(Graphics g)
            {
                Rectangle r = new Rectangle(new Point(0, 0), new Size(100, 100));
                
                switch (iSelectShape)
                {
                    case 1:
                        g.DrawEllipse(Pens.Black , r);
                        break;
                    case 2:
                        g.DrawRectangle(Pens.Black, r);
                        break;
                }
            }
        }
        

        Custom panel class:

        public class MyCustomPanel : Panel
        {
            public ShapeDrawing Drawing { get; set; }
        
            protected override void OnPaint(PaintEventArgs e)
            {
                // Draw the image here using e.Graphics
                if (Drawing != null)
                    Drawing.DrawShape(e.Graphics);
            }
        }
        

        And main form with one panel:

        public partial class Form1 : Form
        {
        public Form1()
        {
        InitializeComponent();

                ShapeDrawing shape1 = new ShapeDrawing();
                myPanel.Drawing = shape1;
                shape1.SelectShape = 2;
            }
        
            private void myPanel\_Paint(object sender, PaintEventArgs e)
            {
        
            }
        
        }
        

        and InitializeComponent

        private void InitializeComponent()
        {
        this.myPanel = new Dynamic_controls_panel.MyCustomPanel();
        this.SuspendLayout();
        //
        // myPanel
        //
        this.myPanel.BackColor = System.Drawing.Color.White;
        this.myPanel.Drawing = null;
        this.myPanel.Location = new System.Drawing.Point(0, 0);
        this.myPanel.Name = "myPanel";
        this.myPanel.Size = new System.Drawing.Size(301, 331);
        this.myPanel.TabIndex = 0;
        this.myPanel.Paint += new System.Windows.Forms.PaintEventHandler(this.myPanel_Paint);
        //
        // Form1
        //
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(416, 459);
        this.Controls.Add(this.myPanel);
        this.Name = "Form1";

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups