Click here to Skip to main content
15,891,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I need to write a customised progress bar that I am using to track progress on a file that is replaying.

I want to be able to click any position in the progress bar container (not the bar itself that is green), and grab the click event. I plan to do this to seek back and forward through the track.

As there is no built in click event on the progressbar for WPF, I used the following to work around:

C#
private double SetProgressBarValue(double MousePosition)
 {
    if (replayer != null)
        replayer.JumpToFrame((int)PBar.Value);
    PBar.Value = PBar.Minimum;
    double ratio = MousePosition / PBar.ActualWidth;
    double ProgressBarValue = ratio * PBar.Maximum;

    return ProgressBarValue;
 }

private void PBar_MouseDown(object sender, MouseButtonEventArgs e)
 {
     double MousePosition = e.GetPosition(PBar).X;
     PBar.Value = SetProgressBarValue(MousePosition);
 }


 private void PBar_MouseMove(object sender, MouseEventArgs e)
 {
     if (e.LeftButton == MouseButtonState.Pressed)
     {
         double MousePosition = e.GetPosition(PBar).X;
         PBar.Value = SetProgressBarValue(MousePosition);

     }
 }


So on mouse down I allow progress to be changed. HOWEVER, the event only fires when clicking the green bar. Not the section of the progress bar control that has no progress held within (for forward tracking my replay file).

Can someone show me a way around this?

Thanks in advance!
Posted
Comments
mccaber82 13-Nov-13 15:54pm    
Thanks Sergey, I could go about creating my own control but i'd be building one that looks and acts identical to the progress bar. Is there any way to just derive from it and extend to include?
Sergey Alexandrovich Kryukov 13-Nov-13 16:07pm    
I have a better idea, please see Solution 1.
—SA

Hi Sergey,

I have not been able to implement what you've advised due to the click event still being obscured by the incomplete portion of the progress bar control. Here is an example

Here is the code I have implemented:


C#
public class UpdatableProgressBar : System.Windows.Controls.ProgressBar
    {
        public delegate void ValueChange(double oldValue, double newValue);
 
        public event ValueChange ValueChanged;
 
        public UpdatableProgressBar():base()
        {
            this.MouseDown += this_MouseDown;
            this.MouseEnter += this_MouseEnter;
            this.MouseMove += this_MouseMove;
            this.MouseLeave += this_MouseLeave;          
        }
 
        private void this_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            this.Cursor = System.Windows.Input.Cursors.Hand;
        }
 
        private void this_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            this.Cursor = System.Windows.Input.Cursors.Arrow;
        }
 
        private double SetProgressBarValue(double MousePosition)
        {
            this.Value = this.Minimum;
            double ratio = MousePosition / this.ActualWidth;
            double ProgressBarValue = ratio * this.Maximum;
 
            if (ValueChanged != null)
                ValueChanged(this.Value, ProgressBarValue);
 
            return ProgressBarValue;
        }
 
        private void this_MouseDown(object sender, MouseButtonEventArgs e)
        {
            double MousePosition = e.GetPosition(this).X;
            this.Value = SetProgressBarValue(MousePosition);
        }
 
        private void this_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                double MousePosition = e.GetPosition(this).X;
                this.Value = SetProgressBarValue(MousePosition);
 
            }
        }
    }


Do you know why part of it is being obscured? Have I misunderstood what you've advised?

Many thanks,
Rob
 
Share this answer
 
v4
First of all, if you hassle is not having a Click event in the class System.Windows.Controls.ProgressBar, this is not a good excuse for crafting such an unnatural and ugly workaround as working with the click of a progress bar's container. No wonder, it's hard to do smoothly.

Instead, "compose" the click event out of available mouse events, such as MouseUp and MouseDown. Please see:
http://msdn.microsoft.com/en-us/library/system.windows.controls.progressbar_events%28v=vs.110%29.aspx[^].

For some background:

After all, why would the author of the ProgressBar would provide Click event, if the click is not needed for its functionality? Remember that Click (in other controls, such as Button) is the "logical" event which does not imply physical mouse event, could be done with the keyboard. In contrast to that, mouse and keyboard events are "raw" events inherited from System.Windows.UIElement:
http://msdn.microsoft.com/en-us/library/system.windows.uielement(v=vs.110).aspx[^].

As soon as you need more abstract (logically higher-level) events, you introduce Click and whatever else.

—SA
 
Share this answer
 
Comments
mccaber82 13-Nov-13 16:01pm    
Hi Sergey, thanks again for getting back to me. I don't necessarily need a click event but just some way to catch when the mouse is down on the progress bar (the area where the bar has not filled yet). And im happy to work with that. The code I have above shows the mouse down handling and that works but only for the area of the control that has progress bar shown. Not for the rest of the control unfortunately. I need to come up with a way to catch the mouse down on the area not highlighted if possible.
Sergey Alexandrovich Kryukov 13-Nov-13 16:09pm    
Exactly. And that's why my advice should work for you. Now, what's the remaining problem? You will be able to handle mouse events on all the client area of the control, both highlighted and not highlighted parts of it. Just try and you will see.
Will you accept the answer formally (green "Accept" button). I think this is all you need. In all cases, your follow-up questions will be welcome.
—SA
mccaber82 13-Nov-13 16:23pm    
Yes no problem. Thanks
mccaber82 13-Nov-13 17:26pm    
Hi Sergey, please see my additional comment, its not actually the solution (although its marked as such).

Advice welcome!
Sergey Alexandrovich Kryukov 13-Nov-13 18:08pm    
Not clear what exactly happened. What do you mean by "obscured"?
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900