patterncsharpMinor
Adding list of customcontrols
Viewed 0 times
customcontrolslistadding
Problem
This is the basic class that I am trying to display:
It is contained inside a list in in following class:
This is the load function of the main display form
The form shows a list of days for a given month like so
Each line is a separate instance of the
Is there a better way for calculating the position of the user controls that are being added? I don't like the way the
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
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.