限制RICHTEXTBOX的输入的范围

 
 


 

附件: 
 


 
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 WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
       
 #region 设置 和 获得光标所在的行号

        ///要在本类中初始化 richTextBox1 = this;

        private int EM_LINEINDEX = 0x00BB;

        private int EM_LINEFROMCHAR = 0x00C9;

 

        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SendMessage")]

        public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);

 

 

        /// <summary>

        /// 获得光标所在的行号和列号

        /// </summary>

        /// <param name="editControl"></param>

        /// <returns>p.X =列号  p.Y =行号</returns>

        public Point GetCaretPosition()

        {

            int charIndex = (int)SendMessage(richTextBox1.Handle, EM_LINEINDEX, -1, 0);

            int lineIndex = (int)SendMessage(richTextBox1.Handle, EM_LINEFROMCHAR, charIndex, 0);

            Point pt = new Point();

            pt.X = richTextBox1.SelectionStart - charIndex + 1;//Line

            pt.Y = lineIndex + 1;//Column

            return pt;

        }

 

 

        /// <summary>

        /// 转到行

        /// </summary>

        /// <param name="Line">行号</param>

        public void jumpLine(int Line)

        {

            richTextBox1.SelectionStart = SendMessage(richTextBox1.Handle, EM_LINEINDEX, Line - 1, 0);

            richTextBox1.SelectionLength = 0;

            richTextBox1.ScrollToCaret();

        }

 

        #endregion

 
设置 和 获得光标所在的行号
 
        //限制文本的能删除的最小范围
        private int nLimiteLength = 10;
        private void richTextBox1_KeyDown(object senderKeyEventArgs e)
        {
            //放置跨行选中文本然后输入文字
            if (richTextBox1.SelectedText.IndexOf(" ") != -1)
            {
                Text = "MupltiLineSel";
                e.Handled = true;
            }
 
 
            //直接屏蔽的
            //Enter Ctrl+V Ctrl+X DEL
            if (e.KeyData == Keys.Enter ||
                e.KeyData == (Keys.Control|Keys.V)||
                e.KeyData == (Keys.Control|Keys.X)||
          e.KeyData == Keys.Delete   
      )
            {
                Text = "禁止 Enter Ctrl+V Ctrl+X Space";
                e.Handled = true;
            }
 
 
 
            int x = GetCaretPosition().X;
            
            //BACK 
            if (e.KeyData == Keys.Back )
            {
                if (x < nLimiteLength + 1)
                {
                    Text = "禁止 Back";
                    e.Handled = true;
                }
            }
        }
 
           
 
        private void richTextBox1_KeyPress(object senderKeyPressEventArgs e)
        {
            //放置跨行选中文本然后输入文字
            if (richTextBox1.SelectedText.IndexOf(" ") != -1)
            {
                Text = "MupltiLineSel";
                e.Handled = true;
            }
 
            int x = GetCaretPosition().X;
 
            if (x < nLimiteLength)
                e.Handled = true;
 
            //space bar
            if (e.KeyChar == ' ' && x < nLimiteLength)
                e.Handled = true;
        }
 
        private void timer1_Tick(object senderEventArgs e)
        {
            Text = String.Format("X={0},Y={1},SelLength={2}"GetCaretPosition().YGetCaretPosition().XrichTextBox1.SelectedText.Length);
        }
 
        private void Form1_Load(object senderEventArgs e)
        {
            //因为输入汉字能突破上面的限制
            richTextBox1.ImeMode = System.Windows.Forms.ImeMode.Off;
        }
    }
}
 
 
 
 
 
 
 
 
 
 
 





附件列表

原文地址:https://www.cnblogs.com/xe2011/p/3780793.html