Panel array [modified]
-
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
-
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
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
-
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
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";