Results 1 to 6 of 6

Thread: [RESOLVED] Is there a button pressed/click function?

  1. #1

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    384

    Resolved [RESOLVED] Is there a button pressed/click function?

    Hi,
    Consider you code in KeyUp and KeyDown events of a specific keyboard button ("Tab" for instance) and wanted to relate it to a button which pressing that key presses it and releasing it releases it.

    There is a
    Code:
    Button1.PerformClick()
    But I'm not certain how to call/recall/mention those events to let user visually see Button1 be pressed/released corresponded to tab.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Is there a button pressed/click function?

    There isn't really a way to do that in the UI with the Button itself, unless you wanted to get into Windows API calls. What you could do is use the ControlPaint class to draw a picture of a depressed Button over the top of the actual Button on KeyDown and then remove it on KeyUp.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Is there a button pressed/click function?

    Actually, I'm not sure that you can draw text on the Button using ControlPaint. You might have to use the ButtonRenderer class instead.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    384

    Re: Is there a button pressed/click function?

    Thank you jmcilhinney I glad to have you back number 1 VB code-consultant ever.

    Oh, it seems a very tricky meanwhile useless thing then. My final alternative was a simple Label which KeyDown make it border style to 3D, representing a pressed state and in normal condition, it is on fixedsingle or even none. But as an obvious example I can mention Windows On Screen Keyboard utility tool which pressing tab on keyboard make it visually pressed, also pressing it on its form by mouse also performs the same, like you actually pressed physical mechanical tab-key.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Is there a button pressed/click function?

    Try this...

    Code:
    Public Class VisualButton
        Inherits Panel
    
        Public Event Clicked(ByVal sender As Object, ByVal e As EventArgs)
        Private WithEvents tmr As New Timer With {.Interval = 250}
        Private pressed As Boolean = False
        Private sf As New StringFormat With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center}
    
        Public Overrides Property Text() As String
            Get
                Return MyBase.Text
            End Get
            Set(ByVal value As String)
                MyBase.Text = value
                Me.Refresh
            End Set
        End Property
    
        Public Sub New()
            Me.Size = New Size(75, 23)
            Me.Text = "VisualButton"
        End Sub
    
        Public Sub PerformClick()
            pressed = True
            tmr.Start()
            Me.Refresh()
            RaiseEvent Clicked(Me, New EventArgs)
        End Sub
    
        Private Sub VisualButton_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
            pressed = True
            tmr.Start()
            Me.Refresh()
            RaiseEvent Clicked(Me, New EventArgs)
        End Sub
    
        Private Sub VisualButton_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
            ControlPaint.DrawButton(e.Graphics, New Rectangle(0, 0, Me.Width, Me.Height), If(pressed, ButtonState.Pushed, ButtonState.Normal))
            e.Graphics.DrawString(Me.Text, Me.Font, Brushes.Black, New Rectangle(0, 0, Me.Width, Me.Height), sf)
        End Sub
    
        Private Sub tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmr.Tick
            tmr.Stop()
            pressed = False
            Me.Refresh()
        End Sub
    End Class
    Last edited by .paul.; Apr 14th, 2023 at 03:07 AM.

  6. #6

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    384

    Re: Is there a button pressed/click function?

    It is a beautifully done button replacement. Thanks paul.

    - For newbies: Copy-paste it in a class and build once. It will be added to toolbox.
    - Its worst flaw is the fact releasing the mouse performs pressing. So I still stick to plan B (Panel or label with sunken 3D border (instead of raised)).
    Last edited by pourkascheff; Apr 22nd, 2023 at 03:40 PM.

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width