Button press configuration in VB.NET

Here is something custom I am trying to do. Please let me know if it is even possible to do this.

When I press a button, it should operate normally.

But, when I press and hold the button for 2 seconds, pop a context menu with an option named "Reset"

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You could use the mouse down event and then run a thread that sleeps for 2 seconds before calling a function. Then in the mouse up have a function that cancels the thread.

I'm not near VS.NET but here is how i think it should work.

Imports System.Threading

Private timerThread As Thread
Private cancelThread As Boolean = False

Private Sub DelayedPopup()
   If cancelThread = False Then
   End If
End Sub

Private Sub Button1_MouseDown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.MouseDown
   timerThread = New Thread(New ThreadStart(AddressOf DelayedPopup))
   cancelThread = False
End Sub

Private Sub Button1_MouseUp(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.MouseUp
   timerThead = Nothing
   cancelThread = True
End Sub

That should do it. Not sure if it works because right now I'm on linux. But that concept will work

Hope That Helps

Sample .....

Public Class Form1
    Inherits System.Windows.Forms.Form
    Private Shared myTimer As New System.Windows.Forms.Timer
    Private Shared exitFlag As Boolean = False

#Region " Windows Form Designer generated code "

    Public Sub New()

        'This call is required by the Windows Form Designer.

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
            End If
        End If
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Button1 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.Button1.Location = New System.Drawing.Point(152, 180)
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Button1"
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Name = "Form1"
        Me.Text = "Form1"

    End Sub

#End Region

    Private Sub Button1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown
        ' Adds the event and the event handler for the method that will
        ' process the timer event to the timer.
        AddHandler myTimer.Tick, AddressOf TimerEventProcessor

        ' Sets the timer interval to 2 seconds.
        myTimer.Interval = 2000

        ' Runs the timer, and raises the event.
        While exitFlag = False
            ' Processes all the events in the queue.
        End While

    End Sub

    Private Sub TimerEventProcessor(ByVal myObject As Object, ByVal myEventArgs As EventArgs)

        'Declare the menu items and the context menu.
            Dim menuItems() As MenuItem = New MenuItem() _
                {New MenuItem("Some Button Info"), _
                New MenuItem("Some Other Button Info"), _
                New MenuItem("Exit")}

            Dim buttonMenu As New ContextMenu(menuItems)
            buttonMenu.Show(Button1, New System.Drawing.Point(20, 20))
        exitFlag = True
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MessageBox.Show("Click Button")
    End Sub
End Class

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cjinsocal581Author Commented:
Second solution worked the way I needed. Thanks for the effort to both.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.