General Category > Tips & Tricks
Add keystroke (shortcut) to basic button
authorleon:
Hello,
I am trying to make a simple application and I have some buttons that work with the AB SLC500. Great.
I would like to add on the button a key shortcut so the user can press the button or press a key.
--- Code: --- Private Sub BasicButton1_Click(sender As Object, e As EventArgs) Handles BasicButton1.Click
End Sub
--- End code ---
I have been trying for about 6 hours but no joy. The code above is what I have as the standard code.
Thank you very much.
Godra:
To assign a shortcut one could possibly try using the MainForm's "KeyUp" event:
--- Code: --- Private Sub MainForm_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
If e.Alt AndAlso e.KeyCode = Keys.B Then
BasicButton1.PerformClick()
End If
End Sub
--- End code ---
This is a general approach and should not work properly with the BasicButton control since this control relies on the MouseDown and MouseUp events to perform its function. It should work with whatever code you might include in the sub you posted.
authorleon:
Hello,
Thank you for the info, but it did not work:
--- Code: ---Public Class MainForm
Private Sub MainForm_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
If e.Alt AndAlso e.KeyCode = Keys.W Then
BasicButton1.PerformClick()
End If
End Sub
'*******************************************************************************
'* Stop polling when the form is not visible in order to reduce communications
'* Copy this section of code to every new form created
'*******************************************************************************
Private Sub Form_VisibleChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.VisibleChanged
AdvancedHMIDrivers.Utilities.StopComsOnHidden(components, Me)
End Sub
'**************************************
'* Filling the form with a gradient
'**************************************
'Private Sub MainForm_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
'Dim rect As New System.Drawing.Rectangle(0, 0, e.ClipRectangle.Width, e.ClipRectangle.Height)
'Dim gradientBrush As New Drawing.Drawing2D.LinearGradientBrush(New Point(0, 0), New Point(0, Height), System.Drawing.Color.FromArgb(180, 100, 200), System.Drawing.Color.FromArgb(110, 200, 255))
'e.Graphics.FillRectangle(gradientBrush, rect)
'End Sub
Private Sub BasicButton1_Click(sender As Object, e As EventArgs) Handles BasicButton1.Click
End Sub
Private Sub BasicButton7_Click(sender As Object, e As EventArgs) Handles BasicButton7.Click
End Sub
Private Sub BasicButton2_Click(sender As Object, e As EventArgs) Handles BasicButton2.Click
End Sub
--- End code ---
I have posted all the code I have at present.
Thank you very much.
If there is a better way. I would like to know. I am energizing: O:9/3.
When I press the button it energizes: O:9/3. When I let go, it stop.
Thank you
Godra:
I did suggest that this would not work with the BasicButton because its functionality depends on the mouse events.
You could possibly simulate the functionality by writing the values with the driver, similar to this:
--- Code: --- Private Sub MainForm_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.Alt AndAlso e.KeyCode = Keys.W Then
ModbusTCPCom1.Write("00001", "1")
End If
End Sub
Private Sub MainForm_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
If e.Alt AndAlso e.KeyCode = Keys.W Then
ModbusTCPCom1.Write("00001", "0")
End If
End Sub
--- End code ---
Instead of ModbusTCPCom1 you would use your driver and instead of "00001" you would use your address O:9/3.
I am not sure if it is a good practice to energize outputs of the PLC outside of the ladder program (but if it works for you then it works for me as well).
authorleon:
Hello,
I did what you said but i get this error.
Please have a look at the image attached. Also, I am using a serial connection.
Thank you
Navigation
[0] Message Index
[#] Next page
Go to full version