Set focus to another Control

Stack Overflow Asked by Binu on August 4, 2020

i want to set focus from one textbox1 to another textbox2 while i am pressing ENTER key in textbox1 in C# windows application(c# 2005)

3 Answers

add this to your form

protected override void OnKeyDown(KeyEventArgs e)
        if (e.KeyCode == Keys.Enter)
            Control NextControl = this.GetNextControl(this.ActiveControl, true);
                while (!NextControl.TabStop || !NextControl.Enabled || !NextControl.Visible)
                    NextControl=this.GetNextControl(NextControl, true);

Answered by paywand dlshad on August 4, 2020

First, you will have to set the KeyPreview property of the Form set to true. Then you will have to override the form's OnKeyDown method and make a case like:

if(e.KeyCode == Keys.Enter)
      Control ctlNext = this.GetNextControl(this.ActiveControl, true);

Mind you that this code will work for every control on the form, and move the focus to the next one. If you just want this code to work for the textboxes you could add a check like:

if(this.ActiveControl is TextBox)

Answered by Nikos Steiakakis on August 4, 2020

Handle the KeyPress or KeyDown event of textbox1 and then call textbox2.Focus().

Answered by Taylor Leese on August 4, 2020

Add your own answers!

Related Questions

Extract rows that have common values in R dataframe

3  Asked on January 31, 2021 by cirrus


Sum rows with same Id based on type and exlude where SUM = 0

1  Asked on January 31, 2021 by daniele-arrighi



2  Asked on January 31, 2021 by sujeet-chaurasia


x86-16 imul instruction “wrong parameters”?

1  Asked on January 30, 2021 by vbvbvb123


Find minimum number of digits required to make a given number

4  Asked on January 30, 2021 by saheel-das


How create table variable with join in SQL

2  Asked on January 30, 2021 by andrii-tkachenko


Convert Image to Base64String in SELECT query

2  Asked on January 30, 2021 by arthur-rey


Getting this error while rendering the page

4  Asked on January 29, 2021 by vijayt


Power set code showing segmentation fault

2  Asked on January 29, 2021


Give a delay on Entry textchanged event in python and tkinter

2  Asked on January 29, 2021 by rickifernando


Ask a Question

Get help from others!

© 2022 All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir