progress bar click => change current track play position - c#

i am making a music player in winforms. i have a progress bar and when i click on a position along the progress bar, i want to get the int for that position ( from 1 to 100 , i.e. for when i want to get to a certain point in my song ) . How can i do that ?
Regards,
Alexandru Badescu

Use TrackBar control, and this is may an advance one, I hope it helps you.
Good luck.

Try this:
'Using The Click or MouseDown or any Mouse Event
Dim Value As Integer= Me.PointToClient(MousePosition).X-Progressbar.Bounds.X
Progressbar.value= Value

you can use a control that have to be placed on the progress bar --like a small bullet .
now if you use the code
progressbar1.value=control.location.x/y
place the name of the control at the position of control x/y decides the coordinates.
make a note that you have to move the control on the progress bar for simplifications use a picture box and use the keydown event to move it side by side

This one brings up a value in relation to the position where the click was done on the progressbar. The value range here is calculated from 0 to 100%. Its also in relation to the width of the progressbar so the range will stay fixed to percentage logic.
private void progressBar1_Click(object sender, EventArgs e)
{
// Get mouse position(x) minus the width of the progressbar (so beginning of the progressbar is mousepos = 0 //
float absoluteMouse = (PointToClient(MousePosition).X - progressBar1.Bounds.X);
// Calculate the factor for converting the position (progbarWidth/100) //
float calcFactor = progressBar1.Width / (float)progressBar1.Maximum;
// In the end convert the absolute mouse value to a relative mouse value by dividing the absolute mouse by the calcfactor //
float relativeMouse = absoluteMouse / calcFactor;
// Set the calculated relative value to the progressbar //
progressBar1.Value = Convert.ToInt32(relativeMouse);
}
I know this question was asked a long time ago, but it was out of the right solution. Here it is now.

Well you could use a picturebox to simulate a progress bar ... have a method to partially fill it based on current progress, and wire up the MouseDown event (this will provide you with the mouse position which you can then scale accordingly).

Related

How to set Selection start based on Mouse Position WinForms

I need to set Selection start of a text box based on the mouse position, i tried to load the text box on Double Click, once the text box is loaded, i need to set the selection start based on the Mouse position. (i.e) if a text box contains some values like "abcdef", if the mouse cursor is near "c" when textbox is loaded, then the selection start should be after "c".
I have also tried this
textBox.GetCharIndexFromPosition(e.Location);
but i didn't get it right,
Thanks in advance.
Regards,
Venkatesan R
Putting #Reza's code in the correct event will work just fine:
private void textBox_MouseDoubleClick(object sender, MouseEventArgs e)
{
textBox.Text = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // load the text data here
// now position the caret onto the mouse position
textBox.SelectionStart = textBox.GetCharIndexFromPosition(e.Location);
// and clear a selection
textBox.SelectionLength = 0;
}
Note that you need to use the MouseDoubleClick, not the simple DoubleClick or else you miss the e.Location param!
This is the simplest and most direct way to get the mouse coordinates relative to the TextBox.
If your loading method is complex you can call it by passing in the MouseEventArgs e but simply calling it instead of the textBox.Text = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; is the most natural way.
If you want to you could also use
textBox.SelectionStart = textBoxtextBox1.PointToClient(Control.MousePosition));
This will work in any event or method. PointToClient will calulate the relative position from the screen position Control.MousePosition.

Is it possible to get which character of text is clicked on C# form?

I am trying to get which of the * is clicked in text of label as in picture.
NOTE : Number of * is not known at first.User inputs it.So the position of mouse click is not useful.
Answer is :
X of first *'s location is known, equals = 10 .The asterisks are spaced equally,distance between two * is ~15 pixel .
When user clicks on one of asterisks , index of asterisks can be calculated as the code below
private void label4_MouseDoubleClick(object sender, MouseEventArgs e)
{
double t = (e.X - 10) / 15.0;
int indexOfClickedAsterisk=(int)Math.Round(t) + 1;
}
It depends. If you are willing to display the text in a TextBox instead of Label (which is what it looks like you're using now), then you can use the GetCharIndexFromPosition method. Just make sure you specify the Point in client coordinates (comes for free if you are handling mouse clicks in the Label control itself).
Note that you can set the TextBox as ReadOnly, assuming you don't want the user actually modifying the text. They will still be able to select the text though.
If you need an actual Label control (e.g. you don't even want selectable text and the grayed-out appearance of a disabled TextBox isn't appropriate), then you'll have to write your own (or find one online that someone else wrote already), as the built-in one doesn't have that functionality. It would not be too difficult. You can use the TextRenderer methods to determine where each character is laid out when drawn and then use that information to correlate character positions with mouse clicks.

C# Y coordinate is always offset

I'm a bit stuck with this one.
I'm writing a visual Semaphore flag signalling application, and I'm having a bit of trouble with the positioning of labels for left and right arms.
This is the code before:
private void leftHandDown()
{
display.DrawLine(penLeftArm, centXCoord, centYCoord, LHDownXCoord, LHDownYCoord);
lblLeftHand.Top = LHDownYCoord;
lblLeftHand.Left = LHDownXCoord;
lblLeftHand.Show();
}
And this is what it looks like:
http://i137.photobucket.com/albums/q221/omar319/sema.png (I don't have any rep to post photos on here). I have set the background to blue, as I'm also trying to pin down another problem (labels leave a white box when they change position, not sure why).
I would like the labels to appear at the end of the hands drawn by the pen (end coordinates LHDownXCoord and LHDownYCoord) but the labels are always offset by -80px on the y-axis. The Right Hand label I have added 75px to the Y axis coordinate.
Any idea what is causing the offset?
Cheers,
Omar
I think the labels need to be offset by the top left coordinates of your display control

How to animate an image in C#

I'm trying to animate an image in C#. Basically, I want to take an image and connect it to another function and based on a scale of 1 to 10, make the image move up or down. By default, the image will start out at 1. I have searched and I've found ways to make shapes move up and down on the screen, but not an actual image. The image is small, say 60x60 pixels. I feel like this should be simple, but I have yet to figure it out. I was thinking of just having an image placed on the Windows form and then basically have it move up or down the y-axis of the form, but I would like it to move smoothly.
Ok, I was able to hook a button up to a timer function and get the button to move smoothly up and down the screen. The button has to keep moving for the duration of the program running. However, I'm having a hard time writing a function that stops the timer and the image(button) from moving once the image reaches a certain location. Without that, the timer continues and the image(button) moves off the screen. I've tried messing with button.Location.Y functions, but I have yet to get it work right. Can anyone please advise? Thanks. Oh yea, once the image(button) reaches Y location of 192 or 447, it should stop moving.
An example of what I have:
private void timer2_Tick(object sender, EventArgs e)
{
button2.Top = button2.Top + 1;
if (button2.Location.Y == button2.Location.Y - 192)
{
timer2.Stop();
timer3.Stop();
}
//if (timer_limit < 100)
//{
// button2.Top = button2.Top + 1;
// timer_limit++;
//}
//else
//{
// timer2.Stop();
//}
}
Several ways to do this. You could just use a PictureBox and change its Location property. Or you could draw the image in the form's OnPaint() override and change the argument to e.Graphics.DrawImage(). You'll then have to call Invalidate() to force the OnPaint method to run. It is the cheapest way.

How can I dynamically move/resize a form?

I am doing an alert system that will show messages 'a la' Messenger, and I want them to move or resize to make them appear.
How can I do this without having to do this:
do
{
this.prop += 1;
} while (this. prop = destination);
You need to make a Timer component and call the form's SetBounds method in the timer's Tick event.
You need a frame rate independent interpolator.
Take a look at: Frame Rate Independent interpolation. Basically, the idea is that you compute what your current position should be based upon the expected animation time and how long you've been animating... This means that the animation will take the same amount of time to go from point a to point b on any hardware.
Of course, you'll need to position the form with the values coming out of this, but thats the easy part!
set form.size property to change size, form.position to change position

Categories

Resources