下面代码实现的是:如果输入大于64个字符 则一个按钮变绿
怎么实现:输入一段字符后,按钮变绿 删除64个字符后 按钮便会变回原来的control 颜色
- C# code
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; } }
------解决方案--------------------
MessageBox.Show("内存已满");//如何实现当按钮全为绿色的时候 text_content不能输入只能删除?
把这句改成e.Cancel = true;
试试
------解决方案--------------------
设置一个全局变量,来控制这个输入多少的字符..
在事件里判断全局变量的值,本人认为这样效率高点..
------解决方案--------------------
加入 文本框 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;
}
}
(纯手写的,不是很好,见谅见谅!)
------解决方案--------------------
- C# code
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; } }