Click here to Skip to main content
15,895,256 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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);
                        }
                    }
                }
Posted
Updated 11-Oct-16 4:13am
Comments
[no name] 11-Oct-16 9:57am    
What? You have the code to remove a panel but you can't figure out how to Add a panel?
Member 9983063 11-Oct-16 9:58am    
i dont want to add panel i just want to add category in panel which category when start time is <= datetime
ZurdoDev 11-Oct-16 10:13am    
Where are you stuck? Put a textbox or a label or some control and then put the category in the textbox or label. Simple.
Karthik_Mahalingam 11-Oct-16 11:06am    
Always use  Reply  button, to post Comments/query to the user, so that the user gets notified and responds to your text.
F-ES Sitecore 11-Oct-16 11:08am    
Rather than adding a category control you should have the category always there and populate it with the relevant text if you need to show it, and if you don't need to show it then set its visibility to hidden, or remove it the way you're removing the button.

1 solution

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:

C#
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);
    }
}


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.

C#
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);
}
 
Share this answer
 
v6
Comments
Member 9983063 11-Oct-16 15:37pm    
sir thank you for your reply but i want to know this code for hide categories but i want to show categories how to do it?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900