下面代码实现的是:如果输入大于64个字符 则一个按钮变绿
怎么实现:输入一段字符后,按钮变绿 删除64个字符后 按钮便会变回原来的control 颜色
 private void text_content_TextChanged(object sender, EventArgs e)
        {
            
            int i=0;
            int count=Encoding.Unicode.GetByteCount(text_content.Text)/ 64;
            textBox_charnumber.Text = Encoding.Unicode.GetByteCount(text_content.Text).ToString();//输出text_content里的字符数。
           
            for ( i = 0; i <= count; i++)//定义了64个按钮。
            {
                btn[i].BackColor = Color.Green;
            }
            if (btn[63].BackColor == Color.Green)
            {
                MessageBox.Show("内存已满");//如何实现当按钮全为绿色的时候 text_content不能输入只能删除?
                
                return;
            }
        }

解决方案 »

  1.   

     MessageBox.Show("内存已满");//如何实现当按钮全为绿色的时候 text_content不能输入只能删除?
    把这句改成e.Cancel = true;
    试试
      

  2.   

    加入 文本框 keyPress事件,可以设置一个bool变量,按钮绿时设置为True,keyPress事件里判断如果为true并且不是删除键则e.handled为True 
    也可以:
    private void text_content_TextChanged(object sender, EventArgs e)
    {
        if (btn[63].BackColor == Color.Green && 
            e.KeyChar != 8
            e.KeyChar != 13)
        {
            e.Handled=true;
        }
    }
    (纯手写的,不是很好,见谅见谅!)
      

  3.   

    额,补充一下, 13那行是不要的,只能删除。 只要不等于8就好了,8是删除键的ASCII码。
    最好设置一个私有全局变量去判断,按照你原有的判断第63个按钮的背景颜色 这样不妥,因为也可能有别的错导致你的按钮颜色错误了。
    呵呵呵,其实这个很简单的。
      

  4.   

            private void text_content_KeyUp(object sender, KeyEventArgs e)
            {            if (user.Text.Length > 10)
                {
                    SendKeys.Send("\b");
                    return;
                }            int i = 0;
                int count = Encoding.Unicode.GetByteCount(text_content.Text) / 64;
                textBox_charnumber.Text = Encoding.Unicode.GetByteCount(text_content.Text).ToString();//输出text_content里的字符数。            for (i = 0; i <= count; i++)//定义了64个按钮。
                {
                    btn[i].BackColor = Color.Green;
                }        }