title: C#: Change Panel Location via Mouse Location
body: <p>I've got a User Control consisting of Panels and Labels.
  <code>MinPanel</code>, <code>ValuePanel</code>, and <code>MaxPanel</code>.</p>
  
  <p>I'm trying to allow the user to drag the panels much like you would in a <code>TrackBar</code>.</p>
  
  <p>My problem is that when I click and attempt to drag the <code>MinPanel</code>, it jump around starting at 0, then to 200, then to other random values. It seems to be attempting to reset to it's default value each time I drag it.</p>
  
  <pre><code>public partial class ToolboxCustomTrackBar : UserControl
  {
      private int min = 0;
      private int max = 1000;
      private int selectedMin = 0;
      private int selectedMax = 1000;
      private int selectedValue = 400;
      private int selectionWidth = 0;
      private int labelHeight = 10;
  
      public int Min
      {
          get { return min; }
          set
          {
              min = value;
              Invalidate();
          }
      }
      public int Max
      {
          get { return max; }
          set
          {
              max = value;
              Invalidate();
          }
      }
      public int SelectedMin
      {
          get { return selectedMin; }
          set
          {
              selectedMin = value;
              Invalidate();
          }
      }
      public int SelectedMax
      {
          get { return selectedMax; }
          set
          {
              selectedMax = value;
              Invalidate();
          }
      }
      public int SelectedValue
      {
          get { return selectedValue; }
          set { selectedValue = value; Invalidate(); }
      }
      public int LabelHeight
      {
          get { return labelHeight; }
          set { labelHeight = value; Invalidate(); }
      }
  
      public ToolboxCustomTrackBar()
      {
          InitializeComponent();
          SetStyle(ControlStyles.AllPaintingInWmPaint, true);
          SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
  
          backdropPanel.Width = Width;
          backdropPanel.Height = Height;
  
          SelectedMin = Min;
          SelectedMax = Max;
          SelectedValue = (Max - Min) / 2;
          selectionWidth = Max - Min;
          Invalidate();
  
          backdropPanel.BackColor = Color.LightBlue;
          minPanel.BackColor = Color.DarkRed;
          maxPanel.BackColor = Color.DarkGreen;
          valuePanel.BackColor = Color.Black;
  
          backdropPanel.Location = new Point(Min, backdropPanel.Location.Y);
      }
  
      private void ToolboxCustomTrackBar_Paint(object sender, PaintEventArgs e)
      {
          backdropPanel.Location = new Point(0, LabelHeight);
          backdropPanel.Width = Width;
          backdropPanel.BackColor = Color.AliceBlue;
  
          minPanel.Location = new Point(SelectedMin * Width / (Max - Min), backdropPanel.Location.Y);
  
          maxPanel.Location = new Point(SelectedMax * Width / (Max - Min), backdropPanel.Location.Y);
  
          valuePanel.Location = new Point((SelectedValue) * Width / (Max - Min), backdropPanel.Location.Y);
  
          minLabel.Location = new Point(SelectedMin - (minLabel.Width / 2) + (minPanel.Width / 2), backdropPanel.Location.Y - LabelHeight);
          minLabel.Text = SelectedMin.ToString();
  
          maxLabel.Location = new Point(SelectedMax - (maxLabel.Width / 2) + (maxPanel.Width / 2), backdropPanel.Location.Y - LabelHeight);
          maxLabel.Text = SelectedMax.ToString();
  
          valueLabel.Location = new Point(SelectedValue - (valueLabel.Width / 2) + (valuePanel.Width / 2), backdropPanel.Location.Y - LabelHeight);
          valueLabel.Text = SelectedValue.ToString();
      }
  
      private void minPanel_MouseMove(object sender, MouseEventArgs e)
      {
          if (e.Button != MouseButtons.Left) return; //ensure user left-clicked
  
          int pointedValue = Min + e.X * (Max - Min) / Width; //selectionWidth?
          SelectedMin = pointedValue;
      }
  }
  </code></pre>
  
  <p>I've cut some unrelated bits out.</p>