C# Textboxlar arası Keypress Eventi İle focuslanma

Textboxları sıra ile koysakta tab ile aralarında geçiş biraz zor oluyor.Hele arada bırde dateTimePicker gibi kontrol var kayıt eklerken kullanıcı çok zorluk çekiyor.

Bunun için goControl Fonksiyonu ile KeyPress eventinde tab ve ya enter tuşuna basıldıktan sonra focus olması gereken kontrole fucuslanabiliriz.


 private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
 goControl(Keys.Enter, textBox2);
        }

        private void textBox2_KeyPress_1(object sender, KeyPressEventArgs e)
        {
            goControl(Keys.Enter, textBox3);
        }



	 public void goControl(Keys keyData, Control i)
        {


            if (keyData == Keys.Enter)
            {
                Control c = i;
                while (c != null && !c.TabStop)
                {
                    c = this.GetNextControl(c, true);

                    if (c != null)
                    {
                        c.Focus();
                    }

                }



            }
if (keyData == Keys.Tab)
            {
                Control c = i;
                while (c != null && !c.TabStop)
                {
                    c = this.GetNextControl(c, true);

                    if (c != null)
                    {
                        c.Focus();
                    }

                }



            }

        }

     

Leave a Reply