扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
public partial class TextBoxEx : TextBox
    
{
        public TextBoxEx()
        
{
            InitializeComponent();
        }

        属性#region 属性
        private int m_MaxByteLength = 0;
        [Description("获取或设置用户可在文本框控件中键入或粘贴的最大字节数。0 为允许无限长度。")]
        /**//// <summary>
        /// 获取或设置用户可在文本框控件中键入或粘贴的最大字节数。0 为允许无限长度。
        /// </summary>
        public int MaxByteLength
        
{
            get 
{ return m_MaxByteLength; }
            set 
{ m_MaxByteLength = value; }
        }
}
public string RealText
        
{
            get
            
{
                if (m_MaxByteLength == 0)
                
{
                    return this.Text;
                }
                if (m_MaxByteLength >= GetByteLength(this.Text))
                
{
                    return this.Text;
                }
                string text = this.Text;
                if (string.IsNullOrEmpty(text))
                
{
                    return text;
                }
                char[] c = text.ToCharArray();
                StringBuilder sb = new StringBuilder();
                int count = 0;
                for (int i = 0; i < c.Length; i++)
                
{
                    count += GetByteLength(c[i].ToString());
                    if (m_MaxByteLength >= count)
                    
{
                        sb.Append(c[i]);
                    }
                }
                return sb.ToString();
            }
        }
至此,可以通过设置 MaxByteLength  来限制最大字节数了。
查看本文来源
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。