?
Solved

Button press configuration in VB.NET

Posted on 2005-03-21
3
Medium Priority
?
406 Views
Last Modified: 2008-02-01
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"

Ideas?
0
Comment
Question by:cjinsocal581
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 2

Expert Comment

by:GlassCubeMedia
ID: 13593771
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()
   Thread.Sleep(2000)
   If cancelThread = False Then
      ContextMenu.Show()
   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))
   timerThread.Start
   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

Matt
0
 
LVL 27

Accepted Solution

by:
planocz earned 2000 total points
ID: 13594069
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()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        '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
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    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.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(152, 180)
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Button1"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Controls.Add(Me.Button1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    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
        myTimer.Start()

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

    End Sub

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

        '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
0
 
LVL 6

Author Comment

by:cjinsocal581
ID: 13594442
Second solution worked the way I needed. Thanks for the effort to both.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question