附件:
 

 
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, -, );

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

            Point pt = new Point();

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

            pt.Y = lineIndex + ;//Column

            return pt;

        }

        /// <summary>

        /// 转到行

        /// </summary>

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

        public void jumpLine(int Line)

        {

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

            richTextBox1.SelectionLength = ;

            richTextBox1.ScrollToCaret();

        }

        #endregion

设置 和 获得光标所在的行号

 
        //限制文本的能删除的最小范围
        private int nLimiteLength = 10;
        private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
        {
            //放置跨行选中文本然后输入文字
            if (richTextBox1.SelectedText.IndexOf("\n") != -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 sender, KeyPressEventArgs e)
        {
            //放置跨行选中文本然后输入文字
            if (richTextBox1.SelectedText.IndexOf("\n") != -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 sender, EventArgs e)
        {
            Text = String.Format("X={0},Y={1},SelLength={2}", GetCaretPosition().Y, GetCaretPosition().X, richTextBox1.SelectedText.Length);
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            //因为输入汉字能突破上面的限制
            richTextBox1.ImeMode = System.Windows.Forms.ImeMode.Off;
        }
    }
}
 
 
 
 
 
 
 
 
 
 
 

附件列表

限制RICHTEXTBOX的输入的范围的更多相关文章

  1. WPF自定义控件与样式(3)-TextBox & RichTextBox & PasswordBox样式、水印、Label标签、功能扩展

    一.前言.预览 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要是对文本 ...

  2. 将richTextBox中的内容写入txt文件发现不换行(解决方法),在richTextBox指定位置插入文字

    string pathname = dt.ToString().Replace(":", ""); string str = richTextBoxResult ...

  3. 初学c# -- 学习笔记(七) RichTextBox支持GIF

    园子里许明吉博客写的一篇,刚好用到这个,写的非常好.转过来了 不过在应用中也有一些问题,win10下不能中文输入,凑合着进行了修改, 下面是原来的代码: private void button2_Cl ...

  4. RichTextBox文字处理控件属性介绍

    RichTextBox控件是一种既能够输入文本. 又能够修改文本的文字处理控件, 与TextBox控件比较, RichTextBox控件的文字处理功用更加丰厚, 不只能够设定文字的色彩. 字体, 还具 ...

  5. WPF RichTextBox,关键字搜索,样式改变,超链接替换,图文混排

    RichTextBox 只是一个控件,表示对 FlowDocument 对象执行操作的丰富编辑控件.它所承载的内容由其 Document 属性来呈现. Document 是一个 FlowDocumen ...

  6. (C#)RichTextBox控件

    RichTextBox(有格式文本)控件可实现TextBox控件的所有功能. ❶在RichTextBox控件中显示滚动条 RichTextBox可设置Multiline属性来控制是否显示滚动套,tru ...

  7. C# winform 加载网页 模拟键盘输入自动接入访问网络

    声明: 本文原创,首发于博客园 http://www.cnblogs.com/EasyInvoice/p/6070563.html 转载请注明出处. 背景: 由于所在办公室网络限制,笔者每天都使用网络 ...

  8. 《Programming WPF》翻译 第3章 2.处理输入

    原文:<Programming WPF>翻译 第3章 2.处理输入 在Windows应用程序中,又3种基本的用户输入形式:鼠标.键盘和手写板.同时,还有一种更高级输入方式,其可能来自快捷键 ...

  9. C# RichTextBox 制作文本编辑器

    本文利用一个简单的小例子[文本编辑器],讲解RichTextBox的用法,仅供学习分享使用,如有不足之处,还请指正. Windows窗体中的RichTextBox控件用于显示,输入和操作格式化的文本, ...

随机推荐

  1. codecombat js

    #1 // Move to the gem. // Don't touch the walls! // Type your code below. this.moveRight(); this.mov ...

  2. nlogn 求最长上升子序列 LIS

    最近在做单调队列,发现了最长上升子序列O(nlogn)的求法也有利用单调队列的思想. 最长递增子序列问题:在一列数中寻找一些数,这些数满足:任意两个数a[i]和a[j],若i<j,必有a[i]& ...

  3. [Visual Studio] SOA服务框架搭建

    1.服务框架搭建 2.服务模板创建 3.Nuget引用 4.客户端调用 任务点: 1.分析SOA 2.修改SOA架构名称以及关键字 3.使用Nuget添加引用 4.选择服务模板进行创建 5.尝试调用 ...

  4. JavaEE-学习目录

    JavaEE ============================================ Web工作机制 JSP Struts基础 Struts核心文件 Struts数据校验与国际化 S ...

  5. php安装配置

    Content 0.序 1.安装前准备 2.安装PHP 3.配置php-fpm 0.序 本文主要是记录php在 Centos下的安装配置 .文中如无特别说明.表示php-5.6.31代码目录. 1.安 ...

  6. MYSQL用户操作管理大杂烩

    一.创建用户 命令:CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 说明:username - 你将创建的用户名, host - 指定该 ...

  7. WordPress Permissions Update Error [RESOLVED]

    Recently I ran into an issue where an installation of WordPress that had never had any issues updati ...

  8. GDB 自动化操作的技术-PYTHON

    https://github.com/spacewander/debugger-utils http://python.jobbole.com/85415/ https://segmentfault. ...

  9. 解决win8内置管理员无法激活此应用

    解决win8内置管理员无法激活此应用 方法/步骤   在运行中输入:“gpedit.msc”,就会启动组策略编辑器.   依次展开“计算机配置”里面的 “Windows设置” “安全设置” “本地策略 ...

  10. 舌尖上的硬件:CPU/GPU芯片制造解析(高清)(组图)

    一沙一世界,一树一菩提,我们这个世界的深邃全部蕴藏于一个个普通的平凡当中.小小的厨房所容纳的不仅仅是人们对味道的情感,更有推动整个世界前进的动力.要想理解我们的世界,有的时候只需要细细品味一下我们所喜 ...