Home > Software design >  my user control doesn't show up on the forms design
my user control doesn't show up on the forms design

Time:01-05

I'm trying to create a calendar for my project and I created a blank user control to use it for each day for a week.

the user control i created is like this:

enter image description here

However, when I run the .cs file , there aren't any changes on the form. The expected output should be something like this:

enter image description here

My form output, but there aren't any "temporary" user controls:

enter image description here

What may be the reason of this problem?

This is my code:

private void displayDays()
{
    DateTime now = DateTime.Now;

    // getting the first day of the month
    DateTime startofthemonth = new DateTime(now.Year, now.Month, 1);

    // getting the count of days of the month
    int day = DateTime.DaysInMonth(now.Year, now.Month);

    // conver the startofthemont to integer
    int dayoftheweek = Convert.ToInt32(startofthemonth.DayOfWeek.ToString("d"));

    // i created here a blank user control from project >> add user control
    for(int i = 1; i < dayoftheweek; i  )
    {
        UserControlBlank ucblank = new UserControlBlank();
        // daycontainer is flowLayoutPanel
        daycontainer.Controls.Add(ucblank);
    }
}

CodePudding user response:

startofthemonth.DayOfWeek is already an int, so there is no need to convert it to a string and then back again.

The problem is the first day of January 2023 is a Sunday, and the value for Sunday is zero (0), so your loop never runs because 1 is not less than 0!

If the intent was to create an entry for each day of the month, then you need to modify your for loop:

// getting the count of days of the month
int daysInMonth = DateTime.DaysInMonth(now.Year, now.Month);
for(int i = 1; i <= daysInMonth; i  )
{
    UserControlBlank ucblank = new UserControlBlank();
    // daycontainer is flowLayoutPanel
    daycontainer.Controls.Add(ucblank);
}

If the intent was to create just the starting week, starting with the first day of the month and ending with the last day of the first week (Saturday), then:

for(int i = startofthemonth.DayOfWeek; i <= DayOfWeek.Saturday; i  )
{
    UserControlBlank ucblank = new UserControlBlank();
    // daycontainer is flowLayoutPanel
    daycontainer.Controls.Add(ucblank);
}
  • Related