Link to home
Start Free TrialLog in
Avatar of menachem680
menachem680

asked on

vb.net f1 button

i have a button btnpage1 i wanna the user shulde be able to click f1 and page1 shulde open
 Private Sub frmtemplate_keyDown(ByVal sender as object,ByVal e as System.Windows.Forms.KeyEventArgs)Handles Me.KeyDown
        Try
can you give me the rest?
Avatar of scott_josephs
scott_josephs

Use keyup not keydown otherwise it'll fire to many times. See attached code
Private Sub Form1_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
        If e.KeyCode = Keys.F1 Then
            ' show form
        End If
    End Sub

Open in new window

Avatar of Mike Tomlinson
You can also do:
Public Class Form1

    Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
        Select Case keyData
            Case Keys.F1
                Debug.Print("F1")

        End Select

        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function

End Class

Open in new window

Avatar of menachem680

ASKER

i have a button to go to (name page2) form2
so+ the button i want the user shulde be able to press f1 to go to form2
????
Ok...to display Form2 you can do:

    Form2.Show()

Or if you want a new instance every time:

    Dim f2 As New Form2
    f2.Show()
now i wanna make a f2 for page3 what i have to change?
Something like...
Public Class Form1

    Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
        Select Case keyData
            Case Keys.F1
                Dim f2 As New Form2
                f2.Show()

            Case Keys.F2
                Dim f3 As New Form3
                f3.Show()

        End Select

        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function

End Class

Open in new window

that what you gave me i have to copy?
i got this error on this line
ProcessCmdKey
and
MyBase.ProcessCmdKey
Can you show me all of your code?
Public Class Form1

        Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
            Select Case keyData
                Case Keys.F1
                    Dim f2 As New Page_2
                    Page_2.Show()

                Case Keys.F2
                    Dim f3 As New PAGE_3
                    PAGE_3.Show()

            End Select

            Return MyBase.ProcessCmdKey(msg, keyData)
        End Function
You're missing the End Class at the bottom (though that might just be a copy/paste error).

You're actually only displaying the default instance so these two lines can be removed:

    Dim f2 As New Page_2

    Dim f2 As New PAGE_3

Not sure why you're getting the error...

Is this WinForms or WebForms?...and what version VB.Net?
function 'ProcessCmdKey' cannot be declared 'Overrides' because it does not override a function in a base class.      
'ProcessCmdKey' is not a member of 'Objcet

The ProcessCmdKey() code has to be INSIDE the FORM's code.  Where have you placed it?
this is my page code
Public Class Form1

    'Public Class Form1

    Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
        Select Case keyData
            Case Keys.F1
                Dim f2 As New Page_2
                Page_2.Show()

        End Select

        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function



    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btnpage1.Click
        Dim frm As New Form
        Page_2.Show()
    End Sub
    Private Sub page2_keyup(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp

        If e.KeyCode = Keys.F1 Then

            'show form


        End If
        'Select Case e.KeyCode
        'Case Keys.F1
        ' Btnpage1_click()
        ' Case Keys.F9
        ' Button2_Click()

        ' End Select
        ' Catch ex As Exception

        ' End Try

    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim frm As New Form
        PAGE_3.Show()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim from As New Form
        PAGE_4.Show()
    End Sub

    Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub DateTimePicker2_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub DateTimePicker1_ValueChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'DateTimePicker1.Value()
    End Sub
    ' Private Sub page3_keyup(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp


    'If e.KeyCode = Keys.F2 Then

    'show form


    'End If

    'End Sub
    Public Class Form1

        Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
            Select Case keyData
                Case Keys.F1
                    'Dim f2 As New Page_2
                    Page_2.Show()

                Case Keys.F2
                    'Dim f3 As New PAGE_3
                    PAGE_3.Show()

            End Select

            Return MyBase.ProcessCmdKey(msg, keyData)
        End Function

    End Class


End Class
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks its now good
but now i can click  f1 f2 f3 f4 only from the first page i wanna click from every pages
so i have only to copy past on every page???
Well...you can either:
(1) Copy and paste that code into EVERY form.
(2) Implement IMessageFilter() to trap the Function keys at the APPLICATION level.

Here is what the IMessageFilter approach might look like:
Public Class Page_1

    Private Sub Page_1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Application.AddMessageFilter(New MyFilter)
    End Sub

End Class

Public Class MyFilter
    Implements IMessageFilter

    Private Const WM_KEYDOWN As Integer = &H100

    Public Function PreFilterMessage(ByRef m As System.Windows.Forms.Message) As Boolean Implements System.Windows.Forms.IMessageFilter.PreFilterMessage
        Select Case m.Msg
            Case WM_KEYDOWN
                Select Case m.WParam.ToInt32
                    Case Keys.F1 To Keys.F2
                        Select Case m.WParam.ToInt32
                            Case Keys.F1
                                Page_2.Show()

                            Case Keys.F2
                                Page_3.Show()

                            Case Keys.F3
                                Page_4.Show()

                        End Select
                        Return True

                End Select
        End Select

        Return False
    End Function

End Class

Open in new window