none
KeyPress RRS feed

  • Question

  • im making a program and i was wondering on how to make it if like i hit F1 button1 turns on and if i hit F1 again the button turns off
    Tuesday, November 14, 2017 5:37 PM

All replies

  • Using Function buttons is different than using the Button classes text ability with the ButtonBase.UseMnemonic property which "true if the first character that is preceded by an ampersand (&) is used as the mnemonic key of the control; otherwise, false. The default is true." and allows you to assign the text preceded with an ampersand so that the 1st character of the text will make the button click.

    I do not know why you would want to hold a button on, voiding the click event, and then turn it off later as that is not the standard for the control. It would be simpler to use an If statement or something to discern something like If Button1.Backcolor = Color.LimeGreen then Button1.BackColor = Color.Red (and whatever else to do) else Button1.BackColor = Color.LimeGreen (and do whatever else to do) and toggle the buttons backcolor between LimeGreen (On) and Red (Off) (or vice versa) such that something will run constantly when the buttons backcolor is LimeGreen or not when it is Red maybe.


    La vida loca

    Tuesday, November 14, 2017 5:48 PM
  • im making a program and i was wondering on how to make it if like i hit F1 button1 turns on and if i hit F1 again the button turns off

    A button doesn't have on or off.  Instead, use a Checkbox and set the Appearance to Button.
    https://msdn.microsoft.com/en-us/library/system.windows.forms.checkbox.appearance(v=vs.110).aspx

    Add a handler for a form keypress, check for F1 key, and adjust the checkbox Checked state as required.

        Private Sub Form3_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
            If e.KeyCode = Keys.F1 Then
                CheckBox1.Checked = Not CheckBox1.Checked
            End If
        End Sub
    Depending on what other controls are on the form, you will probably want to set KeyPreview to True.
    https://msdn.microsoft.com/en-us/library/system.windows.forms.form.keypreview(v=vs.110).aspx
    • Proposed as answer by Frank L. Smith Tuesday, November 14, 2017 9:48 PM
    Tuesday, November 14, 2017 8:18 PM
  • Hi Finesserx,

    I would like to make it clear.
    What do you mean by [F1]?  I guess it is [F1] key, i.e. you want to do like this:
    When you press [F1] key, [button1] turns to Enable or Disable (toggle behavior).
        

    If this is correct, code would be like this:
    [Important] Don't forget [KeyPreview = True], in MyBase.Load or in Designer
    ' --- KeyDown
    Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
    	' --- Important: (Me.KeyPreview = True) is necessary in MyBase.Load
    	If (e.KeyCode = Keys.F1) Then
    		If (Me.Button1.Enabled = True) Then
    			Me.Button1.Enabled = False
    			Me.Button1.BackColor = Color.Gray
    		Else
    			Me.Button1.Enabled = True
    			Me.Button1.BackColor = Color.Red
    			Me.Button1.ForeColor = Color.White
    		End If
    	End If
    End Sub

    Regards,

    Ashidacchi


    Wednesday, November 15, 2017 2:32 AM