c# - 转换和查找值
问题描述
我需要帮助找到两个数字(0~274)之间的一个值和一个值,如果该值介于这些值之间,它将允许我的一个表单上的文本为黑色。如果文本为 275~300,则文本将为红色。
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
string Lent = richTextBox1.TextLength.ToString();
l6.Text = Lent + "/300";
if (Lent == "275")
{
l6.ForeColor = Color.Red;
}
else if (Lent == "274")
{
l6.ForeColor = Color.Red;
}
else if (Lent == "0")
{
l6.ForeColor = Color.Red;
}
}
l6
是我的label6
,它显示来自 的文本长度richTextBox
,例如"0/300"
. 我试图找到两者之间的值但失败了,我真的需要一些帮助!
解决方案
对范围使用整数比较。
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
var textLength = richTextBox1.TextLength;
l6.Text = @"{textLength}/300";
// Add ranges in condition and set color.
if (textLength == 0 || textLength <= 274)
{
l6.ForeColor = Color.Black; //Whatever color
}
else if (textLength > 275)
{
l6.ForeColor = Color.Red;
}
}
替代且更具可读性的解决方案。
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
var textLength = richTextBox1.TextLength;
l6.Text = @"{textLength}/300";
l6.ForeColor = (textLength >= 275) ? Color.Red : Color.Black;
}
推荐阅读
- python - 如何将 Python 项目打包到 MSIX 包中?
- spring-mvc - 为什么要从 ArrayList<> 迭代最后一个数据?
- java - Google Drive API“发布到网络”一个.pptx文件上传到谷歌驱动器
- python - 有没有办法在 Kivy 中制作一条跨越整个 Widget 的垂直线?
- power-automate - 如何避免错误的“表达式无效”错误和不更新的表达式更新?
- java - Eureka 客户端重新注册需要时间
- javascript - 在 jQuery 中,是否可以存储通配符以便在函数中进一步使用?
- javascript - 如何通过光标后的元素触发悬停事件
- android - Android 实时开发者通知
- java - 带钥匙斗篷的安全弹簧靴不会返回 403