• 限制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;
            }
        }
    }
     
     
     
     
     
     
     
     
     
     
     





    附件列表

  • 相关阅读:
    Windows Server 设置自动登陆
    Kettle学习笔记(四)— 总结
    Kettle学习笔记(一)— 环境部署及运行
    Web项目自动打开并且全屏
    数据库SQL Server 2016“功能选择”详细说明及精简安装选择
    kettle学习笔记(三)— 定时任务的脚本执行
    Kettle学习笔记(二)— 基本操作
    effective C++ 条款 54:让自己熟悉包括TR1在内的标准程序库
    [转]基于MFC的ActiveX控件开发
    effective C++ 条款 50:了解new和delete的合理替换时机
  • 原文地址:https://www.cnblogs.com/xe2011/p/3780793.html
Copyright © 2020-2023  润新知