附件:
 

 
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. bzoj 2555

    暴力. 收获: 1.第一道后缀自动机,大概知道怎么写了,有一些原理性的东西还要理解. 2.计算right集合的大小 /***************************************** ...

  2. Trie树理解

    前言 Trie树又称单词查找树,字典树,是哈希树的变种: 优点在于:最大限度地减少无谓的字符串比较,查询效率比哈希高: 缺点在于:空间消耗很大: 性质 其基本性质可以归纳为: 跟结点不包括字符,除跟结 ...

  3. HTML5 UI 控件Mobiscroll的使用(年月日三级联动)

    概述: 遇到制作一个html5界面,需要选择年月日,其实这个功能很常用.一般我们都是网上找,之前也没有收藏一个自己常用的,今天发现一个不错的库.特此记录一下使用过程,以便以后遇到了方面查阅. 1.官方 ...

  4. HDU 5714 拍照 前缀和

    拍照 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5714 Description 小明在旅游的路上看到了一条美丽的河,河上有许多船只,有的船只向左 ...

  5. BZOJ 1208: [HNOI2004]宠物收养所 SET的妙用

    1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4902  Solved: 1879 题目连接 http:/ ...

  6. Poj 题目分类

    初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推. ...

  7. 我们为什么需要Map-Reduce?

    在讨论我们是否真的需要Map-Reduce这一分布式计算技术之前,我们先面对一个问题,这可以为我们讨论这个问题提供一个直观的背景. 问题 我们先从最直接和直观的方式出发,来尝试解决这个问题: 先伪一下 ...

  8. axure8.1可用授权码

    Licensee: University of Science and Technology of China (CLASSROOM)Key: DTXRAnPn1P65Rt0xB4eTQ+4bF5IU ...

  9. mysql查询null异常:attempted to return null from a method with a primitive return type

    select sum(deposit_amount)from tb_commission_ib_day mysql查询时报异常: attempted to return null from a met ...

  10. 第一次ACM赛后总结及感悟

    2014 "嘉杰信息"杯 ACM/ICPC湖南程序设计邀请赛暨第六届湘潭程序设计比赛 赛后总结,尽管已经是大二第二学期了,这却是我的第一次真正的ACM比赛经历,大一尽管说就已经进了 ...