vb.net f1 button

menachem680
menachem680 used Ask the Experts™
on
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?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
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

Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
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

Author

Commented:
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
????
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
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()

Author

Commented:
now i wanna make a f2 for page3 what i have to change?
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
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

Author

Commented:
that what you gave me i have to copy?
i got this error on this line
ProcessCmdKey
and
MyBase.ProcessCmdKey
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
Can you show me all of your code?

Author

Commented:
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
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
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?

Author

Commented:
function 'ProcessCmdKey' cannot be declared 'Overrides' because it does not override a function in a base class.      
'ProcessCmdKey' is not a member of 'Objcet

Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
The ProcessCmdKey() code has to be INSIDE the FORM's code.  Where have you placed it?

Author

Commented:
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
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
Ok...so what you've done is "nested" the Form1 class I provided inside your existing Form1 class.

It would need to look more 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
                Page_2.Show()

            Case Keys.F2
                PAGE_3.Show()

            Case Keys.F3
                PAGE_4.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
        Page_2.Show()
    End Sub

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

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

End Class

Open in new window

Author

Commented:
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???
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial