[英]Problem changing condicionally BackColor in a textbox form
I am trying to design an statement in a textbox of a Windows form so that the backcolor changes if the input given is <
, >
, or ==
to a given variable.我正在尝试在 Windows 表单的文本框中设计一个语句,以便如果给定的输入为<
、 >
或==
给定变量,则背景颜色会发生变化。
I've been trying with the if
/ else if
and switch
.我一直在尝试使用if
/ else if
和switch
。
private void txtPuntosTotalesAGenerar_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
int finalSamplePoints = Convert.ToInt32(Console.ReadLine());
int try1 = 1500;
if (finalSamplePoints > try1)
{
txtPuntosTotalesAGenerar.BackColor = Color.Orange;
}
else if (finalSamplePoints < try1)
{
txtPuntosTotalesAGenerar.BackColor = Color.Red;
}
else if (finalSamplePoints == try1)
{
txtPuntosTotalesAGenerar.BackColor = Color.Green;
}
}
}
With this code I have been able to obtain the red background color, but it is never changing, no matter the values I introduce in the textbox.使用此代码,我已经能够获得红色背景颜色,但无论我在文本框中引入的值如何,它都不会改变。
The issue is Console.ReadLine()
always gives you 0, when the Enter Key is pressed.问题是Console.ReadLine()
在按下 Enter 键时总是给你 0。
I think you need to modify the logic like this我认为您需要像这样修改逻辑
private void txtPuntosTotalesAGenerar_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
int finalSamplePoints = Convert.ToInt32(txtPuntosTotalesAGenerar.Text);
int try1 = 1500;
if (finalSamplePoints > try1)
{
txtPuntosTotalesAGenerar.BackColor = Color.Orange;
}
else if (finalSamplePoints < try1)
{
txtPuntosTotalesAGenerar.BackColor = Color.Red;
}
else if (finalSamplePoints == try1)
{
txtPuntosTotalesAGenerar.BackColor = Color.Green;
}
}
}
Obviously you need to add appropriate validation to make sure any non numeric input is handled appropriately.显然,您需要添加适当的验证以确保正确处理任何非数字输入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.