title: C# Drawing Lines with TextBox
body: <p>I have an application where I can add a textBox on the screen and move it.
  When I add more than two textBox, I double-click on top of two textbox and a line connects both.</p>
  
  <p>My question is: How to make the line move along with the textBox?
  code below:</p>
  
  <pre><code>public partial class principal : Form
  {
      int posMouseFormX, posMouseFormY;
      int posMouseTXT_X, posMouseTXT_Y;
      int posActTXT_X, posActTXT_Y;
      bool txtPressionado = false;
      int qntClick;
      Pen myPen = new Pen(System.Drawing.Color.DarkGreen, 1);
      Graphics Tela;
      List&lt;TextBox&gt; listaNós = new List&lt;TextBox&gt;();
      List&lt;Point&gt; origem = new List&lt;Point&gt;();
      List&lt;Point&gt; destino = new List&lt;Point&gt;();
      Point ponto1, ponto2;
      ContextMenuStrip menu;
  
      public principal()
      {
          InitializeComponent();
          menu = new ContextMenuStrip();
          menu.Items.Add("Remover");
          menu.ItemClicked += new ToolStripItemClickedEventHandler(contextMenuStrip1_ItemClicked);
      }
  
      //TextBox event when the mouse moves over the TXT
      private void txtMover_MouseMove(object sender, MouseEventArgs e)
      {
          TextBox textBox = sender as TextBox;
  
          posMouseFormX = textBox.Location.X + e.Location.X;
          posMouseFormY = textBox.Location.Y + e.Location.Y;
  
          if (txtPressionado == true) moverTxt(textBox);
      }
  
      //Retrieve the X and Y coordinates where clicked within the component. 
      private void txtMover_MouseDown(object sender, MouseEventArgs e)
      {
          posMouseTXT_X = e.Location.X;
          posMouseTXT_Y = e.Location.Y;
          txtPressionado = true;
      }
  
      private void txtMover_MouseUp(object sender, MouseEventArgs e)
      {
          txtPressionado = false;
      }
  
      private void moverTxt(TextBox a)
      {
          a.Location = new System.Drawing.Point(posMouseFormX - posMouseTXT_X, posMouseFormY - posMouseTXT_Y);
          posActTXT_X = a.Location.X;
          posActTXT_Y = a.Location.Y;
  
          System.Drawing.Graphics graphicsObj;
          graphicsObj = this.CreateGraphics();
      }
  
      //insert new TextBox
      private void sb_Inserir_No_Click(object sender, EventArgs e)
      {
  
          TextBox noFilho = new TextBox();
  
          noFilho = new System.Windows.Forms.TextBox();
          noFilho.Location = new System.Drawing.Point(379, 284);
          noFilho.Size = new System.Drawing.Size(100, 30);
          noFilho.TabIndex = 20;
          noFilho.Text = "";
          noFilho.BackColor = Color.White;
  
          posActTXT_X = noFilho.Location.X;
          posActTXT_Y = noFilho.Location.Y;
  
          this.Controls.Add(noFilho);
          noFilho.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
          noFilho.DoubleClick += new System.EventHandler(this.textBox1_Click);
          noFilho.MouseUp += new System.Windows.Forms.MouseEventHandler(txtMover_MouseUp);
          noFilho.MouseDown += new System.Windows.Forms.MouseEventHandler(txtMover_MouseDown);
          noFilho.MouseMove += new System.Windows.Forms.MouseEventHandler(txtMover_MouseMove);
          noFilho.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);
  
          noFilho.ContextMenuStrip = menu;   
      }
  
      //event to resize the txt on the screen as the content.
      private void textBox1_TextChanged(object sender, EventArgs e)
      {
          TextBox textBox1 = sender as TextBox;
          Size size = TextRenderer.MeasureText(textBox1.Text, textBox1.Font);
          textBox1.Width = size.Width + 10;
          textBox1.Height = size.Height;
      }
  
      //Event to control the connection between two give us when double click on the textbox
      private void textBox1_Click(object sender, EventArgs e)
      {
          TextBox textBox1 = sender as TextBox;
          int meio = textBox1.Size.Width / 2;
          Tela = CreateGraphics();
  
          qntClick = qntClick + 1;
  
          if (this.qntClick == 1)
          {
              origem.Add(ponto1);
              ponto1 = new Point(textBox1.Location.X + meio, textBox1.Location.Y);
          }
          if (this.qntClick == 2)
          {
              qntClick = 0;
              destino.Add(ponto2);
              ponto2 = new Point(textBox1.Location.X + meio, textBox1.Location.Y);
              DesenhaSeta(Tela, ponto1, ponto2);
          }
      }
  
      //draw arrow between two TXT
      void DesenhaSeta(Graphics Tela, Point x, Point y)
      {
          myPen.StartCap = LineCap.Triangle;
          myPen.EndCap = LineCap.ArrowAnchor;
          Tela.DrawLine(myPen, x, y);
      }
  
      private void contextMenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
      {
          ContextMenuStrip menu = sender as ContextMenuStrip;
          //recuperando o controle associado com o contextmenu
          Control sourceControl = menu.SourceControl;
  
          DialogResult result = MessageBox.Show("Tem Certeza que deseja remover o nó selecionado?", "Excluir", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
  
          if(result == DialogResult.Yes)
          {
              sourceControl.Dispose();
          }
      }
  
      private void textBox1_KeyDown(object sender, KeyEventArgs e)
      {
          // Determine whether the key entered is the F1 key. Display help if it is.
          if (e.KeyCode == Keys.Space)
          {
              TextBox textBox1 = sender as TextBox;
              novoNó tela = new novoNó(textBox1.Text);
              tela.Show();
          }
      }
  }
  </code></pre>