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)
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);
}
NextControl.Focus();
}
else
{
base.OnKeyDown(e);
}
}
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);
ctlNext.Focus();
}
else
{
base.OnKeyDown(e);
}
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
3 Asked on January 31, 2021 by cirrus
1 Asked on January 31, 2021 by srinath
1 Asked on January 31, 2021 by himanshu-ranjan
2 Asked on January 31, 2021 by detly
1 Asked on January 31, 2021 by daniele-arrighi
2 Asked on January 31, 2021 by sujeet-chaurasia
1 Asked on January 30, 2021 by vbvbvb123
6 Asked on January 30, 2021 by jkil
2 Asked on January 30, 2021 by danicee
1 Asked on January 30, 2021 by pavlo
4 Asked on January 30, 2021 by saheel-das
2 Asked on January 30, 2021 by andrii-tkachenko
2 Asked on January 30, 2021 by arthur-rey
0 Asked on January 30, 2021 by bharti-jha
1 Asked on January 29, 2021 by vitesze
4 Asked on January 29, 2021 by vijayt
3 Asked on January 29, 2021 by chris-dixon
2 Asked on January 29, 2021 by rickifernando
Get help from others!
Recent Questions
Recent Answers
© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir