I am developing a media player and I'm trying to change value of Progress Bar using mouse cursor.
what i want is to set the value of progress bar where mouse cursor points after clicking + Dragging.
A progress bar is not the correct control to use for this. You should use a TrackBar instead.
But it can be made to work, with about -10 elegance points. The trickiest problem with ProgressBar is that it animates progress. That makes it slow to respond to your mouse moves. That animation can be disabled, but not perfectly. Closest you can get is:
Private Shared Sub ChangeProgress(bar As ProgressBar, e As MouseEventArgs)
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim mousepos = Math.Min(Math.Max(e.X, 0), bar.ClientSize.Width)
Dim value = CInt(bar.Minimum + (bar.Maximum - bar.Minimum) * mousepos / bar.ClientSize.Width)
'' Disable animation, if possible
If value > bar.Value And value < bar.Maximum Then
bar.Value = value + 1
bar.Value = value
Else
bar.Value = value
End If
End If
End Sub
And call it from MouseDown and MouseMove event handlers:
Private Sub ProgressBar1_MouseMove(sender As Object, e As MouseEventArgs) Handles ProgressBar1.MouseMove
ChangeProgress(ProgressBar1, e)
End Sub
Private Sub ProgressBar1_MouseDown(sender As Object, e As MouseEventArgs) Handles ProgressBar1.MouseDown
ChangeProgress(ProgressBar1, e)
End Sub
It is workable, you'll notice that getting to 100% is a bit awkward. But, really, use a TrackBar instead. It was made to do this.
Related
I'm trying to do something that actually looks "simple" since several hours, but I cannot understand how to do it...and lurking over SO or different sites it seems that maybe is not that obvious.
The question is simple: i have a tableLayoutPanel with multiple rows, each one of them contains a panel, that contains several other controls.
I just want that when the mouse enters a row, the row background changes and when the mouse leave that row, it comes back to original color.
These are the simple event trappers, where pnlLayoutRow is the name of the panel containing the other controls:
Private Sub devRowMouseEnter(sender As System.Object, e As EventArgs) Handles pnlLayoutrow.MouseEnter
pnlLayoutrow.BackColor = Drawing.Color.FromArgb(&HFFFFEEAA)
End Sub
Private Sub devRowMouseLeave(sender As System.Object, e As EventArgs) Handles pnlLayoutrow.MouseLeave
pnlLayoutrow.BackColor = Drawing.Color.FromArgb(&HFFE7DEBD)
End Sub
The problem is: mouseEnter is correctly fired each time I enter the row, but Mouseleave is fired as soon as the mouse reach one of the controls inside the panel..that drives me crazy.
In other environment, I would solve this placing a transparent object all over the panel and trapping the mouseEnter and leave for that object..but it seems in VB trasparent objects do not exist.
Hope I have been clear in my explanation..it is pretty late in the night and I'm a bit tired.
thank you in advance
hope someone can help me
Cristiano
This version of your mouse leave event checks that the mouse is still within the bounds of your TableLayoutPanel and if it is then it exits without changing the color
Private Sub devRowMouseLeave(sender As System.Object, e As EventArgs) Handles pnlLayoutRow.MouseLeave
Dim p As Point = Me.PointToClient(MousePosition)
If p.Y > pnlLayoutRow.Top And p.Y < (pnlLayoutRow.Top + pnlLayoutRow.Height) And p.X > pnlLayoutRow.Left And p.X < (pnlLayoutRow.Left + pnlLayoutRow.Width) Then
Exit Sub
Else
pnlLayoutRow.BackColor = Drawing.Color.FromArgb(&HFFE7DEBD)
End If
End Sub
It seems to work ok for me,so I hope it is the same for you.
I've had a Google about mouse polling rates and by default, in windows, it's 125hz which might seem OK. However, if you move the mouse quickly, the mouse will enter and leave the panel more quickly that windows can detect it. Because of this, sometimes the .MouseEnter and .MouseLeave events don't fire. So I have here an alternative which will at least detect when the mouse leaves the panel. Add a Timer you your form called tmrPanelLeave
Private Sub devRowMouseEnter(sender As System.Object, e As EventArgs) Handles pnlLayoutRow.MouseEnter
pnlLayoutRow.BackColor = Drawing.Color.FromArgb(&HFFFFEEAA)
tmrPanelLeave.Start()
End Sub
Private Sub tmrPanelLeave_Tick(sender As Object, e As EventArgs) Handles tmrPanelLeave.Tick
Dim p As Point = Me.PointToClient(MousePosition)
If p.Y > pnlLayoutRow.Top And p.Y < (pnlLayoutRow.Top + pnlLayoutRow.Height) And p.X > pnlLayoutRow.Left And p.X < (pnlLayoutRow.Left + pnlLayoutRow.Width) Then
Exit Sub
Else
pnlLayoutRow.BackColor = Drawing.Color.FromArgb(&HFFE7DEBD)
tmrPanelLeave.Stop()
End If
End Sub
I want in a MouseDown event in a Winforms to be able to do some code until either a certain amount of seconds or MouseUp event has been fired off
I want something like this
Dim counter As Integer = 0
Do Until Button.Key IsNot MouseDown
counter += 1
If counter > 3
Exit Do Loop
End If
Application.DoEvents()
Loop
Right now I have it like
seconds = 0
m_mouseButtonIsNotDown = False
Do Until m_mouseButtonIsNotDown
If e.Button = Windows.Forms.MouseButtons.Left Then
seconds += 1
If seconds > 5 Then
m_mouseButtonIsNotDown = True
End If
End If
Application.DoEvents()
Loop
The desired behavior I am looking for is if they let go of the button within a certain amount of loops to handle the code that would be if it was just a simple mouseclick event. If the mousedown has been held for more then 3 or 5 seconds switch to drag and drop mode so they can move the desired row to another datagridview. I have tried to use Timer and StopWatch classes and it doesn't work how I would have expected it too. It doesn't work if I have a mouseclick event and the mousedown event since the mousedown executes over the basic click event. Any ideas? Keep in mind that it can't be dependent on a mouseUp event since the drag and drop can't have it.
Never, never use DoEvents() to solve a problem. A Timer can be made to work. But it is not the correct way to go about it. You want to start a drag when the user moved the mouse enough while holding down the left button. Like this:
Private downPos As Point
Private Sub DataGridView1_MouseDown(sender As Object, e As MouseEventArgs) Handles DataGridView1.MouseDown
downPos = e.Location
End Sub
Private Sub DataGridView1_MouseMove(sender As Object, e As MouseEventArgs) Handles DataGridView1.MouseMove
If e.Button = MouseButtons.Left Then
If Math.Abs(downPos.X - e.X) >= SystemInformation.DoubleClickSize.Width Or
Math.Abs(downPos.Y - e.Y) >= SystemInformation.DoubleClickSize.Height Then
Dim hit = DataGridView1.HitTest(downPos.X, downPos.Y)
If hit.RowIndex >= 0 Then
'' Start dragging row
''...
End If
End If
End If
End Sub
I have code to drag files into a textbox, but I would also like to have code to drag the file out of the textbox, thus clearing it.
Anybody have any ideas?
You can use the DoDragDrop method to do a drag & drop operation. It returns a DragDropEffects value that specifies if the data actually has been dropped somewhere in which case you can clear the text box.
Since a drag & drop operation shouldn't start before the mouse has been moved a bit while pressing the mouse button you need to check for that in the MouseDown and MouseMove events. SystemInformation.DragSize tells you how far the mouse should be moved before a drag & drop operation starts.
In the MouseDown event check if you actually want to start a drag (i.e left button is pressed and text box actually contains text). Then create a rectangle using the mouse location and the size given by SystemInformation.DragSize. In the MouseMove event check if the mouse is dragged outside the rectangle and call DoDragDrop:
Private _dragStart As Boolean
Private _dragBox As Rectangle
Private Sub srcTextBox_MouseDown(sender As Object, e As MouseEventArgs) Handles srcTextBox.MouseDown
' a drag starts if the left mouse button is pressed and the text box actually contains any text
_dragStart = e.Button = MouseButtons.Left And Not String.IsNullOrEmpty(srcTextBox.Text)
If _dragStart Then
Dim dragSize As Size = SystemInformation.DragSize
_dragBox = New Rectangle(New Point(e.X - (dragSize.Width \ 2),
e.Y - (dragSize.Height \ 2)), dragSize)
End If
End Sub
Private Sub srcTextBox_MouseUp(sender As Object, e As MouseEventArgs) Handles srcTextBox.MouseUp
_dragStart = False
End Sub
Private Sub srcTextBox_MouseMove(sender As Object, e As MouseEventArgs) Handles srcTextBox.MouseMove
If Not _dragStart Or
(e.Button And MouseButtons.Left) <> MouseButtons.Left Or
_dragBox.Contains(e.X, e.Y) Then Return
Dim data As New DataObject()
data.SetData(srcTextBox.Text)
' you can optionally add more formats required by valid drag destinations:
' data.SetData(DataFormats.UnicodeText, Encoding.Unicode.GetBytes(srcTextBox.Text))
' data.SetData("UTF-8", Encoding.UTF8.GetBytes(srcTextBox.Text))
' data.SetData("UTF-32", Encoding.UTF32.GetBytes(srcTextBox.Text))
Dim dropEffect As DragDropEffects = srcTextBox.DoDragDrop(data, DragDropEffects.Move)
If (dropEffect = DragDropEffects.Move) Then
srcTextBox.Text = ""
End If
_dragStart = False
_dragBox = Rectangle.Empty
End Sub
Private Sub destTextBox_DragOver(ByVal sender As Object, ByVal e As DragEventArgs) Handles destTextBox.DragOver
If e.Data.GetDataPresent(GetType(String)) Then
e.Effect = e.AllowedEffect And DragDropEffects.Move
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub destTextBox_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles destTextBox.DragDrop
If e.Data.GetDataPresent(GetType(String)) Then
destTextBox.Text = e.Data.GetData(GetType(String)).ToString()
End If
End Sub
Dragging the mouse in a TextBox usually starts text selection. The above code changes this behavior. Users can't use the mouse any more to select text. This is obviously not a good idea since users wouldn't expect that. To allow both text selection with the mouse and dragging you need to control the selection mechanism. This means you need to create your own text box class.
I would suggest a different approach: Use a label that shows the value as the dragging source and/or destination. To allow editing you can create a hidden text box. If the user double clicks on the label you hide the label and show the text box. After the user finishes editing (by hitting enter or cancel) or if the text box looses focus you hide the text box and show the label again.
Check out the DragLeave Event on the TextBox
Easiest way will probably be to store it as a variable or in clip board if leaving app.
Private Sub TheTextBox_DragLeave(sender As System.Object, e As System.EventArgs) Handles TheTextBox.DragLeave
Clipboard.SetText(TheTextBox.Text)
TheTextBox.Clear() 'Optional
End Sub
Then you'll need to code what happens when you mouse up of course, or drag into or whatever. You could clear the textbox in one of these steps. Depends on your implementation.
I want to combine MouseEnter with MousePressed on a label.
Public Sub populateGrid()
lblTest.BackColor()
lblTest.BackColor = System.Drawing.Color.Red
gbWorkflow.Controls.Add(lblTest)
For j As Integer = 1 To 40
For i As Integer = 1 To 20
Dim L As New Label
L.Size = New Size(30, 30)
L.Text = "L:" + i.ToString + j.ToString
L.BackColor = Color.AliceBlue
Dim x, y As Integer
Dim loc As Point = gbWorkflow.Location
y = loc.Y * (i * 8) '- (gbWorkflow.Height + L.Size.Height) * i
x = loc.X * (j * 8)
L.Location = New Point(x, y)
gbWorkflow.Controls.Add(L)
AddHandler L.MouseEnter, AddressOf L_Enter
AddHandler L.MouseLeave, AddressOf L_Leave
Next
Next
End Sub
Private Sub L_Enter(ByVal sender As Object, ByVal e As System.EventArgs)
Dim TheLabel As Label = CType(sender, Label)
TheLabel.BackColor = Color.Red
End Sub
My idea was to create a method that triggers on MouseDown and changes the value of a boolean variable. I would then use that variable as a condition to apply the changes to the labels. However, that doesnt seem to work...
How can I achieve this in the best way? Or, at all?
Pseudo-code:
When mouse enter label:
if left mousebutton is pressed then
do stuff with the label
Edit:
It also has to work when the mousebutton is still pressed and the cursor is dragged over several labels. All the labels that the cursor crosses while the left button is pressed should be changed.
The MouseDown event delivers what you are after. Sample code:
Private Sub L_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs)
If (e.Button = Windows.Forms.MouseButtons.Left) Then
'Do stuff
End If
End Sub
Adding the event handler:
AddHandler L.MouseDown, AddressOf L_MouseDown
--- UPDATE
As said, you cannot accomplish directly what you want but there are many alternative ways to deliver an equivalent performance. For example:
Boolean flag indicating whether one of the target labels has been clicked (MouseDown) + MouseEnter performing the modifications only if this flag is true. Sample code:
Private LWasClicked As Boolean = False
Private Sub L_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs)
If (e.Button = Windows.Forms.MouseButtons.Left And Not LWasClicked) Then
LWasClicked = True
End If
End Sub
Private Sub L_MouseEnter(sender As Object, e As System.EventArgs)
If (LWasClicked) Then
'Do stuff
End If
End Sub
With the code above, you can "activate the editing" by just clicking on any label (or on a specific one); once it is activated, you can just pass the mouse over any label and the actions will be performed. You will also have to set an event to de-activate this behaviour (example: new Click/MouseDown). As you can see, this delivers an equivalent performance to what you want and is compatible with how events work.
CLARIFICATION: I think that this (or any other alternative on these lines) delivers an excellent performance. If still you don't want that and prefer to do everything with the mouse-button pressed, you would have to rely on something different (e.g., position of the mouse on the screen, analysis triggered by other means; or even events from different threads). What is clear is that what you aim cannot be accomplished with one-thread events of different controls (a new event cannot be started before the previous one has ended).
I currently use this code to fix the bug where if you click somewhere on the Horizontal TrackBar it jumps to the middle then to the end of the TrackBar. So this code fixes that bug, which now jumps to the location you click.
But still a problem remains when I keep my mouse down and move it around the TrackBar the slider should follow but it just resets to beginning position, how do I make it follow right on top of cursor? would I need a timer control for that?
Private Sub tbTest_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tbTest.MouseDown
Dim dblValue As Double
'Jump to the clicked location, bug FIX.
dblValue = (Convert.ToDouble(e.X) / Convert.ToDouble(tbTest.Width)) * (tbTest.Maximum - tbTest.Minimum)
tbTest.Value = Convert.ToInt32(dblValue)
End Sub
Make the method handle both the MouseDown() and MouseMove() events like this:
Private Sub tbTest_MovePointer(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tbTest.MouseDown, tbTest.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim dblValue As Double
'Jump to the clicked location, bug FIX.
dblValue = (Convert.ToDouble(e.X) / Convert.ToDouble(tbTest.Width)) * (tbTest.Maximum - tbTest.Minimum)
tbTest.Value = Convert.ToInt32(dblValue)
End If
End Sub
*Note the multiple events listed after the Handles keyword at the end of the first line. I also added a check to ensure the left mouse button is down.