TechTalkz.com Logo Ask the Experts!

Go Back   TechTalkz.com Technology & Computer Troubleshooting Forums > Tech Support Archives > Programing Languages > C#(C Sharp)

allowing a user to click on a progressbar

C#(C Sharp)

 
 
Thread Tools Display Modes
Unread 13-10-2007, 05:11 PM   #1
Eps
Guest
 
Posts: n/a
allowing a user to click on a progressbar

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.

--
Eps
 
Unread 13-10-2007, 05:11 PM   #2
Paul E Collins
Guest
 
Posts: n/a
Re: allowing a user to click on a progressbar

"Eps" <eps@mailinator.com> wrote:

> 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.


 
Unread 13-10-2007, 05:11 PM   #3
Eps
Guest
 
Posts: n/a
Re: allowing a user to click on a progressbar

Paul E Collins wrote:

> 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 ?

--
Eps
 
Unread 13-10-2007, 07:12 PM   #4
Liz
Guest
 
Posts: n/a
Re: allowing a user to click on a progressbar


"Eps" <eps@mailinator.com> wrote in message
news:u0B1VsbDIHA.5360@TK2MSFTNGP03.phx.gbl...
> 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


 
Unread 13-10-2007, 07:12 PM   #5
Eps
Guest
 
Posts: n/a
Re: allowing a user to click on a progressbar

Liz wrote:

> 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.

--
Eps
 
Unread 13-10-2007, 09:09 PM   #6
Liz
Guest
 
Posts: n/a
Re: allowing a user to click on a progressbar


"Eps" <eps@mailinator.com> wrote in message
news:uW519acDIHA.4752@TK2MSFTNGP04.phx.gbl...

> 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


 
Unread 14-10-2007, 02:04 AM   #7
Paul E Collins
Guest
 
Posts: n/a
Re: allowing a user to click on a progressbar

"Liz" <liz@tiredofspam.com> wrote:

> 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.


 
Unread 14-10-2007, 08:00 AM   #8
Liz
Guest
 
Posts: n/a
Re: allowing a user to click on a progressbar


"Paul E Collins" <find_my_real_address@CL4.org> wrote in message
news:scOdneuB_J7k7ozaRVnyiQA@bt.com...
> "Liz" <liz@tiredofspam.com> wrote:
>
>> 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


 
Unread 14-10-2007, 11:03 AM   #9
Eps
Guest
 
Posts: n/a
Re: allowing a user to click on a progressbar

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.

--
Eps
 
 

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Allowing user to modify their Network connections Blackberry Windows Server 2003 4 22-09-2007 04:08 PM
Dealing with ProgressBar kshama.bafna@gmail.com C#(C Sharp) 3 17-09-2007 01:01 PM
how to use 'marquee' to show circular progressbar windsim C#(C Sharp) 1 05-09-2007 09:02 AM
allowing Internet access with a standard user Jonine Windows Vista All 0 17-08-2007 10:01 PM
User Account Control- Allowing certain programs automatically TimePilot Windows Vista All 4 17-08-2007 09:14 AM


< Home - Windows Help - MS Office Help - Hardware Support >


New To Site? Need Help?

All times are GMT. The time now is 07:47 AM.


vBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO
Copyright © 2005-2013, TechTalkz.com. All Rights Reserved - Privacy Policy
Valid XHTML 1.0 Transitional