HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Adding list of customcontrols

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
customcontrolslistadding

Problem

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 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

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.

Solution

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

Code Snippets

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

Context

StackExchange Code Review Q#64769, answer score: 2

Revisions (0)

No revisions yet.