我想在面板中显示类别名称 [英] I want to show category name in panel
问题描述
大家好!我想在面板中显示类别名称我有隐藏代码,但我不知道如何显示此类别。
我尝试过:
Hello Guys! i want to show category name in panel i have code for hide but i dont know how to show this category.
What I have tried:
dbe = new Database1Entities();
foreach (Control ctrl in flpCategories.Controls)
{
var btn = ctrl as Button;
if (btn != null)
{
int id = int.Parse(btn.Tag.ToString());
var cat = dbe.tblCategory.FirstOrDefault(x => x.categoryId == id);
if (cat != null)
{
if (!((cat.StartTime.Value.Year <= DateTime.Now.Year && cat.StartTime.Value.Month <= DateTime.Now.Month && cat.StartTime.Value.Day <= DateTime.Now.Day && cat.StartTime.Value.Hour <= DateTime.Now.Hour && cat.StartTime.Value.Minute <= DateTime.Now.Minute) && (cat.EndTime.HasValue && cat.EndTime.Value.Year >= DateTime.Now.Year && cat.EndTime.Value.Month >= DateTime.Now.Month && cat.EndTime.Value.Day >= DateTime.Now.Day && cat.EndTime.Value.Hour >= DateTime.Now.Hour && cat.EndTime.Value.Minute >= DateTime.Now.Minute)))
{
flpCategories.Controls.Remove(ctrl);
}
}
}
推荐答案
我知道,我可能会写这样的代码,因为它更容易阅读if语句。它调用此解决方案结尾处显示的扩展方法:
Ya know, I would probably write that code like this, because it's easier to read the if statement. It calls the extension method shown at the end of this solution:
if (cat != null)
{
DateTime start = (cat.StartTime.HasValue) ? cat.StartTime.Value.SetSeconds() : new DateTime(0);
DateTime end = (cat.EndTime.HasValue) ? cat.EndTime.Value.SecSeconds() : new DateTime(0);
DateTime now = DateTime.Now;
bool inRange = (start <= now && end >= now);
if (!(start <= now && end >= now))
{
flpCategories.Controls.Remove(ctrl);
}
}
至于 inRange $ c时显示控件$ c>为true,只需为控件设置可见标志即可。既然你没有指定框架(WinForms,WPF,ASP.Net),我会留给你找到合适的方法来做到这一点。
如果您有兴趣,我写了一个提示/技巧,允许您比较两个 DateTime
对象的所需部分:
部分日期时间对象平等 [ ^ ]
和btw,这是一个方便的扩展方法,用于设置DateTime的秒数。没有参数调用它会自动返回日期值,秒和毫秒为0.
As for showing a control when inRange
is true, simply set the visible flag for the control. Since you didn't specify the framework (WinForms, WPF, ASP.Net), I'll leave it to you to find the appropriate way to do that.
If you're interested, I wrote a tip/trick that allows you to compare the desired parts of two DateTime
objects:
Partial DateTime Object Equality[^]
And btw, here's a handy extension method for setting the seconds from a DateTime. calling it with no parameters will automatically return the date value with the seconds and milliseconds to 0.
public static DateTime SetSeconds(this DateTime date, double secs=0, double millisecs=0)
{
return new DateTime(date.Year, date.Month, date.Day, date.Hour, date.Minute, secs, millisecs, date.Kind);
}
这篇关于我想在面板中显示类别名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!