C# 转换和查找值

C# 转换和查找值,c#,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")

我需要帮助找到一个值和两个数字之间的值,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;
        }
    }

您应该将长度保持为一个数字,这样您就可以将其与其他数字进行正确比较:

int length = richTextBox1.TextLength;
l6.Text = length + "/300";

// when the length is 0 or higher than 275
if (length == 0 || length > 275)
{
    // make the text red
    l6.ForeColor = Color.Red;
}
else
{
    // otherwise keep it black
    l6.ForeColor = Color.Black;
}

您的替代解决方案做了一些完全不同的事情。根据OP的问题,对于长度超过275的文本,文本需要为红色,否则为黑色。@pokeYou知道,问题和示例没有清楚地说明所需的行为。我试图表达的想法是使用长度作为int,并根据条件使用任何颜色;对于所有这些情况,我打字时犯了一个小错误,无法编辑
int length = richTextBox1.TextLength;
l6.Text = length + "/300";

// when the length is 0 or higher than 275
if (length == 0 || length > 275)
{
    // make the text red
    l6.ForeColor = Color.Red;
}
else
{
    // otherwise keep it black
    l6.ForeColor = Color.Black;
}