locked
How to make Double Click Event of a Button Work? RRS feed

  • Question

  • Dear Friends,

    I tried this code but it didn't work.

     Private Sub Button2_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.DoubleClick
            
    
            TextBox1.Text = "Double Click Event"
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    
            TextBox1.Text = "Single Click Event"
    
    
        End Sub

    Can you teach me please. I went through the suggested answers but the No good explanation is given for Double-click Not working.

    Thanks

    Regards

    VKSBK


    A Real Novice Programmer !

    Tuesday, April 8, 2014 9:51 AM

Answers

  • Hi,

    Double Click event is not raised by default as mentioned by the Button.DoubleClick Event documentation here.

    To capture double click events, you must enable the control with the SetStyle method. 

    Please find a good example of enabling the SetStyle method and capturing double click events here.

    • Proposed as answer by ChervineMVP Tuesday, April 8, 2014 11:16 AM
    • Marked as answer by V.K.S.B.K Tuesday, April 8, 2014 2:52 PM
    Tuesday, April 8, 2014 11:16 AM
  • I don't know why you would want to do this. Perhaps a mouse down event would be better.

    Option Strict On
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.CenterToScreen()
        End Sub
    
        Private Sub Button1_MouseDown(sender As Object, e As MouseEventArgs) Handles Button1.MouseDown
            If e.Button = Windows.Forms.MouseButtons.Left Then
                TextBox1.Text = "Left Mouse Button."
            ElseIf e.Button = Windows.Forms.MouseButtons.Right Then
                TextBox1.Text = "Right Mouse Button."
            ElseIf e.Button = Windows.Forms.MouseButtons.Middle Then
                TextBox1.Text = "Middle Mouse Button."
            End If
        End Sub
    
    End Class


    Hello. I'm old and retired. I like to program if you could call what I do programming. However I'd like to code for you! If you've got the dime then I've got the time. Call me, landline, @ BR-549.

    • Marked as answer by V.K.S.B.K Tuesday, April 8, 2014 2:53 PM
    Tuesday, April 8, 2014 12:42 PM

All replies

  • Hi,

    Double Click event is not raised by default as mentioned by the Button.DoubleClick Event documentation here.

    To capture double click events, you must enable the control with the SetStyle method. 

    Please find a good example of enabling the SetStyle method and capturing double click events here.

    • Proposed as answer by ChervineMVP Tuesday, April 8, 2014 11:16 AM
    • Marked as answer by V.K.S.B.K Tuesday, April 8, 2014 2:52 PM
    Tuesday, April 8, 2014 11:16 AM
  • Buttons should not have to be double clicked.  It goes against the guidelines for good UI design.

    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." JohnWein

    Tuesday, April 8, 2014 11:28 AM
  • I don't know why you would want to do this. Perhaps a mouse down event would be better.

    Option Strict On
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.CenterToScreen()
        End Sub
    
        Private Sub Button1_MouseDown(sender As Object, e As MouseEventArgs) Handles Button1.MouseDown
            If e.Button = Windows.Forms.MouseButtons.Left Then
                TextBox1.Text = "Left Mouse Button."
            ElseIf e.Button = Windows.Forms.MouseButtons.Right Then
                TextBox1.Text = "Right Mouse Button."
            ElseIf e.Button = Windows.Forms.MouseButtons.Middle Then
                TextBox1.Text = "Middle Mouse Button."
            End If
        End Sub
    
    End Class


    Hello. I'm old and retired. I like to program if you could call what I do programming. However I'd like to code for you! If you've got the dime then I've got the time. Call me, landline, @ BR-549.

    • Marked as answer by V.K.S.B.K Tuesday, April 8, 2014 2:53 PM
    Tuesday, April 8, 2014 12:42 PM
  • Dear Friend,

    I saw in few programs when single click make an action and double click open a dialog / another form. So I wanted to do that in my project couldn't do that so far.

    Thanks

    Kind Regards

    VKSBK


    A Real Novice Programmer !

    Tuesday, April 8, 2014 4:31 PM
  • Dear Friend,

    I saw in few programs when single click make an action and double click open a dialog / another form. So I wanted to do that in my project couldn't do that so far.

    Thanks

    Kind Regards

    VKSBK


    A Real Novice Programmer !

    Guidelines Link

    I have never seen what you are describing, but I would have never considered double clicking a button.


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." JohnWein

    Tuesday, April 8, 2014 7:59 PM
  • Guidelines Link

    I have never seen what you are describing, but I would have never considered double clicking a button.


    This thread seems to have deteriorated into a lesson on style.

    I have the same problem - I am using the Button class to represent the squares in a Sudoku grid rather than to perform a program action, and have come across the same problem. No doubt someone here will tell me that this is all wrong and I should use another class altogether (but which one, please?).

    But there also seems to be a problem with the click handling - even with StandardDoubleClick style set to True the DoubleClick event isn't caught. The following program shows the problem - when I single-click I get the expected result, when I double-click I get 2 single clicks but no double-click. StandardDoubleClick seems to be True by default. Setting it makes no difference. The form as a button and a text box with obvious names.

    Public Class MainForm
    
      Dim s As String
    
    
      Private Sub Display(e As System.Windows.Forms.MouseEventArgs)
        s = s & e.Clicks
    
        Select Case e.Button
    
          Case Windows.Forms.MouseButtons.Left
            s = s & " left button"
    
          Case Windows.Forms.MouseButtons.Middle
            s = s & " middle button"
    
          Case Windows.Forms.MouseButtons.Right
            s = s & " right button"
    
          Case Windows.Forms.MouseButtons.XButton1
            s = s & " XButton1"
    
          Case Windows.Forms.MouseButtons.XButton2
            s = s & " Xbutton2"
    
        End Select
    
        If My.Computer.Keyboard.AltKeyDown Then
          s = s & " Alt"
        End If
    
        If My.Computer.Keyboard.CtrlKeyDown Then
          s = s & " Ctrl"
        End If
    
        If My.Computer.Keyboard.ShiftKeyDown Then
          s = s & " Shift"
        End If
    
        TB1.AppendText(s + vbCrLf)
      End Sub
    
      Private Sub BtnMouse_Click(sender As Object, e As EventArgs) Handles BtnMouse.Click
        Dim bd, bs As Boolean
        bs = GetStyle(ControlStyles.StandardClick)
        bd = GetStyle(ControlStyles.StandardDoubleClick)
        s = "Button single click. Single, double click styles = " & bs.ToString & ", " & bd.ToString
        Display(e)
      End Sub
    
    
      Private Sub BtnMouse_DoubleClick(sender As Object, e As EventArgs) Handles BtnMouse.DoubleClick
        s = "Button double click"
        Display(e)
      End Sub
    End Class


    Peter

    Thursday, July 10, 2014 9:35 AM
  • What is supposed to happen in your Sudoku game when the button is double clicked?  Maybe you are using the wrong control.

    'Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it.'  JohnWein

    Multics

    My Serial Port Answer

    Thursday, July 10, 2014 9:41 AM
  • I think you just made my first point - maybe instead of telling people that they're wrong you could suggest the correct alternative.

    I'm using a Button as that has most of the functionality needed for playing Sudoku - it can be selected (and change appearance automatically when selected), have text, have a foreground and background colour etc.

    When I single-click I want to select it (and do some other program logic which would bore you). When I double-click I want to update the value (again the details will bore you). I could easily use a right-click for that but, as as purist, you will object to my using an action more normally suited to bringing up a context menu).

    If you can suggest another control more suited to having two different click actions and with all the required functionality, please let me know (I asked in my previous post!).

    I notice that you don't comment on the non-functionality of double-clicking. Maybe you would like to. That, at least, would help solve my and VBSBK's problems.


    Peter

    Thursday, July 10, 2014 10:00 AM
  • I don't know why you hi jack this old thread. 

    If you have a question, than make a new one. 

    It is free.


    Success
    Cor


    Thursday, July 10, 2014 10:46 AM
  • I think you just made my first point - maybe instead of telling people that they're wrong you could suggest the correct alternative.

    I'm using a Button as that has most of the functionality needed for playing Sudoku - it can be selected (and change appearance automatically when selected), have text, have a foreground and background colour etc.

    When I single-click I want to select it (and do some other program logic which would bore you). When I double-click I want to update the value (again the details will bore you). I could easily use a right-click for that but, as as purist, you will object to my using an action more normally suited to bringing up a context menu).

    If you can suggest another control more suited to having two different click actions and with all the required functionality, please let me know (I asked in my previous post!).

    I notice that you don't comment on the non-functionality of double-clicking. Maybe you would like to. That, at least, would help solve my and VBSBK's problems.


    Peter

    How could I suggest an alternative when it is not at all clear how you want your Sudoku game to work.  Normally you draw a board with some squares.  Some of them are filled in, some aren't.  The goal of the game is to fill in the blanks.  You are telling me that isn't how your game works.

    You are correct, I haven't shown how to make a 'Button' double-clickable.  Why would I encourage non-standard UI design?  I have written plenty of code where I chose the wrong control and went back and fixed it, not much fun but it was the right thing to do.

    I suggest you see the table in the remarks here

    http://msdn.microsoft.com/en-us/library/system.windows.forms.control.doubleclick%28v=vs.110%29.aspx

    It clearly shows the events raised for a Button double click, that is Click, Click.


    'Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it.'  JohnWein

    Multics

    My Serial Port Answer


    • Edited by dbasnett Thursday, July 10, 2014 11:10 AM
    Thursday, July 10, 2014 11:06 AM
  • How could I suggest an alternative when it is not at all clear how you want your Sudoku game to work.  Normally you draw a board with some squares.  Some of them are filled in, some aren't.  The goal of the game is to fill in the blanks.  You are telling me that isn't how your game works.

    You are correct, I haven't shown how to make a 'Button' double-clickable.  Why would I encourage non-standard UI design?  I have written plenty of code where I chose the wrong control and went back and fixed it, not much fun but it was the right thing to do.

    I suggest you see the table in the remarks here

    http://msdn.microsoft.com/en-us/library/system.windows.forms.control.doubleclick%28v=vs.110%29.aspx

    It clearly shows the events raised for a Button double click, that is Click, Click.



    Thanks for the link - I'd not found that one.

    I do know how to play Sudoku as it happens. But there are other things I want to do with the grid: display what possible values there are, change the background colour so I can see which squares are initialised, and which I've played (and change the colour for that on the fly). All of which is easy to do with a Button.

    Yes, I could completely rewrite the program in the way you suggest. But as Button has most of the functionality needed (but I've found the one place it doesn't) it is obviously (maybe not to a diehard traditionalist...) the easiest way to program it. As it happens, I've written a program to play Killer Soduku in Java, and found that building a grid was quite hard work. So, as a way of trying out VB I decided to try buttons. Maybe I should just move to Java and give up on VB and .NET :)


    Peter

    Thursday, July 10, 2014 11:19 AM
  • I don't know why you hi jack this old thread. 

    If you have a question, than make a new one. 

    It is free.


    Success
    Cor



    Because I have exactly the same question. And April isn't exactly "old".

    Peter

    Thursday, July 10, 2014 11:21 AM
  • How could I suggest an alternative when it is not at all clear how you want your Sudoku game to work.  Normally you draw a board with some squares.  Some of them are filled in, some aren't.  The goal of the game is to fill in the blanks.  You are telling me that isn't how your game works.

    You are correct, I haven't shown how to make a 'Button' double-clickable.  Why would I encourage non-standard UI design?  I have written plenty of code where I chose the wrong control and went back and fixed it, not much fun but it was the right thing to do.

    I suggest you see the table in the remarks here

    http://msdn.microsoft.com/en-us/library/system.windows.forms.control.doubleclick%28v=vs.110%29.aspx

    It clearly shows the events raised for a Button double click, that is Click, Click.



    Thanks for the link - I'd not found that one.

    I do know how to play Sudoku as it happens. But there are other things I want to do with the grid: display what possible values there are, change the background colour so I can see which squares are initialised, and which I've played (and change the colour for that on the fly). All of which is easy to do with a Button.

    Yes, I could completely rewrite the program in the way you suggest. But as Button has most of the functionality needed (but I've found the one place it doesn't) it is obviously (maybe not to a diehard traditionalist...) the easiest way to program it. As it happens, I've written a program to play Killer Soduku in Java, and found that building a grid was quite hard work. So, as a way of trying out VB I decided to try buttons. Maybe I should just move to Java and give up on VB and .NET :)


    Peter

    So use a textbox for the grid.  On the side of the board show other controls that give the functionality you want for the currently selected textbox.  UI design is UI design, buttons, regardless of platform, are not double-clickable.  Consistent design practices means that users understand what is going on.

    'Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it.'  JohnWein

    Multics

    My Serial Port Answer

    Thursday, July 10, 2014 11:42 AM