Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task 3 #7

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions ClockApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,15 @@
<Compile Include="Form1.Designer.cs">
<DependentUpon>Form1.cs</DependentUpon>
</Compile>
<Compile Include="models\WorldTimeItem.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TimerOverForm.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="TimerOverForm.Designer.cs">
<DependentUpon>TimerOverForm.cs</DependentUpon>
</Compile>
<EmbeddedResource Include="Form1.resx">
<DependentUpon>Form1.cs</DependentUpon>
</EmbeddedResource>
Expand All @@ -67,6 +74,9 @@
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<EmbeddedResource Include="TimerOverForm.resx">
<DependentUpon>TimerOverForm.cs</DependentUpon>
</EmbeddedResource>
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
Expand Down
50 changes: 48 additions & 2 deletions ClockTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ClockApp
{
Expand All @@ -15,12 +16,57 @@ class Alarm
public string Text;
}

class Timer
public class Timer
{
TimeSpan SaveTime;
public TimeSpan InitialTime;
public TimeSpan TimeLeft = new TimeSpan();
public bool IsActive = false;
public bool IsActive = true;
public string Text;
public GroupBox Box;
public Label LabelTimer;
public Button PauseBtn;
public Button ResetBtn;

private TimeSpan GetNowTimeSpan()
{
DateTime dt = DateTime.Now;
TimeSpan result = new TimeSpan(dt.Hour, dt.Minute, dt.Second);
return result;
}
public Timer()
{
SaveTime = GetNowTimeSpan();
}

public void Pause()
{
IsActive = false;

}

public void Unpause()
{
IsActive=true;
SaveTime = GetNowTimeSpan();
}

public void TimeTick()
{
if(IsActive)
{
TimeSpan newTimeSpan = GetNowTimeSpan();
TimeLeft -= newTimeSpan - SaveTime;
SaveTime = newTimeSpan;
}
}
public void Reset()
{
TimeLeft = InitialTime;
Unpause();
LabelTimer.Text = TimeLeft.ToString();
}

}

class Stopwatch
Expand Down
259 changes: 238 additions & 21 deletions Form1.Designer.cs

Large diffs are not rendered by default.

243 changes: 242 additions & 1 deletion Form1.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,261 @@
using System;
using ClockApp.models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;

namespace ClockApp
{
public partial class Form1 : Form
{
private ObservableCollection<models.WorldTimeItem> items;
private TimeSpan zeroTimeSpan = TimeSpan.Zero;
public Form1()
{
InitializeComponent();
items = new ObservableCollection<models.WorldTimeItem>();

}

private void Form1_Load(object sender, EventArgs e)
{
var tz = TimeZoneInfo.GetSystemTimeZones();
comboBoxAddTimezone.DataSource = tz;
comboBoxAddTimezone.DisplayMember = "DisplayName";
comboBoxAddTimezone.ValueMember = "Id";
items.CollectionChanged += Items_CollectionChanged;
timer1.Start();
}

private void Items_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
switch (e.Action)
{
case System.Collections.Specialized.NotifyCollectionChangedAction.Add:
AddTime((WorldTimeItem)e.NewItems[0]);
break;
case System.Collections.Specialized.NotifyCollectionChangedAction.Remove:
RemoveTime((WorldTimeItem)e.OldItems[0]);
break;
}
}
private void AddTime(WorldTimeItem item)
{
string tzName = TimeZoneInfo.GetSystemTimeZones().FirstOrDefault(i => i.Id == item.Id).DisplayName;
GroupBox gb = new GroupBox();
gb.Height = 50;
gb.Width = 770;
gb.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
Label name = new Label();
name.Text = tzName;
name.Location = new Point(5, 10);
name.Width = 700;
name.Height = 20;
Label time = new Label();
time.Text = "";
time.Location = new Point(5, 30);
time.Width = 700;
time.Height = 20;
time.Tag = gb;
Button btn = new Button();
btn.Text = "X";
btn.Location = new Point(740, 10);
btn.Width = 20;
btn.Height = 20;
btn.Tag = gb;
btn.Click += BtnRemoveTZ_Click;

gb.Controls.Add(name);
gb.Controls.Add(time);
gb.Controls.Add(btn);
gb.Tag = (string)comboBoxAddTimezone.SelectedValue;
flowLayoutPanelTimezones.Controls.Add(gb);
item.control = time;
}
private void RemoveTime(WorldTimeItem item)
{
var gb = (GroupBox)((Label)item.control).Tag;
gb.Dispose();
}

private void buttonAddTimezone_Click(object sender, EventArgs ev)
{
if (comboBoxAddTimezone.SelectedIndex != -1)
{
items.Add(new WorldTimeItem() { Id = (string)comboBoxAddTimezone.SelectedValue });
}
}

private void BtnRemoveTZ_Click(object sender, EventArgs e)
{
var gb = (GroupBox)((Button)sender).Tag;
string id = (string)gb.Tag;
var item = items.FirstOrDefault(i => i.Id == id);
items.Remove(item);
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
timer1.Stop();
}
//Серьгей савельев Иллюзия моделирования мозга
private void timer1_Tick(object sender, EventArgs e)
{
foreach (var item in items)
{
var tz = TimeZoneInfo.GetSystemTimeZones().FirstOrDefault(i => i.Id == item.Id);
item.control.Text = (DateTime.UtcNow + tz.BaseUtcOffset).ToString();
}
foreach(var timer in Program.Timers)
{
if (timer.IsActive)
{
timer.TimeTick();
timer.LabelTimer.Text = timer.TimeLeft.ToString();
if (timer.TimeLeft <= zeroTimeSpan)
{
timer.Pause();
//MessageBox.Show(timer.Text);
TimerOverForm timerOverForm = new TimerOverForm(timer);
}
}
}
}

private void addTimer(int hour, int minute, int seconds, string text)
{
GroupBox gb = new GroupBox();
Button pauseBtn = new Button();
Button resetBtn = new Button();

Timer timer = new Timer()
{
Text = text,
InitialTime = new TimeSpan(hour, minute, seconds),
TimeLeft = new TimeSpan(hour, minute, seconds),
Box= gb,
IsActive = true,
PauseBtn = pauseBtn,
ResetBtn = resetBtn
};
Program.Timers.Add(timer);

gb.Height = 60;
gb.Width = 770;
gb.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;

Label name = new Label();
name.Text = timer.TimeLeft.ToString();
name.Location = new Point(5, 10);
name.Width = 300;
name.Height = 20;
Label time = new Label();
time.Text = text;
time.Location = new Point(5, 40);
time.Width = 700;
time.Height = 20;
time.Tag = gb;

Button btn = new Button();
btn.Text = "X";
btn.Location = new Point(740, 10);
btn.Width = 20;
btn.Height = 20;
btn.Tag = gb;
btn.Click += deleteTimer;


pauseBtn.Text = "| |";
pauseBtn.Location = new Point (700, 15);
pauseBtn.Width = 25;
pauseBtn.Height = 25;
pauseBtn.Tag = timer;
pauseBtn.Click += pauseTimerBtn_Click;

resetBtn.Text = "R";
resetBtn.Location = new Point(665, 15);
resetBtn.Width = 25;
resetBtn.Height = 25;
resetBtn.Tag = timer;
resetBtn.Click += resetTimerBtn_Click;

gb.Controls.Add(name);
gb.Controls.Add(time);
gb.Controls.Add(btn);
gb.Controls.Add(pauseBtn);
gb.Controls.Add(resetBtn);
gb.Tag = (string)comboBoxAddTimezone.SelectedValue;
timerFlowLayoutPanel.Controls.Add(gb);

timer.LabelTimer = name;
//timerFlowLayoutPanel.Controls.Remove(gb);
}

private void resetTimerBtn_Click(object sender, EventArgs e)
{
Timer timer = (Timer)((Button)sender).Tag;
timer.Reset();
if (!timer.PauseBtn.Enabled)
{
timer.PauseBtn.Show();
timer.PauseBtn.Enabled = true;
}
}

private void pauseTimerBtn_Click(object sender, EventArgs e)
{
Button btn = ((Button)sender);
((Timer)btn.Tag).Pause();
btn.Text = "|>";
btn.Click -= pauseTimerBtn_Click;
btn.Click += unpauseTimerBtn_Click;
}
private void unpauseTimerBtn_Click(object sender, EventArgs e)
{
Button btn = ((Button)sender);
((Timer)btn.Tag).Unpause();
btn.Text = "||";
btn.Click -= unpauseTimerBtn_Click;
btn.Click += pauseTimerBtn_Click;
}
private void deleteTimer(object sender, EventArgs e)
{
var gb = (GroupBox)((Button)sender).Tag;
foreach (var i in Program.Timers)
{
if (i.Box == gb)
{
Program.Timers.Remove(i);
break;
}

}
timerFlowLayoutPanel.Controls.Remove(gb);
}
private void deleteTimer(GroupBox sender)
{
var gb = sender;
foreach (var i in Program.Timers)
{
if (i.Box == gb)
{
Program.Timers.Remove(i);
break;
}

}
timerFlowLayoutPanel.Controls.Remove(gb);
}
private void timerAddButton_Click(object sender, EventArgs e)
{
addTimer((int)hourNumeric.Value, (int)minuteNumeric.Value, (int)secondNumeric.Value, timerMessageTextBox.Text);
}
}
}
3 changes: 3 additions & 0 deletions Form1.resx
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,7 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="timer1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
</root>
Loading