allowing a user to click on a progressbar

E

Eps

Hi there,

Is there a way to get the progressbar value when a user clicks on it ?

I am using wpf at the moment but windows forms solutions would be
welcome too.

I get the the x and y positions of the mouse click but I don't know how
to translate that into the value of the progressbar.

any help appreciated.
 
P

Paul E Collins

Eps said:
Is there a way to get the progressbar value when a user clicks on it
?

Unless I'm missing something in your problem description, you don't
need to do anything with x- and y-co-ordinates. Just handle the
ProgressBar's own Click event.

private void progressBar1_Click(object sender, EventArgs e)
{
MessageBox.Show(((ProgressBar) sender).Value.ToString());
}

Eq.
 
E

Eps

Paul said:
Unless I'm missing something in your problem description, you don't
need to do anything with x- and y-co-ordinates. Just handle the
ProgressBar's own Click event.

private void progressBar1_Click(object sender, EventArgs e)
{
MessageBox.Show(((ProgressBar) sender).Value.ToString());
}

Eq.

hmmm, the wpf progress bar does not seem to have a Click event

anyone know how to do this in wpf ?
 
L

Liz

Eps said:
Paul E Collins wrote:
hmmm, the wpf progress bar does not seem to have a Click event

anyone know how to do this in wpf ?


there are all kinds of Mouse events exposed: MouseUp, MouseDown,
MouseLeftButtonUp, etc ..... one of them should fit your needs

clicking on a ProgressBar seems a bit counter-intuitive though, no? if the
numeric value is important why don't you just post it on the form at an
appropriate interval? what I don't understand about the PB is why there is
no ValueChanged event .. (there is in WPF but it refers to the max and min
range values)

L
 
E

Eps

Liz said:
there are all kinds of Mouse events exposed: MouseUp, MouseDown,
MouseLeftButtonUp, etc ..... one of them should fit your needs

clicking on a ProgressBar seems a bit counter-intuitive though, no? if the
numeric value is important why don't you just post it on the form at an
appropriate interval? what I don't understand about the PB is why there is
no ValueChanged event .. (there is in WPF but it refers to the max and min
range values)

L

I am trying to use the progress bar to represent the playing of a mp3, I
guess its a bit of a hack to try and allow the user to change the value
of a progressbar by clicking with the mouse, not what the progress bar
is for.

I am sure there is some maths i could do to get an approximate value
using the X position of the mouse cursor, so I will look in to that.
 
L

Liz

I am trying to use the progress bar to represent the playing of a mp3, I
guess its a bit of a hack to try and allow the user to change the value of
a progressbar by clicking with the mouse, not what the progress bar is
for.

I am sure there is some maths i could do to get an approximate value using
the X position of the mouse cursor, so I will look in to that.

yeah, you could do that with some simple arithmetic using x,y .. pixel
length of the progress bar, etc ... should work ..; meanwhile, you might
look at doing it with the TrackBar control, which is designed to get input
values along a continuous range ... but it doesn't have quite the same
aesthetics to it ...

thing with this is that I don't think I'd want a discrete click to trigger
changes in the value of the Prog Bar; imo, I think you really want to use
the MouseMove event .. the problem you need to solve there is that the mouse
does not have to be down for the event to fire, which is bound to lead to
unintended changes in track position

I think it'll be a little tough with progress bar because the player (WMP??)
will not likely respond immediately to the calculated change in track
position; you need to defer the re-positioning of the track until the user
has stopped "turning the dial" .. like maybe wait 300 ms until a track
position change is effected

L
 
P

Paul E Collins

Liz said:
thing with this is that I don't think I'd want a discrete click to
trigger changes in the value of the Prog Bar; imo, I think you
really want to use the MouseMove event .. the problem you need to
solve there is that the mouse does not have to be down for the event
to fire, [...]

Unless WPF has really crippled everything beyond repair, you can look
at the MouseEventArgs.Buttons property.

Eq.
 
L

Liz

Paul E Collins said:
Liz said:
thing with this is that I don't think I'd want a discrete click to
trigger changes in the value of the Prog Bar; imo, I think you really
want to use the MouseMove event .. the problem you need to solve there is
that the mouse does not have to be down for the event to fire, [...]
Unless WPF has really crippled everything beyond repair, you can look at
the MouseEventArgs.Buttons property.

BINGO! ... with that information in hand, this seems to do what the OP was
looking for:

private void progressBar1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
decimal pos = 0M;
pos = ((decimal)e.X / (decimal)progressBar1.Width) * 100;
pos = Convert.ToInt32(pos);

if (pos >= progressBar1.Minimum && pos <= progressBar1.Maximum)
{
progressBar1.Value = (int)pos;

// info only:
label5.Text = e.X.ToString() + " / " +
progressBar1.Width.ToString() + " [" +
(Convert.ToInt32(pos).ToString()).ToString() + "]";
}
}
}
L
 
E

Eps

I had already implemented it as below, but thanks for suggesting the
mouse move event, it works even better now ! (I didn't think I would be
able to get it to support draging the progress bar).

private double SetProgressBarValue(double MousePosition)
{
double Length = m_mp3play.CurrentSongLength;

double ratio = MousePosition / progressBar1.ActualWidth;
double ProgressBarValue = ratio * progressBar1.Maximum;
m_mp3play.CurrentPosition = ProgressBarValue;
return ProgressBarValue;
}

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

I have implemented the mouse move event like so...

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

This is all WPF code.

Thanks for your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top