C# 标签(改变标签颜色)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13632405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# Labels(altering label color)
提问by
In a windows form application, i have six text boxes with labels on them. each label shows the date starting from monday to saturday. There are also buttons to navigate to the previous and next weeks respectively.
I want to be able to highlight in RED the current date each time i load the from. Though i've managed to achieve this, each time i click the next/previous button the label still remains coloured. For example if current date is 29 November Thursday (highlighted in RED), clicking the next button would show 06 December Thursday on that label but also highlighted in red which is wrong because 06 December Thursday isn't the current date. Any ideas how i can solve this problem? Here is my code for altering label color. Thanks
在 Windows 窗体应用程序中,我有六个带有标签的文本框。每个标签显示从星期一到星期六的日期。还有一些按钮可以分别导航到上一周和下一周。
我希望每次加载来源时都能够以红色突出显示当前日期。虽然我已经设法实现了这一点,但每次我单击下一个/上一个按钮时,标签仍然保持彩色。例如,如果当前日期是 11 月 29 日星期四(以红色突出显示),单击下一个按钮将在该标签上显示 06 十二月星期四,但也会以红色突出显示,这是错误的,因为 12 月 6 日星期四不是当前日期。我有什么想法可以解决这个问题吗?这是我更改标签颜色的代码。谢谢
if (label1.Text == DateTime.Now.ToString("dd MMMM dddd"))
label1.ForeColor = Color.Red;
else label1.ForeColor = Color.Black;
if (label2.Text == DateTime.Now.ToString("dd MMMM dddd"))
label2.ForeColor = Color.Red;
else label2.ForeColor = Color.Black;
if (label3.Text == DateTime.Now.ToString("dd MMMM dddd"))
label3.BackColor = Color.Red;
else label3.ForeColor = Color.Black;
if (label4.Text == DateTime.Now.ToString("dd MMMM dddd"))
label4.BackColor = Color.Red;
else label4.ForeColor = Color.Black;
if (label5.Text == DateTime.Now.ToString("dd MMMM dddd"))
label5.BackColor = Color.Red;
else label5.ForeColor = Color.Black;
if (label6.Text == DateTime.Now.ToString("dd MMMM dddd"))
label6.BackColor = Color.Red;
else label6.ForeColor = Color.Black;
回答by Sergey Berezovskiy
I think problem is that you are mixing BackColorand ForeColorsetting. Remove duplicated logic and apply style in same way in one place. Create methods
我认为问题在于您正在混合BackColor和ForeColor设置。删除重复的逻辑并在一个地方以相同的方式应用样式。创建方法
private void ApplyStyleTo(Label label)
{
label.ForeColor = GetLabelForeColor(label.Text);
}
private Color GetLabelForeColor(string text)
{
string todayText = DateTime.Now.ToString("dd MMMM dddd");
return (text == todayText) ? Color.Red : Color.Black;
}
And apply styles to all labels:
并将样式应用于所有标签:
ApplyStyleTo(label1);
// ...
ApplyStyleTo(label6);
BTWActully I'd go further and created custom label for displaying date. Place these labels on your form and set their Dateproperty like this:
顺便说一句,实际上我会更进一步并创建用于显示日期的自定义标签。将这些标签放在您的表单上并Date像这样设置它们的属性:
dateLabel1.Date = DateTime.Now;
Label will format and colorize date. You will be able to change date format and colors.
标签将格式化和着色日期。您将能够更改日期格式和颜色。
public class DateLabel : Label
{
private DateTime? _date;
public DateLabel()
{
Format = "dd MMMM dddd";
TodayForeColor = Color.Red;
}
public DateTime? Date
{
get { return _date; }
set {
_date = value;
Text = _date.HasValue ? _date.Value.ToString(Format) : "";
ForeColor = IsToday ? TodayForeColor : ForeColor;
}
}
public bool IsToday
{
get {
if (!_date.HasValue)
return false;
return _date.Value.Date == DateTime.Today;
}
}
public string Format { get; set; }
public Color TodayForeColor { get; set; }
}

