|
-
Apr 13th, 2023, 01:43 AM
#1
Thread Starter
Hyperactive Member
[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.
-
Apr 13th, 2023, 01:52 AM
#2
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.
-
Apr 13th, 2023, 01:54 AM
#3
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.
-
Apr 13th, 2023, 02:08 AM
#4
Thread Starter
Hyperactive Member
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.
-
Apr 14th, 2023, 02:36 AM
#5
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.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 22nd, 2023, 03:34 PM
#6
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|