title: Drawing on to a programmatically created Panel in a C# WinForm?
body: <p>I've got a bunch of questions with certain answer types in my database. I currently use them for questionnaires on an android app, but I'd like to also view them on a desktop application so you can get a rough idea of what it'll look like and to make it easier to add questions and stuff. Buuuuuuut! Adding things programmatically in a C# form seems to be more effort than in android. I've got all the basic ones down(texbox, checkbox, dropdown) but I'm trying to do a drawing one now. </p>
  
  <p>I figured I'd use a Panel since I can draw on it, but it doesn't seem to be working. I've just been browsing and googling what to do in terms of this since I'm not terribly well acquainted with C# Forms. I set a <code>MouseDown</code> and <code>MouseMove</code> event handler and on the <code>MouseMove</code> event handler, I try draw it and the code gets called and it executes the line but nothing happens. It's either something really smaller and silly or I'm going about it in a wrong way. If I can't do it that way, I don't mind any other suggestions as to how I'd do it. It's all done dynamically, so it adds question + answer type to a <code>TableLayoutPanel</code>, so it'd need to be addable to that. As far as I'm aware, a Canvas isn't a Control, so I can't add it. </p>
  
  <p>This is the relevant code:</p>
  
  <pre><code>switch (questionnaireAnswerType.questionnaireAnswerTypeId)
                  {
                      case 1:
                          ComboBox dropDown = new ComboBox();
                          dropDown.Anchor = AnchorStyles.Left;
                          String[] items = question.dropDownContents.Split(';');
                          dropDown.Items.AddRange(items);
                          tablePanel.Controls.Add(dropDown, 1, index);
                          break;
                      case 2:
                          TextBox textBox = new TextBox();
                          textBox.Size = new System.Drawing.Size(70, 20);
                          tablePanel.Controls.Add(textBox, 1, index);
                          break;
                      case 3:
                          CheckBox checkBox = new CheckBox();
                          checkBox.Anchor = AnchorStyles.Left;
                          tablePanel.Controls.Add(checkBox, 1, index);
                          break;
                      case 4:
                          Panel drawPanel = new Panel();
                          System.Drawing.Size size = new System.Drawing.Size();
                          drawPanel.MouseDown += new MouseEventHandler(drawPanel_MouseDown);
                          drawPanel.MouseMove += new MouseEventHandler(drawPanel_MouseMove);
                          size.Height = 100;
                          size.Width = 500;
                          drawPanel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                          drawPanel.Size = size;
                          AddTableRow();
                          tablePanel.Controls.Add(drawPanel, 0, index +1);
                          tablePanel.SetColumnSpan(drawPanel, 2);
                          break;
                  }
  </code></pre>
  
  <p>And these are the mousedown events:</p>
  
  <pre><code> void drawPanel_MouseMove(object sender, MouseEventArgs e)
      {
          if (e.Button == System.Windows.Forms.MouseButtons.Left)
          {
              Line line = new Line();
  
              line.Stroke = System.Windows.SystemColors.WindowFrameBrush;
              line.X1 = currentPoint.X;
              line.Y1 = currentPoint.Y;
              line.X2 = e.Location.X;
              line.Y2 = e.Location.Y;
  
              currentPoint = e.Location;
  
              using (Graphics g = ((Panel) sender).CreateGraphics())
              {
                  g.DrawLine(new Pen(Color.Black, 3), new System.Drawing.Point(currentPoint.X, currentPoint.Y), new System.Drawing.Point(e.Location.X, e.Location.Y));
              }
          }
      }
  
      void drawPanel_MouseDown(object sender, MouseEventArgs e)
      {
          if (e.Button == System.Windows.Forms.MouseButtons.Left)
              currentPoint = e.Location;
      }
  </code></pre>