Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Map Functions Keys with buttons on Window Form in C# and VB using .NET 2.0

Author: Zunnair
Download Source Code : 728_MapButtons.zip

In this simple article you will learn that how you can Map Functions Keys with buttons on Window Form.

Simply just create New Project of windows application in C#.

 

Drag 4 buttons on the form.

 

Now write following code on form Load

 

C#

 

private void Form1_Load(object sender, EventArgs e)

        {

            this.Text = "DevAsp APP";

            button1.Text = button1.Text + " ( F6 )";

            button2.Text = button2.Text + " ( F7 )";

            button3.Text = button3.Text + " ( F8 )";

            button4.Text = button4.Text + " ( F9 )";

        }

 

 

VB

 

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)

        Me.Text = "DevAsp APP"

        button1.Text = button1.Text + " ( F6 )"

        button2.Text = button2.Text + " ( F7 )"

        button3.Text = button3.Text + " ( F8 )"

        button4.Text = button4.Text + " ( F9 )"

    End Sub

 

Now write following code on Button Click events

 

 

C#

 

private void button1_Click(object sender, EventArgs e)

        {

            MessageBox.Show("Button1 Pressed.");

        }

        private void button2_Click(object sender, EventArgs e)

        {

            MessageBox.Show("Button2 Pressed.");

        }

        private void button3_Click(object sender, EventArgs e)

        {

            MessageBox.Show("Button3 Pressed.");

        }

        private void button4_Click(object sender, EventArgs e)

        {

            MessageBox.Show("Button4 Pressed.");

        }

 

VB

 

Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)

        MessageBox.Show("Button1 Pressed.")

    End Sub

    Private Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs)

        MessageBox.Show("Button2 Pressed.")

    End Sub

    Private Sub button3_Click(ByVal sender As Object, ByVal e As EventArgs)

        MessageBox.Show("Button3 Pressed.")

    End Sub

    Private Sub button4_Click(ByVal sender As Object, ByVal e As EventArgs)

        MessageBox.Show("Button4 Pressed.")

    End Sub

 

Now write following code Key down Events

 

C#

 

private void button4_KeyDown(object sender, KeyEventArgs e)

        {

            try

            {

                switch (e.KeyCode)

                {

                    case Keys.F6:

                        button1_Click(this, e);

                        break;

                    case Keys.F7:

                        button2_Click(this, e);

                        break;

                    case Keys.F8:

                        button3_Click(this, e);

                        break;

                    case Keys.F9:

                        button4_Click(this, e);

                        break;

                }

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

        }

        private void button1_KeyDown(object sender, KeyEventArgs e)

        {

            try

            {

                switch (e.KeyCode)

                {

                    case Keys.F6:

                        button1_Click(this, e);

                        break;

                    case Keys.F7:

                        button2_Click(this, e);

                        break;

                    case Keys.F8:

                        button3_Click(this, e);

                        break;

                    case Keys.F9:

                        button4_Click(this, e);

                        break;

                }

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

       

        }

 

        private void button2_KeyDown(object sender, KeyEventArgs e)

        {

            try

            {

                switch (e.KeyCode)

                {

                    case Keys.F6:

                        button1_Click(this, e);

                        break;

                    case Keys.F7:

                        button2_Click(this, e);

                        break;

                    case Keys.F8:

                        button3_Click(this, e);

                        break;

                    case Keys.F9:

                        button4_Click(this, e);

                        break;

                }

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

        }

 

        private void button3_KeyDown(object sender, KeyEventArgs e)

        {

            try

            {

                switch (e.KeyCode)

                {

                    case Keys.F6:

                        button1_Click(this, e);

                        break;

                    case Keys.F7:

                        button2_Click(this, e);

                        break;

                    case Keys.F8:

                        button3_Click(this, e);

                        break;

                    case Keys.F9:

                        button4_Click(this, e);

                        break;

                }

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

        }

 

        private void Form1_KeyDown(object sender, KeyEventArgs e)

        {

            try

            {

                switch (e.KeyCode)

                {

                    case Keys.F6:

                        button1_Click(this, e);

                        break;

                    case Keys.F7:

                        button2_Click(this, e);

                        break;

                    case Keys.F8:

                        button3_Click(this, e);

                        break;

                    case Keys.F9:

                        button4_Click(this, e);

                        break;

                }

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

        }

 

VB

 

Private Sub button4_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)

        Try

            Select Case e.KeyCode

                Case Keys.F6

                    button1_Click(Me, e)

                    Exit Select

                Case Keys.F7

                    button2_Click(Me, e)

                    Exit Select

                Case Keys.F8

                    button3_Click(Me, e)

                    Exit Select

                Case Keys.F9

                    button4_Click(Me, e)

                    Exit Select

            End Select

        Catch ex As Exception

            MessageBox.Show(ex.Message)

        End Try

    End Sub

    Private Sub button1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)

        Try

            Select Case e.KeyCode

                Case Keys.F6

                    button1_Click(Me, e)

                    Exit Select

                Case Keys.F7

                    button2_Click(Me, e)

                    Exit Select

                Case Keys.F8

                    button3_Click(Me, e)

                    Exit Select

                Case Keys.F9

                    button4_Click(Me, e)

                    Exit Select

            End Select

        Catch ex As Exception

            MessageBox.Show(ex.Message)

        End Try

 

    End Sub

 

    Private Sub button2_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)

        Try

            Select Case e.KeyCode

                Case Keys.F6

                    button1_Click(Me, e)

                    Exit Select

                Case Keys.F7

                    button2_Click(Me, e)

                    Exit Select

                Case Keys.F8

                    button3_Click(Me, e)

                    Exit Select

                Case Keys.F9

                    button4_Click(Me, e)

                    Exit Select

            End Select

        Catch ex As Exception

            MessageBox.Show(ex.Message)

        End Try

    End Sub

 

    Private Sub button3_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)

        Try

            Select Case e.KeyCode

                Case Keys.F6

                    button1_Click(Me, e)

                    Exit Select

                Case Keys.F7

                    button2_Click(Me, e)

                    Exit Select

                Case Keys.F8

                    button3_Click(Me, e)

                    Exit Select

                Case Keys.F9

                    button4_Click(Me, e)

                    Exit Select

            End Select

        Catch ex As Exception

            MessageBox.Show(ex.Message)

        End Try

    End Sub

 

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)

        Try

            Select Case e.KeyCode

                Case Keys.F6

                    button1_Click(Me, e)

                    Exit Select

                Case Keys.F7

                    button2_Click(Me, e)

                    Exit Select

                Case Keys.F8

                    button3_Click(Me, e)

                    Exit Select

                Case Keys.F9

                    button4_Click(Me, e)

                    Exit Select

            End Select

        Catch ex As Exception

            MessageBox.Show(ex.Message)

        End Try

    End Sub

 

Now your buttons events are mapped on functions keys.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Open and read COM port data using System.IO.Ports in C# and VB .NET 2.0

Disclaimer - Privacy
© 2002-2017 DevASP.net