Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have an app where i add images programmatically to simulate the desktop shortcuts.... the idea is that i can drag the images around and click on them to start a process.
The app creates the mouse events via addHandler in order to drag the image around...
it uses 1)mousedown: it sets a boolean to true in order to indicate the drag operatio has started 2)mousemove: calculates the position during movement and updates the image position 3)mouseup: sets the boolean to false

the problem is that if i add a Mouseclick event it is always triggered as soon as the event museup occurs, because a click is just a mousedown+mouseup.
Is there a workaround to avoid this?
Thanks in advance
Alex
Posted
Comments
BotCar 29-Aug-14 9:06am    
When do you want the mouseclick to trigger?
ADemontis 29-Aug-14 9:16am    
when i click the mouse, of course.... it's mouseclick...
I want to be able to move the picturebox and to open a process by clicking on it.
BotCar 29-Aug-14 9:24am    
Well how do you define "when I click the mouse"? Is it when you press the button down? Or when you release the button? Or when you press the button down and release it shortly thereafter?

What I'm getting at is this: How is the behaviour you observe different from the behaviour you expect or want?
ADemontis 29-Aug-14 9:41am    
'to click the mouse' is pushing the button and releasing... a click is formally made up of a mousedown followed by a mouseup events. But te mpuseclick event seems to be unusable along with the 2 single parts that make a click (mousedown and mouseup). I am lloking for a workaround to use the 3 events on the same object.
Sergey Alexandrovich Kryukov 29-Aug-14 9:49am    
You did not tag your application type or UI library you want to use.
And there is no such thing as "at the same time".
—SA

Your best bet is to add another bool: ignoreClick.
Set it to true in the MouseUp handler if it was doing a drag and check it in the Click handler.
Reset it in two places: at the end of the Click handler, and in the MouseDown event just to be on the safe side.


I got the order of execution wrong: when you release the mouse you get events in this order:
Click
MouseClick
Up
So it's even easier: all you have to do is check if you are dragging and ignore the event if you are - the Up event handler will clear the drag flag so subsequent ones will work.
 
Share this answer
 
v2
Comments
ADemontis 29-Aug-14 13:21pm    
what do you exatcly mean by 'check it in the click handler'?
I have modified the code:

Friend Sub newbutton_MouseClick(sender As Object, e As System.Windows.Forms.MouseEventArgs)

If ignoreclick = True And drag = False Then
' reads the file where the process definition i stored and associates the process to the button depending on the button name
Try
LeggiDizionario(sender, My.Application.Info.DirectoryPath & "\custom\custombuttons.txt")
Process.Start(My.Settings.buttprocess)
ignoreclick = False
Catch ex As Exception
MsgBox("Cannot start process - check definition")
End Try
dict.Clear()
Else
MsgBox(ignoreclick.ToString)
End If


End Sub
Private Sub newbutton_MouseDown(sender As Object, e As MouseEventArgs)
drag = True
ignoreclick = False
mousex = Windows.Forms.Cursor.Position.X - sender.Left
mousey = Windows.Forms.Cursor.Position.Y - sender.Top
End Sub
Private Sub newbutton_MouseUp(sender As Object, e As MouseEventArgs)
drag = False
ignoreclick = True
End Sub
Private Sub newbutton_MouseMove(sender As Object, e As MouseEventArgs)
If drag = True And ignoreclick = False Then
sender.Top = Windows.Forms.Cursor.Position.Y - mousey
sender.Left = Windows.Forms.Cursor.Position.X - mousex
End If
End Sub


the drag part works well but i don't know why i always get the ignoreclick to False status
OriginalGriff 29-Aug-14 14:07pm    
Answer updated.
ADemontis 30-Aug-14 10:36am    
i have modified it this way but still does not work

Friend Sub newbutton_MouseClick(sender As Object, e As System.Windows.Forms.MouseEventArgs)
If drag = False Then
Try
LeggiDizionario(sender, My.Application.Info.DirectoryPath & "\custom\custombuttons.txt")
Process.Start(My.Settings.buttprocess)
ignoreclick = False
Catch ex As Exception
MsgBox("Cannot start process - check definition")
End Try
dict.Clear()
End If
End Sub
I have found out that the Button object is not viable for this particular issue, in no way there is a method to implement the MouseClick or the MouseDoubleClick in the same object defining the mousedown and mouseup event programmatically.
I had to change the newbutton type to Picturebox and now everything works... except that picturebox does not have a 'Text' property so i had to use a Tooltip to show the icon name.

Thanks to everyone for your aid.
Alex

I report here the full code section in case anyone finds it useful... the basic idea is to simulate windows' desktop shortcut system, that is, icons on the desktop that an be dragged and can start a process.


'start: saves icon name and associated process in 2 variables and in a file
Private Sub createiconbutton_Click(sender As System.Object, e As System.EventArgs) Handles createiconbutton.Click
My.Settings.butttext = iconnametext.Text
My.Settings.buttprocess = iconprocesstext.Text
My.Settings.Save()
My.Settings.Reload()
Dim path As String = My.Application.Info.DirectoryPath & "\custom\custombuttons.txt"
Dim sw As StreamWriter
If File.Exists(path) = False Then
sw = File.CreateText(path)
sw.WriteLine(iconnametext.Text & "^" & iconprocesstext.Text)
sw.Flush()
sw.Close()
Else
sw = File.AppendText(path)
sw.WriteLine(iconnametext.Text & "^" & iconprocesstext.Text)
sw.Flush()
sw.Close()
End If
Dim NewButton As New PictureBox
Dim myToolTip As New ToolTip()
'sets the new icon properties
With NewButton
.Width = 65
.Height = 65
.BackColor = Color.Transparent
.BackgroundImage = My.Resources.App_icon 'assign here whatever image you want
.BackgroundImageLayout = ImageLayout.Zoom
.Text = My.Settings.butttext
.Font = New Font("Kokila", 14, FontStyle.Bold, GraphicsUnit.Pixel)
.Top = 100
.Left = 100
myToolTip.SetToolTip(NewButton, My.Settings.butttext)
End With
Me.Controls.Add(NewButton)
AddHandler NewButton.DoubleClick, AddressOf newbutton_DoubleClick
AddHandler NewButton.MouseUp, AddressOf newbutton_MouseUp
AddHandler NewButton.MouseDown, AddressOf newbutton_MouseDown
AddHandler NewButton.MouseMove, AddressOf newbutton_MouseMove
End Sub
Friend Sub newbutton_DoubleClick(sender As Object, e As MouseEventArgs)
Try
'leggidizionario is the function that reads the file and splits the string into key and value
LeggiDizionario(sender, My.Application.Info.DirectoryPath & "\custom\custombuttons.txt")
Process.Start(My.Settings.buttprocess)
Catch ex As Exception
MsgBox("Cannot start process - check definition: " & My.Settings.buttprocess & " -- " & My.Settings.butttext)
End Try
dict.Clear()
End Sub
Private Sub newbutton_MouseDown(sender As Object, e As MouseEventArgs)
drag = True
mousex = Windows.Forms.Cursor.Position.X - sender.Left
mousey = Windows.Forms.Cursor.Position.Y - sender.Top
End Sub
Private Sub newbutton_MouseUp(sender As Object, e As MouseEventArgs)
drag = False
End Sub
Private Sub newbutton_MouseMove(sender As Object, e As MouseEventArgs)
If drag = True Then
sender.Top = Windows.Forms.Cursor.Position.Y - mousey
sender.Left = Windows.Forms.Cursor.Position.X - mousex
End If
End Sub
Private Sub LeggiDizionario(sender As Object, FullPathFileName As String)
Dim lines As String() = IO.File.ReadAllLines(FullPathFileName)
For Each line As String In lines
Dim kv As KeyValuePair(Of String, String) = ToKeyValuePair(line)
dict.Add(kv.Key, kv.Value)
If kv.Key = sender.text Then
My.Settings.buttprocess = kv.Value
End If
Next
End Sub
'this function splits the 2 parts of the definition
Public Function ToKeyValuePair(pair As String) As KeyValuePair(Of String, String)
Dim two As String() = pair.Split("^")
Return New KeyValuePair(Of String, String)(two(0), two(1))
End Function
 
Share this answer
 
v2

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