3
\$\begingroup\$

This is the basic class that I am trying to display:

public class Day
{
    public DateTime Date { get; private set; }

    public SpecialDay SpecialDay { get; set; }

    public DateTime TimeIn { get; private set; }

    public DateTime TimeOut { get; private set; }

    public DayOfWeek DayOfWeek

    public TimeSpan TotalHours
}

It is contained inside a list in in following class:

public class Month
{
    private int _month { get; set; }
    private int _year { get; set; }

    public DateTime Date


    public List<Day> Days { get; private set; }

    public TimeSpan TotalHoursInMonth
}

This is the load function of the main display form

private void MonthDataForm_Load(object sender, EventArgs e)
    {
        int bottomOfPreviousDay = -1;

        foreach (var item in MonthDisplaying.Days)
        {
            DayView dayDisplay = new DayView(item);

            if (bottomOfPreviousDay == -1)
            {
                dayDisplay.Top = this.Top + 10;
            }
            else
            {
                dayDisplay.Top = bottomOfPreviousDay + 2;
            }

            bottomOfPreviousDay = dayDisplay.Bottom;
            this.Controls.Add(dayDisplay);
        }

        this.WindowState = FormWindowState.Maximized;
    }

The form shows a list of days for a given month like so display screenshot

Each line is a separate instance of the Day class, and in the next stage of development there will be buttons to perform different actions on each instance.

Is there a better way for calculating the position of the user controls that are being added? I don't like the way the if/else looks but I am not sure how else to do it.

\$\endgroup\$
  • \$\begingroup\$ Can you add some more context? What exactly are you trying to accomplish here? Are you stacking each of the days on top of eachother? Maybe a screenshot would help too. \$\endgroup\$ – Steve Michael Oct 5 '14 at 13:14
2
\$\begingroup\$

As you seem to specifically ask for: Is there a better way for calculating the position of the user controls that are being added? I don't like the way the if/else looks but I am not sure how else to do it.

My answer will address only this. You can make a better use of the variable and instead of -1 use Top + 10 and later = Bottom + 2. The code:

    int nextTop = this.Top + 10;

    foreach (var item in MonthDisplaying.Days)
    {
        DayView dayDisplay = new DayView(item);

        dayDisplay.Top = nextTop;

        nextTop = dayDisplay.Bottom + 2;

        this.Controls.Add(dayDisplay);
    }
\$\endgroup\$
  • \$\begingroup\$ Thanks!! This is exactly the kind of solution that I was looking for. \$\endgroup\$ – Mord Zuber Oct 5 '14 at 14:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.