[C #] WinForm Next car or TAB key to automatically toggle control focus

Source: Internet
Author: User

Meet the user experience, in the data entry, you can enter a message after the Input or TAB key automatically switch to the next control (field).

When the interface control is designed, it can be implemented by default by setting the TabIndex of the control. But when the layout is adjusted or when the input content is selective, it is better to maintain it from the way the code is handled.

The complete implementation method is as follows:

<summary>///Enter, tab keyboard toggle or Execute action////</summary> public sealed class Tabenter:idisposable {p       rivate list<stringbuilder> ml;       private int i=0;       Private System.Windows.Forms.Control MC;       <summary>//Know whether to enable Tab function///</summary> private bool Mallowtab=false;       <summary>//whether tab Toggle/execute is enabled.           </summary> public bool Allowtab {get {return mallowtab;}       set {Mallowtab = value;}           } public Tabenter (System.Windows.Forms.Control c) {ml = new list<stringbuilder> ();       MC = C;       Public Tabenter (System.Windows.Forms.Control C, bool Allowtab): This (c) {mallowtab = Allowtab;           } public void Add (System.Windows.Forms.Control c) {c.keypress + = Keypresshandler;           C.tabindex = i; ml.           ADD (New StringBuilder (c.name));       i + = 1;       }<summary>////When the keypress time needs to be handled independently, the use of KeyUp to carry out, of course, can continue to implement KeyDown//</summary>//<param Name= "C" ></param> public void Addkeyup (System.Windows.Forms.Control c) {c.keyup + = Keyupha           Ndler;           C.tabindex = i; ml.           ADD (New StringBuilder (c.name));       i + = 1; The private void Keypresshandler (object sender, System.Windows.Forms.KeyPressEventArgs e) {if (E.key char = = (char) 13) | |           (E.keychar = = (Char) 9 && Mallowtab = True)) {Int J = ((System.Windows.Forms.Control) sender).               TabIndex; if (j >= ml.               COUNT-1) return; String cname = Ml[j + 1].               ToString (); if (string.               IsNullOrEmpty (CNAME)) return; system.windows.forms.control[] TCA = MC.               Controls.find (CNAME, true); if (TCA = = NULL | | TCA.               Length = = 0) return;               System.Windows.Forms.Control TC = tca[0]; if (TC = = nULL) return;               System.Windows.Forms.Button B = tc as System.Windows.Forms.Button;               if (b! = null) B.performclick (); Else TC.           Focus (); }} private void Keyuphandler (Object sender, System.Windows.Forms.KeyEventArgs e) {if (E.key Code = = System.Windows.Forms.Keys.Enter) | |           (E.keycode = = System.Windows.Forms.Keys.Tab && Mallowtab = = True)) {Int J = ((System.Windows.Forms.Control) sender).               TabIndex; if (j >= ml.               COUNT-1) return; String cname = Ml[j + 1].               ToString (); if (string.               IsNullOrEmpty (CNAME)) return; system.windows.forms.control[] TCA = MC.               Controls.find (CNAME, true); if (TCA = = NULL | | TCA.               Length = = 0) return;               System.Windows.Forms.Control TC = tca[0];               if (TC = = null) return; if (TC.  GetType () ==typeof (System.Windows.Forms.Button))             {((System.Windows.Forms.Button) TC).               PerformClick (); } else {if (TC. Visible = = True) tc.               Focus ();            }}} #region "Resource free" public void Dispose () {Disposing (true); Gc.        SuppressFinalize (this);        } private bool m_disposed = false;                protected void Disposing (bool Disposing) {if (!m_disposed) {if (Disposing) {//release Managed resources ml.                    Clear ();                    ml = null;                    i = 0;                MC = null;            }//release unmanaged Resources m_disposed = true;        }} ~tabenter () {Disposing (false); } #endregion}

  

[C #] WinForm Next car or TAB key to automatically toggle control focus

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.