locked
When i press ENTER, next control should be focus like TAB. How can i do this ? RRS feed

  • Question

  • Hello
    i will call below code at Form_Load. My scenirio is that. When i press ENTER, next control should be focus like TAB.

    void AttachKeyDownToControls(ControlCollection ccoll)
    {
    foreach ( Control c in ccoll.Controls )
    {
    if ( c.Controls != null )
    {
    AttachKeyDownToControls( c.Controls )
    }
    else
    {
    c.KeyDown += what should i write here ?
    } } }
    • Moved by OmegaMan Friday, March 5, 2010 2:48 AM (From:Visual C# General)
    Saturday, February 20, 2010 9:34 PM

Answers

  • Hello

    I have did like below.
    ENTER key works like TAB key. it is good. But there is a problem. When ENTER key focus on a button and i press ENTER key, another form be opened that anotherform.show() in button_click. How can i ignore it ? it should be work when i click button with mouse. Enter key should not be work like mouse click at here. i have button1, button2, button3 etc. as name. button1 should be work when i click with mouse or i press ENTER key. Athor buttons should not be work with ENTER key.

     protected override void OnKeyDown(KeyEventArgs e)
            {
                switch (e.KeyCode)
                {
                    case Keys.Enter:
                        SendKeys.Send("{TAB}");
                       // SelectNext(this);
                        break;
                    default:
                        base.OnKeyDown(e);
                        break;
                }
            }
    • Marked as answer by JuniorCsharp Tuesday, February 23, 2010 9:46 AM
    Monday, February 22, 2010 5:16 PM
  • Hello dear,

    Set your buttons TabStop Property to false.
    Please help us improve this community forum for visitors by marking the replies as answers if they help and unmarking them if they provide no help.
    Thanks.
    Monday, February 22, 2010 6:21 PM
  • Don't worry, put following code in the form constructor after InitializeComponent()
    ((Control)linkLabel1).TabStop = false;

    Please help us improve this community forum for visitors by marking the replies as answers if they help and unmarking them if they provide no help.
    Thanks.
    • Marked as answer by JuniorCsharp Tuesday, February 23, 2010 9:46 AM
    Tuesday, February 23, 2010 7:28 AM

All replies

  • I would look into overriding the ProcessCmdKey method of the Form class.  You can then filter down on the key strokes within the implementation and use SendKeys to send a {TAB} command.
     
    http://msdn.microsoft.com/en-us/library/system.windows.forms.form.processcmdkey.aspx
    http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.aspx 


    Mike Huguet
    http://www.geekswithblogs.com/mikehuguet
    http://twitter.com/mhuguet
    Saturday, February 20, 2010 10:09 PM

  • c.KeyDown += new KeyEventHandler(put your keydown event here)

    Example:

    c.KeyDown += new KeyEventHandler(textBox1_KeyDown)
    private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {
      
    }


    You can read this on event handling in C#

    kaymaf

    If that what you want, take it. If not, ignored it and no complain

    CODE CONVERTER SITE

    http://www.carlosag.net/Tools/CodeTranslator/.

    http://www.developerfusion.com/tools/convert/csharp-to-vb/.

    Saturday, February 20, 2010 10:12 PM
  • Instead of using SendKeys, you can call:

    SelectNextControl(ActiveControl, true, true, true, true);

    • Proposed as answer by Yasser Zamani - Mr. Help Sunday, February 21, 2010 6:13 AM
    • Unproposed as answer by JuniorCsharp Sunday, February 21, 2010 1:28 PM
    • Proposed as answer by Wa'el Saturday, November 20, 2010 9:30 AM
    Sunday, February 21, 2010 5:53 AM
  • As Louis.fr told, you can do like
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                AttachKeyDownToControls(this);
            }
            void AttachKeyDownToControls(Control ccoll)
            {
                foreach (Control c in ccoll.Controls)
                {
                    if (0 < c.Controls.Count)
                    {
                        AttachKeyDownToControls(c);
                    }
                    else
                    {
                        c.KeyDown += c_KeyDown;
                    }
                }
            }
    
            void c_KeyDown(object sender, KeyEventArgs e)
            {
                Control c = sender as Control;
                switch (e.KeyCode)
                {
                    case Keys.Enter:
                        c.Parent.SelectNextControl(c, true, true, true, true);
                        e.Handled = true;
                        break;
                }
            }
        }
    }
    

    Please help us improve this community forum for visitors by marking the replies as answers if they help and unmarking them if they provide no help.
    Thanks.
    Sunday, February 21, 2010 6:13 AM
  • Hello
    Thank you for your Codes. But i have some problems and questions.
    First tabindexes of controls is line for ENTER ?
    Second, When control is button, button_click be work instead of ENTER.
    Third my some textbox and control in panel1, some of them in Panel2. Panel2 have  tabcontrol1 and it have Tabpage1 and tabpage2 ... until tabpage 7.
     Tabpage1 have groupbox1 and groupbox2 and groupbox3. All of them have textbox and buttons.
    How can i fix this code for this ?
    Thanks
    Sunday, February 21, 2010 8:31 AM
  • Cool questions, here is a complete solution for this issue
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public class Form1 : ForwardWithEnterForm
        {
            public Form1()
            {
                InitializeComponent();
            }
            #region Windows Form Designer generated code
    
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.button1 = new System.Windows.Forms.Button();
                this.checkBox1 = new System.Windows.Forms.CheckBox();
                this.comboBox1 = new System.Windows.Forms.ComboBox();
                this.textBox1 = new System.Windows.Forms.TextBox();
                this.panel1 = new System.Windows.Forms.Panel();
                this.textBox2 = new System.Windows.Forms.TextBox();
                this.comboBox2 = new System.Windows.Forms.ComboBox();
                this.checkBox2 = new System.Windows.Forms.CheckBox();
                this.button2 = new System.Windows.Forms.Button();
                this.groupBox1 = new System.Windows.Forms.GroupBox();
                this.textBox3 = new System.Windows.Forms.TextBox();
                this.comboBox3 = new System.Windows.Forms.ComboBox();
                this.checkBox3 = new System.Windows.Forms.CheckBox();
                this.button3 = new System.Windows.Forms.Button();
                this.panel1.SuspendLayout();
                this.groupBox1.SuspendLayout();
                this.SuspendLayout();
                // 
                // button1
                // 
                this.button1.Location = new System.Drawing.Point(12, 31);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(75, 23);
                this.button1.TabIndex = 0;
                this.button1.Text = "button1";
                this.button1.UseVisualStyleBackColor = true;
                // 
                // checkBox1
                // 
                this.checkBox1.AutoSize = true;
                this.checkBox1.Location = new System.Drawing.Point(8, 8);
                this.checkBox1.Name = "checkBox1";
                this.checkBox1.Size = new System.Drawing.Size(80, 17);
                this.checkBox1.TabIndex = 1;
                this.checkBox1.Text = "checkBox1";
                this.checkBox1.UseVisualStyleBackColor = true;
                // 
                // comboBox1
                // 
                this.comboBox1.FormattingEnabled = true;
                this.comboBox1.Location = new System.Drawing.Point(0, 60);
                this.comboBox1.Name = "comboBox1";
                this.comboBox1.Size = new System.Drawing.Size(121, 21);
                this.comboBox1.TabIndex = 2;
                // 
                // textBox1
                // 
                this.textBox1.Location = new System.Drawing.Point(16, 87);
                this.textBox1.Name = "textBox1";
                this.textBox1.Size = new System.Drawing.Size(100, 20);
                this.textBox1.TabIndex = 3;
                // 
                // panel1
                // 
                this.panel1.Controls.Add(this.textBox2);
                this.panel1.Controls.Add(this.comboBox2);
                this.panel1.Controls.Add(this.checkBox2);
                this.panel1.Controls.Add(this.button2);
                this.panel1.Location = new System.Drawing.Point(304, 6);
                this.panel1.Name = "panel1";
                this.panel1.Size = new System.Drawing.Size(233, 120);
                this.panel1.TabIndex = 4;
                // 
                // textBox2
                // 
                this.textBox2.Location = new System.Drawing.Point(56, 80);
                this.textBox2.Name = "textBox2";
                this.textBox2.Size = new System.Drawing.Size(100, 20);
                this.textBox2.TabIndex = 7;
                // 
                // comboBox2
                // 
                this.comboBox2.FormattingEnabled = true;
                this.comboBox2.Location = new System.Drawing.Point(40, 53);
                this.comboBox2.Name = "comboBox2";
                this.comboBox2.Size = new System.Drawing.Size(121, 21);
                this.comboBox2.TabIndex = 6;
                // 
                // checkBox2
                // 
                this.checkBox2.AutoSize = true;
                this.checkBox2.Location = new System.Drawing.Point(48, 1);
                this.checkBox2.Name = "checkBox2";
                this.checkBox2.Size = new System.Drawing.Size(80, 17);
                this.checkBox2.TabIndex = 5;
                this.checkBox2.Text = "checkBox2";
                this.checkBox2.UseVisualStyleBackColor = true;
                // 
                // button2
                // 
                this.button2.Location = new System.Drawing.Point(52, 24);
                this.button2.Name = "button2";
                this.button2.Size = new System.Drawing.Size(75, 23);
                this.button2.TabIndex = 4;
                this.button2.Text = "button2";
                this.button2.UseVisualStyleBackColor = true;
                // 
                // groupBox1
                // 
                this.groupBox1.Controls.Add(this.textBox3);
                this.groupBox1.Controls.Add(this.comboBox3);
                this.groupBox1.Controls.Add(this.checkBox3);
                this.groupBox1.Controls.Add(this.button3);
                this.groupBox1.Location = new System.Drawing.Point(234, 197);
                this.groupBox1.Name = "groupBox1";
                this.groupBox1.Size = new System.Drawing.Size(251, 171);
                this.groupBox1.TabIndex = 5;
                this.groupBox1.TabStop = false;
                this.groupBox1.Text = "groupBox1";
                // 
                // textBox3
                // 
                this.textBox3.Location = new System.Drawing.Point(81, 115);
                this.textBox3.Name = "textBox3";
                this.textBox3.Size = new System.Drawing.Size(100, 20);
                this.textBox3.TabIndex = 7;
                // 
                // comboBox3
                // 
                this.comboBox3.FormattingEnabled = true;
                this.comboBox3.Location = new System.Drawing.Point(65, 88);
                this.comboBox3.Name = "comboBox3";
                this.comboBox3.Size = new System.Drawing.Size(121, 21);
                this.comboBox3.TabIndex = 6;
                // 
                // checkBox3
                // 
                this.checkBox3.AutoSize = true;
                this.checkBox3.Location = new System.Drawing.Point(73, 36);
                this.checkBox3.Name = "checkBox3";
                this.checkBox3.Size = new System.Drawing.Size(80, 17);
                this.checkBox3.TabIndex = 5;
                this.checkBox3.Text = "checkBox3";
                this.checkBox3.UseVisualStyleBackColor = true;
                // 
                // button3
                // 
                this.button3.Location = new System.Drawing.Point(77, 59);
                this.button3.Name = "button3";
                this.button3.Size = new System.Drawing.Size(75, 23);
                this.button3.TabIndex = 4;
                this.button3.Text = "button3";
                this.button3.UseVisualStyleBackColor = true;
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(545, 476);
                this.Controls.Add(this.groupBox1);
                this.Controls.Add(this.panel1);
                this.Controls.Add(this.textBox1);
                this.Controls.Add(this.comboBox1);
                this.Controls.Add(this.checkBox1);
                this.Controls.Add(this.button1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.panel1.ResumeLayout(false);
                this.panel1.PerformLayout();
                this.groupBox1.ResumeLayout(false);
                this.groupBox1.PerformLayout();
                this.ResumeLayout(false);
                this.PerformLayout();
    
            }
    
            #endregion
    
            private System.Windows.Forms.Button button1;
            private System.Windows.Forms.CheckBox checkBox1;
            private System.Windows.Forms.ComboBox comboBox1;
            private System.Windows.Forms.TextBox textBox1;
            private System.Windows.Forms.Panel panel1;
            private System.Windows.Forms.TextBox textBox2;
            private System.Windows.Forms.ComboBox comboBox2;
            private System.Windows.Forms.CheckBox checkBox2;
            private System.Windows.Forms.Button button2;
            private System.Windows.Forms.GroupBox groupBox1;
            private System.Windows.Forms.TextBox textBox3;
            private System.Windows.Forms.ComboBox comboBox3;
            private System.Windows.Forms.CheckBox checkBox3;
            private System.Windows.Forms.Button button3;
        }
        public class ForwardWithEnterForm : Form
        {
            public ForwardWithEnterForm()
            {
                this.KeyPreview = true;
            }
            private void SelectNext(Control c)
            {
                if (c.Focused)
                {
                    Control skipControls = this.GetNextControl(c, true);
                    do
                    {
                        while (skipControls is Button ||
                            skipControls is GroupBox ||
                            skipControls is Panel) skipControls = this.GetNextControl(skipControls, true);
                        if (null != skipControls)
                        {
                            skipControls.Select();
                            break;
                        }
                        else
                            skipControls = this.Controls[0];
                    } while (true);
                    return;
                }
                foreach (Control childControl in c.Controls)
                {
                    SelectNext(childControl);
                }
            }
            protected override void OnKeyDown(KeyEventArgs e)
            {
                switch (e.KeyCode)
                {
                    case Keys.Enter:
                        SelectNext(this);
                        break;
                    default:
                        base.OnKeyDown(e);
                        break;
                }
            }
        }
    }
    

    Please help us improve this community forum for visitors by marking the replies as answers if they help and unmarking them if they provide no help.
    Thanks.
    Sunday, February 21, 2010 9:42 AM
  • Thank you for your new codes.
    it works perfect except one problem.
    i have panel1 and panel2 and panel3.
    panel1 have textbox1(tabindex=1)and button1 (tabindex=2) and textbox2(tabindex=3) and textbox3(tabindex=4).
    panel2 have tabcontrol. tabcontrol have groupbox1 and groupbox2 and groupbox3(tabindex=5). groupbox3 have textbox5
    My cursor go from textbox1 to textbox5. my cursor go all textbox with tabindex line except textbox2 and textbox3.
    What can be problem here ?
    Thanks
    Sunday, February 21, 2010 10:21 AM
  • In form designer, click View menu then "Tab Orders" then set tab indexes correctly.
    Please help us improve this community forum for visitors by marking the replies as answers if they help and unmarking them if they provide no help.
    Thanks.
    Sunday, February 21, 2010 10:34 AM
  • ok i found it. i will set it now

    Sunday, February 21, 2010 11:13 AM
  • i set tabindexes from taborder of view menu. But there is same problem still. my textbox2 and textbox3 on panel1 dont be focus. also my one textbox on grouppanel1 that on tabpege dont be focus. my one textbox on grouppanel3 that on tabpege dont be focus. other textboxes works good. what can be problem for this ? Thanks

    Note: My tab key work true.
    Sunday, February 21, 2010 11:33 AM
  • i set tabindexes from taborder of view menu. But there is same problem still. my textbox2 and textbox3 on panel1 dont be focus. also my one textbox on grouppanel1 that on tabpege dont be focus. my one textbox on grouppanel3 that on tabpege dont be focus. other textboxes works good. what can be problem for this ? Thanks

    Note: My tab key work true.

    i tried to copy my designer.cs to here but i couldnt do it. I dont know why. i think that my codes are so long.
    Monday, February 22, 2010 8:06 AM
  • Don't worry, mail it to yasser.zamani@live.com.NO_SPAM (remove NO_SPAM).
    Please help us improve this community forum for visitors by marking the replies as answers if they help and unmarking them if they provide no help.
    Thanks.
    Monday, February 22, 2010 8:09 AM
  • hello
    thank you for helping. i have sent my form to your e-mail.
    Monday, February 22, 2010 10:45 AM
  • Thanks for the mail. I received it.
    I will response as soon as possible.
    Thanks again.


    Please help us improve this community forum for visitors by marking the replies as answers if they help and unmarking them if they provide no help.
    Thanks.
    Monday, February 22, 2010 10:49 AM
  • Hello

    I have did like below.
    ENTER key works like TAB key. it is good. But there is a problem. When ENTER key focus on a button and i press ENTER key, another form be opened that anotherform.show() in button_click. How can i ignore it ? it should be work when i click button with mouse. Enter key should not be work like mouse click at here. i have button1, button2, button3 etc. as name. button1 should be work when i click with mouse or i press ENTER key. Athor buttons should not be work with ENTER key.

     protected override void OnKeyDown(KeyEventArgs e)
            {
                switch (e.KeyCode)
                {
                    case Keys.Enter:
                        SendKeys.Send("{TAB}");
                       // SelectNext(this);
                        break;
                    default:
                        base.OnKeyDown(e);
                        break;
                }
            }
    • Marked as answer by JuniorCsharp Tuesday, February 23, 2010 9:46 AM
    Monday, February 22, 2010 5:16 PM
  • Hello dear,

    Set your buttons TabStop Property to false.
    Please help us improve this community forum for visitors by marking the replies as answers if they help and unmarking them if they provide no help.
    Thanks.
    Monday, February 22, 2010 6:21 PM
  • Hello
    Thank you for setting tabstop property to false.
    But what will happen when i focus on linklabel ? it doesnt have tabstop property.
    Tuesday, February 23, 2010 7:06 AM
  • Don't worry, put following code in the form constructor after InitializeComponent()
    ((Control)linkLabel1).TabStop = false;

    Please help us improve this community forum for visitors by marking the replies as answers if they help and unmarking them if they provide no help.
    Thanks.
    • Marked as answer by JuniorCsharp Tuesday, February 23, 2010 9:46 AM
    Tuesday, February 23, 2010 7:28 AM
  • How can i use this code in asp.net(web application)
    Thursday, November 11, 2010 12:50 PM