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
 

Textbox Finalization with Enter Key in C# and VB using .NET 2.0.

Author: Zunnair
Download Source Code : 732_EnterKeyfinalization.zip

In this simple article you will learn that how to Textbox Finalization with Enter Key

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

 

Drag 1 buttons, six labels and six textbox on the form.

 

Now write following code on form Load

 

 

C#

 

private void Form1_Load(object sender, EventArgs e)

        {

            this.Text = "DevAsp Enter Key finalization";

            textBox1.Focus();

 

        }

 

VB

 

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

        Me.Text = "DevAsp Enter Key finalization"

        textBox1.Focus()

 

    End Sub

 

Now write following code on key down events

C#

private void textBox1_KeyUp(object sender, KeyEventArgs e)

        {

            try

            {

                if (e.KeyCode == Keys.Enter)

                {

                    textBox2.Focus();

                }

                if (e.KeyCode == Keys.Escape)

                {

                    this.Close();

                }

 

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

        }

 

        private void textBox2_KeyUp(object sender, KeyEventArgs e)

        {

            try

            {

                if (e.KeyCode == Keys.Enter)

                {

                    textBox3.Focus();

                }

                if (e.KeyCode == Keys.Escape)

                {

                    this.Close();

                }

 

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

        }

 

        private void textBox3_KeyUp(object sender, KeyEventArgs e)

        {

            try

            {

                if (e.KeyCode == Keys.Enter)

                {

                    textBox4.Focus();

                }

                if (e.KeyCode == Keys.Escape)

                {

                    this.Close();

                }

 

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

        }

 

        private void textBox4_KeyUp(object sender, KeyEventArgs e)

        {

            try

            {

                if (e.KeyCode == Keys.Enter)

                {

                    textBox5.Focus();

                }

                if (e.KeyCode == Keys.Escape)

                {

                    this.Close();

                }

 

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

        }

 

        private void textBox5_KeyUp(object sender, KeyEventArgs e)

        {

            try

            {

                if (e.KeyCode == Keys.Enter)

                {

                    textBox6.Focus();

                }

                if (e.KeyCode == Keys.Escape)

                {

                    this.Close();

                }

 

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

        }

 

        private void textBox6_KeyPress(object sender, KeyPressEventArgs e)

        {

           

        }

 

        private void textBox6_KeyUp(object sender, KeyEventArgs e)

        {

            try

            {

                if (e.KeyCode == Keys.Enter)

                {

                    button1.Focus();

                }

                if (e.KeyCode == Keys.Escape)

                {

                    this.Close();

                }

 

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

        }

 

        private void button1_KeyUp(object sender, KeyEventArgs e)

        {

            try

            {

                if (e.KeyCode == Keys.Enter)

                {

                    textBox1.Focus();

                }

                if (e.KeyCode == Keys.Escape)

                {

                    this.Close();

                }

 

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

        }

VB

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

        Try

            If e.KeyCode = Keys.Enter Then

                textBox2.Focus()

            End If

            If e.KeyCode = Keys.Escape Then

                Me.Close()

 

            End If

        Catch ex As Exception

            MessageBox.Show(ex.Message)

        End Try

    End Sub

 

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

        Try

            If e.KeyCode = Keys.Enter Then

                textBox3.Focus()

            End If

            If e.KeyCode = Keys.Escape Then

                Me.Close()

 

            End If

        Catch ex As Exception

            MessageBox.Show(ex.Message)

        End Try

    End Sub

 

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

        Try

            If e.KeyCode = Keys.Enter Then

                textBox4.Focus()

            End If

            If e.KeyCode = Keys.Escape Then

                Me.Close()

 

            End If

        Catch ex As Exception

            MessageBox.Show(ex.Message)

        End Try

    End Sub

 

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

        Try

            If e.KeyCode = Keys.Enter Then

                textBox5.Focus()

            End If

            If e.KeyCode = Keys.Escape Then

                Me.Close()

 

            End If

        Catch ex As Exception

            MessageBox.Show(ex.Message)

        End Try

    End Sub

 

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

        Try

            If e.KeyCode = Keys.Enter Then

                textBox6.Focus()

            End If

            If e.KeyCode = Keys.Escape Then

                Me.Close()

 

            End If

        Catch ex As Exception

            MessageBox.Show(ex.Message)

        End Try

    End Sub

 

    Private Sub textBox6_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)

 

    End Sub

 

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

        Try

            If e.KeyCode = Keys.Enter Then

                button1.Focus()

            End If

            If e.KeyCode = Keys.Escape Then

                Me.Close()

 

            End If

        Catch ex As Exception

            MessageBox.Show(ex.Message)

        End Try

    End Sub

 

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

        Try

            If e.KeyCode = Keys.Enter Then

                textBox1.Focus()

            End If

            If e.KeyCode = Keys.Escape Then

                Me.Close()

 

            End If

        Catch ex As Exception

            MessageBox.Show(ex.Message)

        End Try

    End Sub

Now write code on Button click event

 

C#

 

private void button1_Click(object sender, EventArgs e)

        {

            MessageBox.Show("Button Pressed.");

        }

 

VB

 

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

        MessageBox.Show("Button Pressed.")

    End Sub

 

Article Comments
It would be nice if when you guys share codes you let the user know what system class /namespace is needed to use certain functions or class member.
Nice example though

Posted on 12/7/2008 10:52:41 PM by Everel

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Working with Window Form Closing Event in C# and VB using .NET 2.0

Disclaimer - Privacy
© 2002-2017 DevASP.net