既然是限制输入长度 为什么不用TextBox的MaxLength属性来限制 而要做得这么复杂如果是需要判断超出长度的话 可以这样写        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if(textBox1.Text.Length > 5)
            {
                //你需要的代码
                //MessageBox.Show("Test");
            }
        }

解决方案 »

  1.   

    怎么才能准确的 触发 textbox的内容在隐藏。
    开始以为是舍去了小说,  
    if (s.Height * ((float) s.Width / textBox1.Width) > textBox1.Height) 这样也不行SizeF f = g.MeasureString(textBox1.Text + e.KeyChar, textBox1.Font);                 Size s = TextRenderer.MeasureText(textBox1.Text + e.KeyChar, textBox1.Font); 感觉都不是很准。
      

  2.   

    private void textBox1_TextChanged(object sender, EventArgs e)
            {
                int dStrLength = Convert.ToInt32(Math.Ceiling(this.CreateGraphics().MeasureString(textBox1.Text, textBox1.Font).Width));
                if (dStrLength >= textBox1.Width)
                {
                    MessageBox.Show("超过长度"); 
                }
            }
      

  3.   

    MeasureString这个是不准确的,可以指定stringformat一般版式试试
      

  4.   

    深更半夜问这么复杂的问题...//所有文本当前的长度
    int AllStrLength = Convert.ToInt32(Math.Ceiling(this.CreateGraphics().MeasureString(txtBoxUserName.Text, txtBoxUserName.Font).Width));
    //单个字符所占的长度
     int xx = Convert.ToInt32(Math.Ceiling(this.CreateGraphics().MeasureString("1", txtBoxUserName.Font).Width));
    // 然后 if 一条语句..
    //   AllStrLength + xx  > txtBoxUserName.size.width 就是超出长度了...// 判断过程 放在    textBox1_TextChanged  , keydown 都可,
    //   放在 textBox1_KeyPress 不准确,少算一个或多算一个字符,
      

  5.   

    求出字体和字号对应的字母宽度 A, 乘以字符长度,如果超出文本框宽度则返回true
      

  6.   

    用api吧,得到字体的宽度,自己在计算!
      

  7.   

    [DllImport("user32")]
            public static extern bool GetCaretPos(out Point lpPoint);
    try
      

  8.   

    private void textBox1_TextChanged(object sender, EventArgs e)
            {
                int dStrLength = Convert.ToInt32(Math.Ceiling(this.CreateGraphics().MeasureString(textBox1.Text, textBox1.Font).Width));
                if (dStrLength >= textBox1.Width)
                {
                    MessageBox.Show("超过长度"); 
                }
            }
      

  9.   

    TextBox可以设置最大输入字符额亲
      

  10.   

    控制winform文本框输入字符限制(数字或字母、长度)
    Simple Numeric TextBox
      

  11.   

    哪有那么复杂private void textBox1_TextChanged(object sender, EventArgs e)
    {
        int index = textBox1.GetCharIndexFromPosition(new Point(0, 0));
        if (index != 0)
        {
            MessageBox.Show("就是现在!");
        }
    }还有右方向键和鼠标触发的...