title: How to add multiple canvases to a picturebox?
body: <p>Let me explain my problem:</p>
  
  <p>I have a picturebox and this should be used to draw several elements of a listbox.
  I gave the picturebox a canvas. by left and rightclick u can put up a rectangle which should preview the element u want to draw on the canvas of the picturebox.</p>
  
  <p>Now this is my problem:</p>
  
  <p>this rectangle should just be a preview and should be erased every time i click on a another position of the picturebox and put up a new rectangle. it should just be drawn on the canvas when i click on the inputbutton.</p>
  
  <p>So how can i preview the chosen elements in the rectangle made by left and rightclick?</p>
  
  <p>I hope my question is clealy enough : /</p>
  
  <p>This is what i did so far:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows.Forms;
  
  namespace Interaktive_systeme_sta
  {
      public partial class Bildeditor : Form
      {
  
  
          void drawRect()
          {
              pbxBild.Refresh();
  
              using (Graphics g = this.pbxBild.CreateGraphics())
              {
  
                  Pen blackPen = new Pen(Color.Black, 1);
                  blackPen.DashPattern = new float[] {2,2};
  
                  int X = Convert.ToInt32(nupX.Value);
                  int Y = Convert.ToInt32(nupY.Value);
                  int B = Convert.ToInt32(nupBreite.Value);
                  int H = Convert.ToInt32(nupHoehe.Value);
  
                  g.DrawRectangle(blackPen, X, Y, B, H);
  
                  g.Dispose();
  
              }
          }
  
          public Bildeditor()
          {
              InitializeComponent();
  
              this.MinimumSize = new Size(630,420);
              this.MaximumSize = new Size(630, 420);
  
              int canvasWidth = pbxBild.Width;
              int canvasHeight = pbxBild.Height;
  
              nupBreite.Maximum = canvasWidth;
              nupHoehe.Maximum = canvasHeight;
  
              nupBreite.Minimum = 0;
              nupHoehe.Minimum = 0;
  
              nupBreite.Value = canvasWidth;
              nupHoehe.Value = canvasHeight;
  
              nupX.Maximum = canvasWidth;
              nupY.Maximum = canvasHeight;
  
              nupX.Minimum = 0;
              nupY.Minimum = 0;
  
              nupX.Value = 0;
              nupY.Value = 0;
  
              drawRect();
  
          }
  
          private void btnZuruecksetzten_Click(object sender, EventArgs e)
          {
              int canvasWidth = pbxBild.Width;
              int canvasHeight = pbxBild.Height;
  
              nupX.Value = 0;
              nupY.Value = 0;
              nupBreite.Value = canvasWidth-1;
              nupHoehe.Value = canvasHeight-1;
          }
  
          private void numericUpDown2_ValueChanged(object sender, EventArgs e)
          {
              drawRect();
          }
  
          private void pbxBild_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
          {
              Point mouseDownLocation = new Point(e.X, e.Y);
  
              switch (e.Button)
              {
                  case MouseButtons.Left:
                      if (e.X &lt; 0)
                      {
                          nupX.Value = 0;
                      }
                      else
                      {
                          nupX.Value = e.X;
                      }
                      if (e.Y &lt; 0)
                      {
                          nupY.Value = 0;
                      }
                      else
                      {
                          nupY.Value = e.Y;
                      }
                      break;
                  case MouseButtons.Right:
  
                      if (e.X - nupX.Value &lt; 1)
                      {
                          nupBreite.Value = 1;
                      }
                      else
                      {
                          nupBreite.Value = e.X - nupX.Value;
                      }
  
                      if (e.Y - nupY.Value &lt; 1)
                      {
                          nupHoehe.Value = 1;
                      }
                      else
                      {
                          nupHoehe.Value = e.Y - nupY.Value;
                      }
                      break;
              }
  
              drawRect();
          }
  
          private void nupY_ValueChanged(object sender, EventArgs e)
          {
              drawRect();
          }
  
          private void nupBreite_ValueChanged(object sender, EventArgs e)
          {
              drawRect();
          }
  
          private void nupHoehe_ValueChanged(object sender, EventArgs e)
          {
              drawRect();
          }
  
          private void btnEinfuegen_Click(object sender, EventArgs e)
          {
              if (lbBildelemente.SelectedIndex == 0)
              {
                  zeichneBild();
              }
              else if (lbBildelemente.SelectedIndex == 1)
              {
                  zeichneLine();
              }
              else if (lbBildelemente.SelectedIndex == 2)
              {
                  zeichneRect();
              }
              else if (lbBildelemente.SelectedIndex == 3)
              {
                  zeichneText();
              }
              else if (lbBildelemente.SelectedIndex == 4)
              {
                  zeichneKreis();
              }
          }
  
          private void lbBildelemente_SelectedIndexChanged(object sender, EventArgs e)
          {
              if (lbBildelemente.SelectedIndex == 0) 
              {
                  tbFarbe.Enabled = false;
                  btnFarbe.Enabled = false;
                  nupPen.Enabled = false;
  
                  tbBild.Enabled = true;
                  btnBild.Enabled = true;
  
                  tbText.Enabled = false;
  
                  tbSchrift.Enabled = false;
                  btnSchrift.Enabled = false;
              }
              else if (lbBildelemente.SelectedIndex == 1 || lbBildelemente.SelectedIndex == 2 || lbBildelemente.SelectedIndex == 4)
              {
                  tbFarbe.Enabled = true;
                  btnFarbe.Enabled = true;
                  nupPen.Enabled = true;
  
                  tbBild.Enabled = false;
                  btnBild.Enabled = false;
  
                  tbText.Enabled = false;
  
                  tbSchrift.Enabled = false;
                  btnSchrift.Enabled = false;
              }
              else if (lbBildelemente.SelectedIndex == 3)
              {
                  tbFarbe.Enabled = true;
                  btnFarbe.Enabled = true;
                  nupPen.Enabled = true;
  
                  tbBild.Enabled = false;
                  btnBild.Enabled = false;
  
                  tbText.Enabled = true;
  
                  tbSchrift.Enabled = true;
                  btnSchrift.Enabled = true;
              }
          }
  
          void zeichneBild()
          {
              Graphics g = pbxBild.CreateGraphics();
  
              int X = Convert.ToInt32(nupX.Value);
              int Y = Convert.ToInt32(nupY.Value);
              int B = Convert.ToInt32(nupBreite.Value);
              int H = Convert.ToInt32(nupHoehe.Value);
  
              Bitmap bitmap = new Bitmap(tbBild.Text);
              g.DrawImage(bitmap, X, Y, B, H);
          }
  
          // Als zusatzfunktion kann man die Pinseldicke auswählen
  
          void zeichneRect() 
          {
              Graphics g = pbxBild.CreateGraphics();
  
              Pen Pen = new Pen(tbFarbe.BackColor, Convert.ToInt32(nupPen.Value));
  
              int X = Convert.ToInt32(nupX.Value);
              int Y = Convert.ToInt32(nupY.Value);
              int B = Convert.ToInt32(nupBreite.Value);
              int H = Convert.ToInt32(nupHoehe.Value);
  
              g.DrawRectangle(Pen, X, Y, B, H);
          }
  
          //zusatzfunktion
  
          void zeichneKreis()
          {
              Graphics g = pbxBild.CreateGraphics();
  
              Pen Pen = new Pen(tbFarbe.BackColor, Convert.ToInt32(nupPen.Value));
  
              int X = Convert.ToInt32(nupX.Value);
              int Y = Convert.ToInt32(nupY.Value);
              int B = Convert.ToInt32(nupBreite.Value);
              int H = Convert.ToInt32(nupHoehe.Value);
  
              g.DrawEllipse(Pen, X, Y, B, H);
          }
  
          void zeichneLine()
          {
              Graphics g = pbxBild.CreateGraphics();
  
              Pen Pen = new Pen(tbFarbe.BackColor, Convert.ToInt32(nupPen.Value));
  
              int X = Convert.ToInt32(nupX.Value);
              int Y = Convert.ToInt32(nupY.Value);
              int B = Convert.ToInt32(nupBreite.Value);
              int H = Convert.ToInt32(nupHoehe.Value);
  
              g.DrawLine(Pen, X, Y, X+B, Y+H);
          }
  
          void zeichneText()
          {
              Graphics g = pbxBild.CreateGraphics();
  
              Brush brush = new SolidBrush(tbFarbe.BackColor);
  
              int X = Convert.ToInt32(nupX.Value);
              int Y = Convert.ToInt32(nupY.Value);
  
              g.DrawString(tbText.Text, fontDialog1.Font, brush, X, Y);
          }
  
          private void btnFarbe_Click(object sender, EventArgs e)
          {
              if (colorDialog1.ShowDialog() == DialogResult.OK)
              {
                  tbFarbe.BackColor = colorDialog1.Color;
              }
          }
  
          private void btnSchrift_Click(object sender, EventArgs e)
          {
              // Show the dialog.
              DialogResult result = fontDialog1.ShowDialog();
              // See if OK was pressed.
              if (result == DialogResult.OK)
              {
                  // Get Font.
                  Font font = fontDialog1.Font;
                  // Set TextBox properties.
                  this.tbSchrift.Text = string.Format(font.Name +"; "+ font.Size +"pt");
              }
          }
  
          private void btnBild_Click(object sender, EventArgs e)
          {
              OpenFileDialog openFileDialog = new OpenFileDialog();
              openFileDialog.Filter = "Joint Photographic Experts Group (*.jpg)|*.jpg|"
                  + "EMF Enhanced Metafile Format (*.emf)|*.emf|"
                  + "Graphics Interchange Format (*.gif)|*.gif|"
                  + "Bitmap (*.bmp)|*.bmp|"
                  + "W3C Portable Network Graphics (*.png)|*.png";
  
              if (openFileDialog.ShowDialog(this) == DialogResult.OK)
              {
                  tbBild.Text = openFileDialog.FileName;
              }
          }
  
          private void bildbereichLöschenToolStripMenuItem_Click(object sender, EventArgs e)
          {
              pbxBild.Refresh();
          }
  
          private void bildLadenToolStripMenuItem_Click(object sender, EventArgs e)
          {
              OpenFileDialog openFileDialog = new OpenFileDialog();
              openFileDialog.Filter = "Joint Photographic Experts Group (*.jpg)|*.jpg|"
                  + "EMF Enhanced Metafile Format (*.emf)|*.emf|"
                  + "Graphics Interchange Format (*.gif)|*.gif|"
                  + "Bitmap (*.bmp)|*.bmp|"
                  + "W3C Portable Network Graphics (*.png)|*.png";
  
              if (openFileDialog.ShowDialog(this) == DialogResult.OK)
              {
                  Graphics g = pbxBild.CreateGraphics();
  
                  int X = 0;
                  int Y = 0;
                  int B = Convert.ToInt32(pbxBild.Width);
                  int H = Convert.ToInt32(pbxBild.Height);
  
                  Bitmap bitmap = new Bitmap(openFileDialog.FileName);
                  g.DrawImage(bitmap, X, Y, B, H);
              }
          }
      }
  }
  </code></pre>