2017-09-02 79 views
0

我有一個由面板和標籤組成的用戶控件。 MinPanelValuePanelMaxPanelC#:通過鼠標位置更改面板位置

我想讓用戶可以像在TrackBar中一樣拖動面板。

我的問題是,當我點擊並嘗試拖動MinPanel時,它會從0開始,然後跳轉到200,然後跳轉到其他隨機值。它似乎試圖重新設置爲默認值,每次我拖動它。

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; 
    } 
} 

我已經削減了一些不相關的數據。

回答

0

我不完全明白爲什麼,但我需要使用+=而不是=

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; 
    SelectedMin += pointedValue; 
}