欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

限制RICHTEXTBOX的输入的范围

发布时间:2023/12/20 编程问答 43 豆豆
生活随笔 收集整理的这篇文章主要介绍了 限制RICHTEXTBOX的输入的范围 小编觉得挺不错的,现在分享给大家,帮大家做个参考.


 
附件: http://files.cnblogs.com/xe2011/WindowsFormsApplication_LimitRichTextBoxInput.rar

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;//Columnreturn 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("\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 senderKeyPressEventArgs 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 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;         }     } }



来自为知笔记(Wiz)



附件列表

 

转载于:https://www.cnblogs.com/xe2011/p/3780793.html

创作挑战赛新人创作奖励来咯,坚持创作打卡瓜分现金大奖

总结

以上是生活随笔为你收集整理的限制RICHTEXTBOX的输入的范围的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。