C# Enter轉Tab,讓MultiLine可換行

  • 6891
  • 0
  • C#
  • 2011-12-24

Enter轉Tab,讓MultiLine可換行

寫Windows Form,常常為了使用者輸入方便,需要用到Enter轉Tab 這個功能

但有的備註欄因為可能比較多行,所以需要設為MultiLine,

而MultiLine這欄如果也讓他執行tab,那我們就不能換行了

所以我們透過this.ActiveControl.Name 來取得控制項的名稱,讓他不做換行的動作

 

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 EnterTab
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.KeyPreview = true;
        }

        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            if (e.KeyChar == 13 && this.ActiveControl.Name != "MultiTxt")
            {
                this.SelectNextControl(this.ActiveControl, true, true, true, true);
                base.OnKeyPress(e);
            }
        }
    }
}

範例下載

 


如有錯誤 歡迎指正