PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VB NET QUESTION: Catching a double-click event using addhandler - How?-VBForums
Page 1 of 2 12 LastLast
Results 1 to 40 of 47

Thread: VB NET QUESTION: Catching a double-click event using addhandler - How?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    512

    VB NET QUESTION: Catching a double-click event using addhandler - How?

    Referring back to VB6, on a control array it was possible to add click events and double-click events and the environment would distinguish those and pass the event to the correct procedure. As simple drop-down selection on the object would give you the correct event. They both worked as expected.

    In .NET I have successfully implemented a collection that can be referenced in the same manner giving me an equivalent of the old VB6 dynamic "control array".

    In the code to dynamically add each control to the collection I have to add some event handling manually in order to catch the same mouse events as before, namely click and doubleclick.

    Code:
                AddHandler aPictureBox.MouseDown, AddressOf picThumbIconClickHandler
                AddHandler aPictureBox.DoubleClick, AddressOf picThumbIconDoubleClickHandler
    However, the double-click is only registered if I remove the click or mousedown handler line.

    In the older (and in some respects more useful) VB6 method there is obviously some advanced code behind the scenes that allows the two events to be distinguished from each other but using event handlers in .NET only the click event is registered. I need that doubleclick event to be fired.

    So, I assume that I need to simulate the doubleclick during two single clicks. I will have to note the single click positions and set up a timer on the first click and compare those to the location and time of the second click in order to determine and simulate a double-click within the click event.

    Does anyone have extant code to do that? This must have been encountered before so I assume it is out there, if so can I have it please?
    Last edited by yereverluvinuncleber; Oct 22nd, 2019 at 06:23 AM.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,705

    Re: VB NET QUESTION: Catching a double-click event using addhandler - How?

    Yeah, except that's bollocks. I just tested this code:
    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
    4.         Console.WriteLine("PictureBox1_MouseDown")
    5.     End Sub
    6.  
    7.     Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
    8.         Console.WriteLine("PictureBox1_Click")
    9.     End Sub
    10.  
    11.     Private Sub PictureBox1_DoubleClick(sender As Object, e As EventArgs) Handles PictureBox1.DoubleClick
    12.         Console.WriteLine("PictureBox1_DoubleClick")
    13.     End Sub
    14.  
    15. End Class
    and then double-clicked the PictureBox and the output was exactly as expected:
    PictureBox1_MouseDown
    PictureBox1_Click
    PictureBox1_MouseDown
    PictureBox1_DoubleClick
    If you're not seeing all events then it is because you have written code that prevents it.

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,705

    Re: VB NET QUESTION: Catching a double-click event using addhandler - How?

    In case you were wondering, this code:
    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    4.         AddHandler PictureBox1.MouseDown, AddressOf PictureBox1_MouseDown
    5.         AddHandler PictureBox1.Click, AddressOf PictureBox1_Click
    6.         AddHandler PictureBox1.DoubleClick, AddressOf PictureBox1_DoubleClick
    7.     End Sub
    8.  
    9.     Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs)
    10.         Console.WriteLine("PictureBox1_MouseDown")
    11.     End Sub
    12.  
    13.     Private Sub PictureBox1_Click(sender As Object, e As EventArgs)
    14.         Console.WriteLine("PictureBox1_Click")
    15.     End Sub
    16.  
    17.     Private Sub PictureBox1_DoubleClick(sender As Object, e As EventArgs)
    18.         Console.WriteLine("PictureBox1_DoubleClick")
    19.     End Sub
    20.  
    21. End Class
    produces the very same output.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    512

    Re: VB NET QUESTION: Catching a double-click event using addhandler - How?

    Except that it isn't bollox. It might be a mistake on my part or a misunderstanding but it isn't bollox. At the moment it simply seems to be an observation.

    The code I am testing will always perform the click event even when there is a handler for the doubleclick event.

    This is the same trial code that I posted before in my earlier post when I was asking for assistance on passing a groupbox name instead of a form.

    http://www.vbforums.com/showthread.p...s-groupbox-how

    This code works and successfully places a series of picboxes on a form, it has an addhandler that can direct each click event to a subroutine, I have simply added a second addhandler for the doubleClick event:

    Code:
    Public Class PictureBoxArray
    
        Inherits System.Collections.CollectionBase
        Private ReadOnly HostForm As System.Windows.Forms.Control
    
    
    
    
        Public Function AddNewPictureBox() As System.Windows.Forms.PictureBox
            Dim aPictureBox As New System.Windows.Forms.PictureBox()
            Me.List.Add(aPictureBox)
            HostForm.Controls.Add(aPictureBox)
            aPictureBox.Top = Count * 50
            aPictureBox.Left = Count * 10
            aPictureBox.Tag = Me.Count
            aPictureBox.BackColor = Color.Blue
    
            AddHandler aPictureBox.Click, AddressOf pictureBoxClickHandler
            AddHandler aPictureBox.DoubleClick, AddressOf pictureBoxDoubleClickHandler
            'AddHandler aPictureBox.MouseDown, AddressOf pictureBoxClickHandler
    
            Return aPictureBox
        End Function
    
        Public Sub New(ByVal host As System.Windows.Forms.Control)
            HostForm = host
            Me.AddNewPictureBox()
        End Sub
    
        Default Public ReadOnly Property Item(ByVal Index As Integer) As  _
           System.Windows.Forms.PictureBox
            Get
                Return CType(Me.List.Item(Index), System.Windows.Forms.PictureBox)
            End Get
        End Property
    
        Public Sub Remove()
            If Me.Count > 0 Then
                HostForm.Controls.Remove(Me(Me.Count - 1))
                Me.List.RemoveAt(Me.Count - 1)
            End If
        End Sub
    
        Public Sub pictureBoxClickHandler(ByVal sender As Object, ByVal e As  _
           System.EventArgs)
            MessageBox.Show("you have clicked pictureBox " & CType(CType(sender,  _
               System.Windows.Forms.PictureBox).Tag, String))
        End Sub
        Public Sub pictureBoxDoubleClickHandler(ByVal sender As Object, ByVal e As  _
           System.EventArgs)
            MessageBox.Show("you have double clicked pictureBox " & CType(CType(sender,  _
               System.Windows.Forms.PictureBox).Tag, String))
        End Sub
    End Class
    If I have the addhandler click event then it is the only event that fires. The double-click is always ignored if the click event is present.

    I have tried it with mousedown or click and the result is always the same.

    Code:
            'AddHandler aPictureBox.Click, AddressOf pictureBoxClickHandler
            AddHandler aPictureBox.DoubleClick, AddressOf pictureBoxDoubleClickHandler
    DoubleClick works.

    Code:
            AddHandler aPictureBox.Click, AddressOf pictureBoxClickHandler
            AddHandler aPictureBox.DoubleClick, AddressOf pictureBoxDoubleClickHandler
    No DoubleClick only a single click is registered.


    For the moment, as a workaround I have added a timer call in the click event to allow the second click to be registered.

    Code:
            clickCount = clickCount + 1
            clickTimer.Interval = 500
            AddHandler clickTimer.Tick, AddressOf clickTimer_Tick
            clickTimer.Enabled = True
    The timer calls the subroutine for the doubleClick, a definite kludge but it works for the moment.
    Last edited by yereverluvinuncleber; Oct 22nd, 2019 at 08:05 AM. Reason: added link to previous post

  5. #5
    Addicted Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    196

    Re: VB NET QUESTION: Catching a double-click event using addhandler - How?

    Have you changed the mouse settings on your test machine?

    Name:  Mouse.jpg
Views: 282
Size:  65.7 KB
    Utterly useless, but always willing to help

    As a finishing touch god created the dutch

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    512

    Re: VB NET QUESTION: Catching a double-click event using addhandler - How?

    Quote Originally Posted by Goggy View Post
    Have you changed the mouse settings on your test machine?
    As your signature quote says, that was bit of help that was utterly useless but thanks for the willingness in any case!

    In reality all is as it should be with regard to Windows mouse configuration, it is only in this set of circumstances within VB.NET that this occurs. That usage of the addhandler in the code segment above causes the doubleClick to be ignored.

  7. #7
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,282

    Re: VB NET QUESTION: Catching a double-click event using addhandler - How?

    Quote Originally Posted by yereverluvinuncleber View Post
    That usage of the addhandler in the code segment above causes the doubleClick to be ignored.

    Nah. It's most likely your use of MessageBox.Show.

    The use of MessageBox/MsgBox screws with the UI message pump. They are not the greatest of debugging tools at the best of times, and generally should be avoided when debugging event handlers.

    Simple test: comment out the MessageBox.Show line in the Click event handler and see if the DoubleClick event handler now spawns its message box.

  8. #8
    Fanatic Member
    Join Date
    Nov 2017
    Posts
    1,003

    Re: VB NET QUESTION: Catching a double-click event using addhandler - How?

    At the risk of getting some snark back, I believe the problem you are encountering is because your "Click" event is displaying a messagebox - which happens immediately after the first half of a double-click, which then causes the second half of the double-click to be ignored. If you change that "tracking" code to do something non-interactive/modal like write to a label on the form, you should see things flowing properly.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    512

    Re: VB NET QUESTION: Catching a double-click event using addhandler - How?

    Quote Originally Posted by OptionBase1 View Post
    At the risk of getting some snark back
    You won't get any snark back from me. That was a joke to Goggy based on his signature.

    I will try that messagebox removal and see what occurs.

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    512

    Re: VB NET QUESTION: Catching a double-click event using addhandler - How?

    Yes I tried removing the messagebox but still no effect. I can add a breakpoint in the doubleClick event and it is never encountered.

    I can add a debug line to it or I can add some other indicator but it never even gets to the routine. No matter what I use to test the doubleClick event it does not fire.

    As soon as I remove the click event addhandler the doubleClick event is triggered.

  11. #11
    Addicted Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    196

    Re: VB NET QUESTION: Catching a double-click event using addhandler - How?

    I tried your code, i commented out the message box.... and <drum roll here> ... it works..
    Utterly useless, but always willing to help

    As a finishing touch god created the dutch

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    512

    Re: VB NET QUESTION: Catching a double-click event using addhandler - How?

    Well Goggy, I am using VB2010 and perhaps it is a combination of that and my code.
    I am testing it regularly and I can only report what I see.

    I'll post the project as it is and then perhaps you can try it and see if it works for you. I appreciate your time.

    EDIT: I have just seen exactly what you have reported and the double click event is being activated after removal of the messagebox.

    I will revisit the 'real' code rather than the test code and see what could be causing the same result as the messagebox.
    Last edited by yereverluvinuncleber; Oct 22nd, 2019 at 09:01 AM.

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    512

    Re: VB NET QUESTION: Catching a double-click event using addhandler - How?

    Goggy et al,

    In my click event it calls a routine to do something. It displays an image amongst other things.

    When I comment the line out to call that routine the doubleclick event is triggered as you have all suggested. When it is present the double click does not fire. It is the same routine that was called on the doubleClick event on the VB6 control array.

    There is something in that code that cancels the ability of VB.NET to determine the second click as being part of the double-click event in order to fire. It could be that the routine takes a few milliseconds, long enough to prevent the second click to be registered as part of the first.

    I will have to dig further to determine what is happening. I have a workaround for the moment in any case (the timer).

    Strange that messageboxes can screw up the debugging but I suppose these things are sent to try us. I will note that tip for the future.

    When I've found the source of the problem I will report it back here and flag it as resolved. Thanks for your time.

    P.S. Goggy, not so useless
    Last edited by yereverluvinuncleber; Oct 22nd, 2019 at 09:36 AM.

  14. #14
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,911

    Re: VB NET QUESTION: Catching a double-click event using addhandler - How?

    Quote Originally Posted by yereverluvinuncleber View Post
    ...
    Strange that messageboxes can screw up the debugging but I suppose these things are sent to try us.
    ...
    I don't know that interrupting an event with a messagebox causing issues would be considered strange. Personally, I have never used messageboxes for debug, even for VB6 or earlier.

    And of course, if you added a msgbox to a click event in VB6, you would never get a DblClick event either. You've broken the continuity of the two click events happening within the short time necessary to consider it a DblClick.

    In the VB6 code below, you will never get the DblClick event.
    Code:
    Option Explicit
    
    Private Sub Picture1_Click()
      MsgBox "Click"
    End Sub
    
    Private Sub Picture1_DblClick()
      MsgBox "doubleClick"
    End Sub
    As for being able to call a sub which updates some graphics in the click event, and do some other processing in the DblClick event in VB6, that could be due to the single threaded nature of VB6 vs the less coupled nature of .Net.
    The event handlers for .Net are delegates, and are processed through the message processor, as were VB6 events, but in .Net there are more events involved with drawing and you may be triggering some of those events (i.e. Paint), for the control if you're updating it, so the additional insertion and processing of other events before the second Click event may be breaking whatever continuity two subsequent click events take to be considered a doubleclick.

    Or it could be that the drawing takes enough time, that the second click event is delayed past the time it would be considered a doubleclick, so you just get a second click event (p.s. as you've already postulated).
    Last edited by passel; Oct 22nd, 2019 at 10:03 AM.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  15. #15
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    2,424

    Re: VB NET QUESTION: Catching a double-click event using addhandler - How?

    well I changed a routine a bit from Button to Picturebox with Doubleclick, the Doubleclick worked

    try yourself

    add a Button and 2 Flowlayoutpanels

    Code:
    Public Class Form9
    
    
        Private btnCollection As New Collection
        Private PicCollection As New Collection
    
        Private Sub InitButtons(ByRef flp As FlowLayoutPanel)
            Dim y As Integer = 0
            Dim xPos As Integer = 0
            Dim yPos As Integer = 0
    
            Dim TList As New ArrayList()
            With TList
                For Each c In "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray()
                    .Add(c)
                Next
            End With
    
            For n As Integer = 0 To 25
                Dim ctrl As New Button()
                ctrl.Width = 24 ' Width of button
                ctrl.Height = 20 ' Height of button
                ctrl.Show()
                flp.Controls.Add(ctrl)
                If (n = 13) Then ' Location of second line of buttons:
                    xPos = 0
                    yPos = 20
                    xPos = xPos + ctrl.Width
                End If
                ctrl.Top = yPos
                ctrl.Name = TList.Item(n).ToString
                ctrl.Text = ctrl.Name.ToString
                ctrl.Tag = n
                btnCollection.Add(ctrl, n)
                AddHandler ctrl.Click, AddressOf ctrl_Click
            Next
        End Sub
    
        Private Sub ctrl_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Dim btn As Button = sender
            MessageBox.Show("You clicked character [" + btn.Text + "]")
        End Sub
    
        Private Sub InitPictureBoxes(ByRef flp As FlowLayoutPanel)
            Dim y As Integer = 0
            Dim xPos As Integer = 0
            Dim yPos As Integer = 0
    
            Dim TList As New ArrayList()
            With TList
                For Each c In "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray()
                    .Add(c)
                Next
            End With
    
            For n As Integer = 0 To 25
    
                Dim ctrl As New PictureBox()
                ctrl.Width = 24 ' Width of button
                ctrl.Height = 20 ' Height of button
                ctrl.BorderStyle = BorderStyle.FixedSingle
                ctrl.Show()
                flp.Controls.Add(ctrl)
                If (n = 13) Then ' Location of second line of buttons:
                    xPos = 0
                    yPos = 20
                    xPos = xPos + ctrl.Width
                End If
                ctrl.Top = yPos
                ctrl.Name = TList.Item(n).ToString
                ctrl.Text = ctrl.Name.ToString
                ctrl.Tag = n
    
                PicCollection.Add(ctrl, n)
                'set for DoubleClick
                AddHandler ctrl.DoubleClick, AddressOf ctrl_PicClick
            Next
        End Sub
    
        Private Sub ctrl_PicClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
    
            Dim btn As PictureBox = sender
            MessageBox.Show("You clicked Picturebox [" + btn.Text + "]")
        End Sub
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Call InitButtons(FlowLayoutPanel1)
            Call InitPictureBoxes(FlowLayoutPanel2)
        End Sub
    
       
    End Class
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  16. #16

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    512

    Re: VB NET QUESTION: Catching a double-click event using addhandler - How?

    Quote Originally Posted by passel View Post
    I don't know that interrupting an event with a messagebox causing issues would be considered strange. Personally, I have never used messageboxes for debug, even for VB6 or earlier.
    I didn't use both, just the useful one in the double click event but I was testing in my 'real' code which would not work regardless as it included the external subroutine call so it may have led me astray.

    Quote Originally Posted by passel View Post
    As for being able to call a sub which updates some graphics in the click event, and do some other processing in the DblClick event in VB6, that could be due to the single threaded nature of VB6 vs the less coupled nature of .Net.
    The event handlers for .Net are delegates, and are processed through the message processor, as were VB6 events, but in .Net there are more events involved with drawing and you may be triggering some of those events (i.e. Paint), for the control if you're updating it, so the additional insertion and processing of other events before the second Click event may be breaking whatever continuity two subsequent click events take to be considered a doubleclick.

    Or it could be that the drawing takes enough time, that the second click event is delayed past the time it would be considered a doubleclick, so you just get a second click event.
    I agree, I think it will be one of those. I have a timer workaround for the moment but I'll do some digging as I'd like to know what is causing it. Your information regarding single-threading and multi-threading is noted. This migration is teaching me a lot.

  17. #17

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    512

    Re: VB NET QUESTION: Catching a double-click event using addhandler - How?

    Thanks Chris.E but I think we got there already. Your assistance appreciated though.

  18. #18
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,911

    Re: VB NET QUESTION: Catching a double-click event using addhandler - How?

    Quote Originally Posted by yereverluvinuncleber View Post
    ... Your information regarding single-threading and multi-threading is noted. This migration is teaching me a lot. ...
    In this case, it isn't really a single-thread vs multi-thread thing, but having additional events generated from within an event handler.

    There are cases where you need to be careful when dealing with an object within an event handler, if calling a method associated with the object may change the state of something that the current event handler is dealing with, or generates the same event with a different state.

    I'm sure that sounds confusing, and is hard to explain, but an example I've come across would be handling the PlayStateChange event of the Windows Media Player.
    If you look for the "Media Ended" state in the handler, and then set the URL property to play another song, it doesn't work.
    I think setting the URL will cause another PlayStateChange event, and since you are in the middle of a PlayStateChange event handler, something gets crossed up internally, and the player never starts playing the new song.

    In that case, I could set a flag to indicate I want to play another song, leave the event handler, and in a timer event, check for that flag and start a new song and clear the flag.

    But, another option, is to not depend on a timer to change the URL outside the current event, but instead "schedule" the URL update to take place after the event handler is left, by invoking a delegate sub to change the URL. The delegate will be called after you've left the current event handler.

    I couldn't replicate your issue easily, so I can't test to see if invoking your sub instead of calling it in the click event would help the situation. It is more a matter of curiosity at this point, I suppose.

    So, instead of calling your sub directly, you can try to invoke it to see if it makes any difference, or makes a bad difference.
    Code:
      ' UpdateImage(myParameter)  'try invoking instead
      Me.Invoke(Sub() UpdateImage(myParmeter) )
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  19. #19
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,902

    Re: VB NET QUESTION: Catching a double-click event using addhandler - How?

    I'm not quite sure what this discussion is about. Didn't JMC show what's happening in his first post? He showed the sequence of events when he double clicked on a panel. I see the click event is clearly in there, before the double click, with a pair of mouse down events sandwiching it. Is that not EXACTLY what you are describing?

    So, the only real question to me, is how could VB6 know that a second click was coming after the first click? I'm pretty sure it couldn't without doing the same kind of pausing routine that you are doing already. If you could count on a double click in all cases, then you could do EVERYTHING in the double click routine, but you'd never be able to handle a click in that case. So, it seems like your only alternative is to wait on the first click to see whether the user was serious, or just getting started.
    My usual boring signature: Nothing

  20. #20
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    2,424

    Re: VB NET QUESTION: Catching a double-click event using addhandler - How?

    Quote Originally Posted by yereverluvinuncleber View Post
    Thanks Chris.E but I think we got there already. Your assistance appreciated though.
    where are you already ?
    I thought you wanted to handle Mousedown and DoubleClick for the Picturebox, or I missed your Code
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  21. #21

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    512

    Re: VB NET QUESTION: Catching a double-click event using addhandler - How?

    Quote Originally Posted by ChrisE View Post
    where are you already ?
    I thought you wanted to handle Mousedown and DoubleClick for the Picturebox, or I missed your Code
    I think we found the culprit. I explained it in my post #16. In the 'real' code when the click event was called it did something. It was calling a routine and the presence of that interrupted the generation and recognition of the second click. I could replace that routine with a call to any other simple test routine and the doubleclick activates, it is just the presence of this particular routine that prevents the doubleclick from activating.

    It was the same subroutine that the VB6 version called on a doubleclick (reads a file, populates an image and fills some text fields) but the effect calling it on VB.NET is somehow different, cancelling the double click event.

    It is now up to me to find out what that difference is. I have a workaround for the moment and I'll report back when I find it. Once again, thanks for your help.

  22. #22

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    512

    Re: VB NET QUESTION: Catching a double-click event using addhandler - How?

    Quote Originally Posted by passel View Post
    So, instead of calling your sub directly, you can try to invoke it to see if it makes any difference, or makes a bad difference.
    Code:
      ' UpdateImage(myParameter)  'try invoking instead
      Me.Invoke(Sub() UpdateImage(myParmeter) )
    Will try that. Thanks.

  23. #23
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    2,424

    Re: VB NET QUESTION: Catching a double-click event using addhandler - How?

    Quote Originally Posted by yereverluvinuncleber View Post
    I think we found the culprit. I explained it in my post #16. In the 'real' code when the click event was called it did something. It was calling a routine and the presence of that interrupted the generation and recognition of the second click. I could replace that routine with a call to any other simple test routine and the doubleclick activates, it is just the presence of this particular routine that prevents the doubleclick from activating.

    It was the same subroutine that the VB6 version called on a doubleclick (reads a file, populates an image and fills some text fields) but the effect calling it on VB.NET is somehow different, cancelling the double click event.

    It is now up to me to find out what that difference is. I have a workaround for the moment and I'll report back when I find it. Once again, thanks for your help.
    well if I wanted to handle Click and DoubleClick in VB6 i changed the Borderstyle of the PictureBox
    or if possible used the Listview instead of the Picturebox

    just tried the change of the Borderstyle in .Net, but this is also a Workaround, can't you switch to the Listview?

    anyway her the .Net code with change of Picturebox Borderstyle for click and doubleclick

    Code:
    Public Class Form1
    
        Private PicCollection As New Collection
    
        Private Sub InitPictureBoxes(ByRef flp As FlowLayoutPanel)
            Dim y As Integer = 0
            Dim xPos As Integer = 0
            Dim yPos As Integer = 0
    
            Dim TList As New ArrayList()
            With TList
                For Each c In "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray()
                    .Add(c)
                Next
            End With
    
            For n As Integer = 0 To 25
    
                Dim ctrl As New PictureBox()
                ctrl.Width = 24 ' Width of button
                ctrl.Height = 20 ' Height of button
                ctrl.BorderStyle = BorderStyle.FixedSingle
                ctrl.Show()
                flp.Controls.Add(ctrl)
                If (n = 13) Then ' Location of second line of buttons:
                    xPos = 0
                    yPos = 20
                    xPos = xPos + ctrl.Width
                End If
                ctrl.Top = yPos
                ctrl.Name = TList.Item(n).ToString
                ctrl.Text = ctrl.Name.ToString
                ctrl.Tag = n
    
                PicCollection.Add(ctrl, n)
                'set for DoubleClick
                AddHandler ctrl.Click, AddressOf ctrl_mousedownPicClick
                AddHandler ctrl.DoubleClick, AddressOf ctrl_doublePicClick
            Next
        End Sub
    
        Private Sub ctrl_doublePicClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Dim btn As PictureBox = sender
            MessageBox.Show("You doubleclicked Picturebox [" + btn.Text + "]")
            btn.BorderStyle = BorderStyle.FixedSingle
        End Sub
    
        Private Sub ctrl_mousedownPicClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Dim btn As PictureBox = sender
            If btn.BorderStyle = BorderStyle.Fixed3D Then
                Exit Sub
            End If
            MessageBox.Show("you clicked Picturebox [" + btn.Text + "]")
            btn.BorderStyle = BorderStyle.Fixed3D
    
        End Sub
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Call InitPictureBoxes(FlowLayoutPanel1)
        End Sub
    
       
    End Class
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  24. #24

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    512

    Re: VB NET QUESTION: Catching a double-click event using addhandler - How?

    Quote Originally Posted by ChrisE View Post

    Code:
    Public Class Form1
    
        Private PicCollection As New Collection
    
        Private Sub InitPictureBoxes(ByRef flp As FlowLayoutPanel)
            Dim y As Integer = 0
            Dim xPos As Integer = 0
            Dim yPos As Integer = 0
    
            Dim TList As New ArrayList()
            With TList
                For Each c In "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray()
                    .Add(c)
                Next
            End With
    
            For n As Integer = 0 To 25
    
                Dim ctrl As New PictureBox()
                ctrl.Width = 24 ' Width of button
                ctrl.Height = 20 ' Height of button
                ctrl.BorderStyle = BorderStyle.FixedSingle
                ctrl.Show()
                flp.Controls.Add(ctrl)
                If (n = 13) Then ' Location of second line of buttons:
                    xPos = 0
                    yPos = 20
                    xPos = xPos + ctrl.Width
                End If
                ctrl.Top = yPos
                ctrl.Name = TList.Item(n).ToString
                ctrl.Text = ctrl.Name.ToString
                ctrl.Tag = n
    
                PicCollection.Add(ctrl, n)
                'set for DoubleClick
                AddHandler ctrl.Click, AddressOf ctrl_mousedownPicClick
                AddHandler ctrl.DoubleClick, AddressOf ctrl_doublePicClick
            Next
        End Sub
    
        Private Sub ctrl_doublePicClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Dim btn As PictureBox = sender
            MessageBox.Show("You doubleclicked Picturebox [" + btn.Text + "]")
            btn.BorderStyle = BorderStyle.FixedSingle
        End Sub
    
        Private Sub ctrl_mousedownPicClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Dim btn As PictureBox = sender
            If btn.BorderStyle = BorderStyle.Fixed3D Then
                Exit Sub
            End If
            MessageBox.Show("you clicked Picturebox [" + btn.Text + "]")
            btn.BorderStyle = BorderStyle.Fixed3D
    
        End Sub
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Call InitPictureBoxes(FlowLayoutPanel1)
        End Sub
    
       
    End Class
    That's a good visual test method.

  25. #25
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,705

    Re: VB NET QUESTION: Catching a double-click event using addhandler - How?

    This issue really comes down to a failure to test properly. When things don't work as expected, you should strip them back to their simplest form and see whether the specific principle you're examining works in isolation. I demonstrated in post #2 how that should have been done in this particular case and that, in fact, the principle does work exactly as expected. That means that, as I said there and then, that is an issue in the code making use of that principle. The correct approach is to then start from the simplest case and build it up, step by step, to the code that isn't working and see exactly where it breaks. That will tell you where the actual issue is. That's how it should have been done in this case and, not that that is known, hopefully that is how it will be done in future.

    Also, if someone says you're wrong and demonstrates as much, it doesn't really help you to insist that you're actually right. At the very least, you should have run the code that I provided for yourself and thus seen for yourself that you were wrong and the issue was, in fact, with your code.

  26. #26

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    512

    Re: VB NET QUESTION: Catching a double-click event using addhandler - How?

    Quote Originally Posted by jmcilhinney View Post
    ...if someone says you're wrong and demonstrates as much, it doesn't really help you to insist that you're actually right.
    The only reason I am responding is for above line. All of the rest you stated is quite correct.

    I said straight away "It might be a mistake on my part or a misunderstanding but it isn't bollox. At the moment it simply seems to be an observation". I wasn't denying anything just reporting what I saw. As soon as I saw the code work on a simpler version, I realised there might have been a bug in my code and stated that immediately.

    Remember I sat and walked it through so many times and the doubleclick never triggered unless the single click event was commented out. So both events were being triggered but they were mutually exclusive.

    I was simply confident that I'd found the bug and asking for advice, nothing more.

    In any case am not sure yet there even is a bug in my code though I will accept that there is... all I know currently is that functional logic that worked with VB6 does not with VB.NET and is short-circuited for as yet unknown reason.

    All I have been doing is observing and reporting what I have seen. I have never in all my years in any language seen a double-click event not actioned when the syntax and logic was correct and there were no syntax errors displayed in the IDE. If anything .NET seems 'sensitive'. Furthermore, the logic was tried and tested, in VB6 the same code (not complex, not unusual code) simply worked. In my mind the logic of a doubleclick event being so immutable and the code being so straightforward that it had to work. This mind says, a single click simply calls a routine, the doubleclick should do the same and the two should not interfere.

    No need to have a 'dig' about this or that, let that wait until I have discovered the culprit and then we can do a post-mortem, otherwise it is all a bit too early.

    If you want to draw any anything in particular from this then it might be your inappropriate use of a word in your first post, one that might be considered aggressive. You may have a lot of experience but trollish behaviour is discouraged if you want to get the best from people. Also, Jim, you aren't right all the time, have you ever made a mistake?

    I do try to be courteous but at the same time be expressive with my language so I understand the use of the word might have been unintentional. I always replace it with rowlocks.

    I appreciate this may generate more discussion but really it is just a small bug in a program or perhaps a very small storm in a very tiny teacup that I will report back on shortly. Shall we just roll this one up for the moment until I come up with the analysis?
    Last edited by yereverluvinuncleber; Oct 23rd, 2019 at 05:25 AM. Reason: grammar

  27. #27
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,902

    Re: VB NET QUESTION: Catching a double-click event using addhandler - How?

    Before this degenerates, let's leave it there. I removed all the posts, because they didn't further anything.

    I don't think that arguing about this, whether justified or not, will advance anything at this point. I admit that I am vaguely curious as to how VB6 could have known that the user was going to follow their first click with a second click, but not curious enough to look into it. When I click a button, I want the button to do something. Whether I will then click it again is entirely up to me.

    This was covered in the first response. I can see how the way it works is annoying, but it is what it is. If you are handling both the click and the doubleclick, you have to deal with the fact that before there can be a doubleclick there has to have been a click. If VB6 didn't respond to that fact, I'd say that's on them, and you got away with one. Your workaround is the obvious one. The other would be much harder, and maybe impossible: Make it such that whatever happens on the click doesn't inherently conflict with what happens on the doubleclick such that they can both coexist.
    My usual boring signature: Nothing

  28. #28

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    512

    Re: VB NET QUESTION: Catching a double-click event using addhandler - How?

    Quote Originally Posted by Shaggy Hiker View Post
    Before this degenerates, let's leave it there. I removed all the posts, because they didn't further anything.

    I don't think that arguing about this, whether justified or not, will advance anything at this point. I admit that I am vaguely curious as to how VB6 could have known that the user was going to follow their first click with a second click, but not curious enough to look into it. When I click a button, I want the button to do something. Whether I will then click it again is entirely up to me.

    This was covered in the first response. I can see how the way it works is annoying, but it is what it is. If you are handling both the click and the doubleclick, you have to deal with the fact that before there can be a doubleclick there has to have been a click. If VB6 didn't respond to that fact, I'd say that's on them, and you got away with one. Your workaround is the obvious one. The other would be much harder, and maybe impossible: Make it such that whatever happens on the click doesn't inherently conflict with what happens on the doubleclick such that they can both coexist.
    Thankyou. I will respond with what I find when my testing is complete.

  29. #29
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,705

    Re: VB NET QUESTION: Catching a double-click event using addhandler - How?

    I would suggest that having a control that responds to a click and a double-click is simply a bad idea for this very reason. Generally speaking, any control will only respond to one or the other. If you feel like you need both, maybe think about using a right-click instead of a double-click.

  30. #30
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,911

    Re: VB NET QUESTION: Catching a double-click event using addhandler - How?

    Quote Originally Posted by Shaggy Hiker View Post
    ... I admit that I am vaguely curious as to how VB6 could have known that the user was going to follow their first click with a second click, but not curious enough to look into it. ... If you are handling both the click and the doubleclick, you have to deal with the fact that before there can be a doubleclick there has to have been a click. If VB6 didn't respond to that fact, I'd say that's on them, and you got away with one. ... Make it such that whatever happens on the click doesn't inherently conflict with what happens on the doubleclick such that they can both coexist.
    Somehow, it seems that the situation of what VB6 did versus what .Net is doing keeps getting confused.

    VB6 didn't magically ignore the click event when there was a double click event, yelub never said that.
    What he did in VB6 was, execute a subroutine in the click event, and then another subroutine in the double-click event.
    In VB6 the click event would come first, followed by the double click event, the same as it should happen in .Net.

    But, in yelub's case, in .Net, the subroutine being called in the click event prevents the double-click event from occurring. Of course, he didn't know initially that calling the subroutine in the click event was what was preventing the doubleclick from happening, and we didn't have a full picture of the code involved, so couldn't recreate the issue.

    Yelub made the mistake of trying to create a short example to illustrate the issue, using a message box in place of the subroutine call, but that was a fallacy. That situation wouldn't work in VB6 either, as it prevents the doubleclick from happening in both languages, and shouldn't be unexpected because of the modal nature of the messagebox.

    So, the problem is now well understood, but the cause is still a question. The problem is that something that happens when that sub is called interrupts the process of a doubleclick from happening, so the doubleclick event doesn't happen. In VB6, when calling the VB6 version of that subroutine, the double click event still occurred.

    That was the root of the confusion. We don't have the code that causes the problem, so we couldn't determine the issue for ourselves.
    When yelub removed the event handler for the click event, then he got the doubleclick event, so made an assumption that the two events were possibly mutually exclusive, so raised the question to confirm if that was the case, or was he misinterpreting the situation.

    Of course, if he removed the click handler, then he wasn't calling the subroutine in the click handler that broke the doubleclick detection, so the fact that he now got the doubleclick event and when he added the click handler back, and he no longer got the doubleclick event, that adding the click handler caused the problem, i.e. a red herring situation. It wasn't the adding of the click handler that was the problem, it was the content of what was in the click handler that was the issue.

    Of course, we're in the dark about what the click event handler is doing, so we can't actually help figure out what might be the issue.
    As jmc says, it will be up to yelub to break up the processing of what is going on in the click handler into smaller pieces, i.e. just adding code bit by bit if possible to the handler until it breaks.

    It would be nice if a short and valid example of the problem could be produced, otherwise we don't really have anything further to offer.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  31. #31
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,911

    Re: VB NET QUESTION: Catching a double-click event using addhandler - How?

    Quote Originally Posted by jmcilhinney View Post
    I would suggest that having a control that responds to a click and a double-click is simply a bad idea for this very reason. Generally speaking, any control will only respond to one or the other. If you feel like you need both, maybe think about using a right-click instead of a double-click.
    Well, I don't know. I know of a number of text editors that move the cursor on a single click, select a word on a double click or select a line on a triple click.
    Also, some drawing programs do different operations on a click vs a double-click.

    Of course, I guess, most programs that do use click and double click, the click event sort of leads into the double click in a non destructive way, i.e. moving the cursor, then the double-click acts on the cursor position.

    In this case, I'm assuming something similar. If you click on the image, the image is changed to show that you've selected it, and a double-click may say I'm going to modify the selected object. So in the click event, the image is modified to show the selection, and in the doubleclick event a dialogbox or something else happens to the selected object. Makes perfect sense, and worked fine in VB6. In .Net however, calling the sub to modify the image to show selection is preventing the doubleclick from happening so you can't doubleclick on the image to put it in "modification" mode.

    Finding out why, or changing the paradigm of how the program currently works will be necessary when porting to .Net, which is an unexpected outcome, is all.
    Last edited by passel; Oct 24th, 2019 at 06:22 PM.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  32. #32
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,705

    Re: VB NET QUESTION: Catching a double-click event using addhandler - How?

    Quote Originally Posted by passel View Post
    Well, I don't know. I know of a number of text editors that move the cursor on a single click, select a word on a double click or select a line on a triple click.
    Also, some drawing programs do different operations on a click vs a double-click.

    Of course, I guess, most programs that do use click and double click, the click event sort of leads into the double click in a non destructive way, i.e. moving the cursor, then the double-click acts on the cursor position.
    Yeah, I meant more when the actions were incompatible. If you have to effectively cancel a click when a double-click occurs then you should probably think about using a right-click instead.

  33. #33

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    512

    Re: VB NET QUESTION: Catching a double-click event using addhandler - How?

    Quote Originally Posted by jmcilhinney View Post
    I would suggest that having a control that responds to a click and a double-click is simply a bad idea for this very reason. Generally speaking, any control will only respond to one or the other. If you feel like you need both, maybe think about using a right-click instead of a double-click.
    You are probably right. The original VB6 design, when the pictureBox was clicked upon once, it merely selected the icon for a preview. When it was double-clicked it was placed into the map. I think I will take your advice here and although I have a workaround for the double click it is not elegant. I think I will implement a drag and drop onto the map instead, so that will do away with the need for a double click. That is good advice and it is appreciated.

  34. #34
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,705

    Re: VB NET QUESTION: Catching a double-click event using addhandler - How?

    Drag-n-drop is sexier anyway. It's something new for you to learn to implement in .NET too. If you need some help, you can follow the CodeBank link in my signature below and check out my thread on the subject. The examples will be using different controls but the principles are the same no matter the controls involved or the data being dragged.

  35. #35

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    512

    Re: VB NET QUESTION: Catching a double-click event using addhandler - How?

    Quote Originally Posted by jmcilhinney View Post
    Drag-n-drop is sexier anyway. It's something new for you to learn to implement in .NET too. If you need some help, you can follow the CodeBank link in my signature below and check out my thread on the subject. The examples will be using different controls but the principles are the same no matter the controls involved or the data being dragged.
    Wilco!

  36. #36
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,902

    Re: VB NET QUESTION: Catching a double-click event using addhandler - How?

    Quote Originally Posted by passel View Post
    But, in yelub's case,
    yelub, huh! Is that where we're going? OK, nobody can spell yereverluvinuncleber anyways, and he sounds like a character from Pogo, which would be deep south in the US rather than North...anything in the UK.
    My usual boring signature: Nothing

  37. #37
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    2,424

    Re: VB NET QUESTION: Catching a double-click event using addhandler - How?

    Quote Originally Posted by jmcilhinney View Post
    I would suggest that having a control that responds to a click and a double-click is simply a bad idea for this very reason. Generally speaking, any control will only respond to one or the other. If you feel like you need both, maybe think about using a right-click instead of a double-click.
    that's why I suggested using the Listview (for click , doubleclick), if he want's to load Pictures , Images

    I mean we all use the Explorer with a click and a doubleclick
    Last edited by ChrisE; Oct 24th, 2019 at 11:51 PM.
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  38. #38
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,705

    Re: VB NET QUESTION: Catching a double-click event using addhandler - How?

    Quote Originally Posted by ChrisE View Post
    that's why I suggested using the Listview (for click , doubleclick), if he want's to load Pictures , Images

    I mean we all use the Explorer with a click and a doubleclick
    I don't recall seeing that but it might not be a bad idea. That said, the WinForms ListView has some limitations with regards to displaying images.

  39. #39
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    2,424

    Re: VB NET QUESTION: Catching a double-click event using addhandler - How?

    Quote Originally Posted by jmcilhinney View Post
    I don't recall seeing that but it might not be a bad idea. That said, the WinForms ListView has some limitations with regards to displaying images.
    in Post#23

    but using the Listview is up to yelub
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  40. #40
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,911

    Re: VB NET QUESTION: Catching a double-click event using addhandler - How?

    Quote Originally Posted by Shaggy Hiker View Post
    yelub, huh! Is that where we're going? OK, nobody can spell yereverluvinuncleber anyways, and he sounds like a character from Pogo, which would be deep south in the US rather than North...anything in the UK.
    Well, I can spell it since it isn't hard to remember the phrase, yer ever luvin uncle ber.
    But, I was debating between the initialism yelub, or the acronym yeluber as possible syncopes since we have a limited number of characters allowed in a post.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width