如何判断用户输入多个空格?如何实现提示用户还可输入多少个字符?就想我们发帖时一样?皆在TextBox下,能不能给点具体代码?在Winform中

解决方案 »

  1.   

    javascript 获取 text.length
      

  2.   

    WinForm 在 text_changed 事件里面判断 textBox.Text.Length
      

  3.   

    在TextChanged事件中实现        private void textBox1_TextChanged(object sender, EventArgs e)
            {
                this.textBox1.MaxLength = 100;
                //显示已经输入多少字符
                this.label1.Text = this.textBox1.Text.Length / this.textBox1.MaxLength + "字";
                if (textBox1.Text.Length >= 100)
                    MessageBox.Show("已经到达" + this.textBox1.MaxLength + "字的上限~!");
            }
      

  4.   


    转一个ajax实现的
    前台:
    <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
            <asp:Timer ID="Timer1" runat="server" Interval="10" OnTick="Timer1_Tick">
            </asp:Timer>
            <asp:TextBox ID="TextBox1" runat="server"  MaxLength="50"></asp:TextBox>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
            
            <asp:Label ID="Label1" runat="server" Text="Label" ForeColor="Red"></asp:Label>
            </ContentTemplate>
            <Triggers>
            <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
            </Triggers>
            </asp:UpdatePanel>后台:
    protected void Timer1_Tick(object sender, EventArgs e)
        {
            Label1.Text = "您还可以输入" + (50 - TextBox1.Text.ToString().Length) + "字";
        }
    可以,把Label1.Text = "您还可以输入" + (50 - TextBox1.Text.ToString().Length) + "字";
    换成:
            Label1.Text = "您还可以输入" + (50 - System.Text.Encoding.Default.GetByteCount(TextBox1.Text.ToString())) + "字"; 
      

  5.   

    在textbox的keypress事件里 用正则匹配
    \s{2,} 这样,就是有2个空格了
    还有多少的话,这个,太简单了
    textbox.text.length.
    自己减一下
      

  6.   

    空格数
            private void textBox1_TextChanged(object sender, EventArgs e)
            {
                //空格数
                int spaceLength = this.textBox.Text.Length - this.textBox.Text.replace(" ", "").Length;        }
      

  7.   

    先在textBox1的属性框中设置可输入的最大字符数
            private void textBox1_TextChanged(object sender, EventArgs e)
            {
                int count1 = 0;//空格数
                int count2 = textBox1.MaxLength - textBox1.Text.Length;//还可输入的字符(或汉字)数
                //获取输入的空格数
                for (int i = 0; i < textBox1.Text.Length; i++)
                {
                    if (textBox1.Text.Substring(i, 1) == " ")//注意此处必须写成" ",""中只能有一个空格
                    {
                        count1++;
                    }
                }
                MessageBox.Show("空格数为:" + count1.ToString());
                MessageBox.Show("还可输入的字符(或汉字)数为:" + count2.ToString());
            }
      

  8.   

    textBox1.TextChanged   +=   new   EventHandler(textBox1_TextChanged); 
    void   textBox1_TextChanged(object   sender,   EventArgs   e) 

    if(this.textBox1.Text.Length >100) 

    MessageBox.Show( "不能超过100."); 

    } 长度也可使用GetByteCount获取
      

  9.   

    if(this.textBox1.Text.Length >100)  
    {  
    MessageBox.Show( "不能超过100.");  
    }