все, лаба 4 работает

This commit is contained in:
Елена Бакальская 2024-10-18 02:07:05 +04:00
parent af8268ea0a
commit 41161517fe
2 changed files with 227 additions and 4 deletions

150
lab4/Form1.Designer.cs generated
View File

@ -31,6 +31,19 @@
dateTimePicker = new DateTimePicker(); dateTimePicker = new DateTimePicker();
label1 = new Label(); label1 = new Label();
numericUpDown = new NumericUpDown(); numericUpDown = new NumericUpDown();
button = new Button();
comboBoxFormats = new ComboBox();
dateTimePicker1 = new DateTimePicker();
dateTimePicker2 = new DateTimePicker();
label2 = new Label();
label3 = new Label();
buttonBrth = new Button();
label4 = new Label();
dateTimePicker3 = new DateTimePicker();
dateTimePicker4 = new DateTimePicker();
textBox1 = new TextBox();
btnToString = new Button();
btnToDate = new Button();
((System.ComponentModel.ISupportInitialize)numericUpDown).BeginInit(); ((System.ComponentModel.ISupportInitialize)numericUpDown).BeginInit();
SuspendLayout(); SuspendLayout();
// //
@ -57,11 +70,133 @@
numericUpDown.Size = new Size(150, 27); numericUpDown.Size = new Size(150, 27);
numericUpDown.TabIndex = 2; numericUpDown.TabIndex = 2;
// //
// button
//
button.Location = new Point(644, 10);
button.Name = "button";
button.Size = new Size(312, 29);
button.TabIndex = 3;
button.Text = "вывести в определенном вормате";
button.UseVisualStyleBackColor = true;
button.Click += button_Click;
//
// comboBoxFormats
//
comboBoxFormats.FormattingEnabled = true;
comboBoxFormats.Location = new Point(466, 11);
comboBoxFormats.Name = "comboBoxFormats";
comboBoxFormats.Size = new Size(151, 28);
comboBoxFormats.TabIndex = 4;
//
// dateTimePicker1
//
dateTimePicker1.Location = new Point(12, 193);
dateTimePicker1.Name = "dateTimePicker1";
dateTimePicker1.Size = new Size(250, 27);
dateTimePicker1.TabIndex = 5;
//
// dateTimePicker2
//
dateTimePicker2.Location = new Point(335, 193);
dateTimePicker2.Name = "dateTimePicker2";
dateTimePicker2.Size = new Size(250, 27);
dateTimePicker2.TabIndex = 6;
//
// label2
//
label2.AutoSize = true;
label2.Location = new Point(15, 229);
label2.Name = "label2";
label2.Size = new Size(150, 20);
label2.TabIndex = 7;
label2.Text = "Ваш день рождения";
//
// label3
//
label3.AutoSize = true;
label3.Location = new Point(335, 229);
label3.Name = "label3";
label3.Size = new Size(161, 20);
label3.TabIndex = 8;
label3.Text = "День рождения друга";
//
// buttonBrth
//
buttonBrth.Location = new Point(690, 195);
buttonBrth.Name = "buttonBrth";
buttonBrth.Size = new Size(94, 29);
buttonBrth.TabIndex = 9;
buttonBrth.Text = "рассчитать";
buttonBrth.UseVisualStyleBackColor = true;
buttonBrth.Click += buttonBrth_Click;
//
// label4
//
label4.AutoSize = true;
label4.Location = new Point(199, 280);
label4.Name = "label4";
label4.Size = new Size(0, 20);
label4.TabIndex = 10;
//
// dateTimePicker3
//
dateTimePicker3.Location = new Point(12, 420);
dateTimePicker3.Name = "dateTimePicker3";
dateTimePicker3.Size = new Size(250, 27);
dateTimePicker3.TabIndex = 11;
//
// dateTimePicker4
//
dateTimePicker4.Location = new Point(706, 420);
dateTimePicker4.Name = "dateTimePicker4";
dateTimePicker4.Size = new Size(250, 27);
dateTimePicker4.TabIndex = 12;
//
// textBox1
//
textBox1.Location = new Point(382, 422);
textBox1.Name = "textBox1";
textBox1.Size = new Size(235, 27);
textBox1.TabIndex = 13;
//
// btnToString
//
btnToString.Location = new Point(259, 506);
btnToString.Name = "btnToString";
btnToString.Size = new Size(94, 29);
btnToString.TabIndex = 14;
btnToString.Text = "в строку";
btnToString.UseVisualStyleBackColor = true;
btnToString.Click += btnToString_Click;
//
// btnToDate
//
btnToDate.Location = new Point(620, 506);
btnToDate.Name = "btnToDate";
btnToDate.Size = new Size(152, 29);
btnToDate.TabIndex = 15;
btnToDate.Text = "обратно в дату";
btnToDate.UseVisualStyleBackColor = true;
btnToDate.Click += btnToDate_Click;
//
// Form1 // Form1
// //
AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450); ClientSize = new Size(977, 612);
Controls.Add(btnToDate);
Controls.Add(btnToString);
Controls.Add(textBox1);
Controls.Add(dateTimePicker4);
Controls.Add(dateTimePicker3);
Controls.Add(label4);
Controls.Add(buttonBrth);
Controls.Add(label3);
Controls.Add(label2);
Controls.Add(dateTimePicker2);
Controls.Add(dateTimePicker1);
Controls.Add(comboBoxFormats);
Controls.Add(button);
Controls.Add(numericUpDown); Controls.Add(numericUpDown);
Controls.Add(label1); Controls.Add(label1);
Controls.Add(dateTimePicker); Controls.Add(dateTimePicker);
@ -77,5 +212,18 @@
private DateTimePicker dateTimePicker; private DateTimePicker dateTimePicker;
private Label label1; private Label label1;
private NumericUpDown numericUpDown; private NumericUpDown numericUpDown;
private Button button;
private ComboBox comboBoxFormats;
private DateTimePicker dateTimePicker1;
private DateTimePicker dateTimePicker2;
private Label label2;
private Label label3;
private Button buttonBrth;
private Label label4;
private DateTimePicker dateTimePicker3;
private DateTimePicker dateTimePicker4;
private TextBox textBox1;
private Button btnToString;
private Button btnToDate;
} }
} }

View File

@ -5,6 +5,8 @@ namespace lab4
public Form1() public Form1()
{ {
InitializeComponent(); InitializeComponent();
//comboBoxFormats.Items.AddRange(new string[] { "Ïîëíàÿ äàòà è âðåìÿ", "Êîðîòêàÿ äàòà", "Äëèííàÿ äàòà", "Òîëüêî âðåìÿ", "Ãîä è ìåñÿö" });
comboBoxFormats.Items.AddRange(new string[] { "F", "d", "D", "T", "Y" });
} }
private void dateTimePicker_ValueChanged(object sender, EventArgs e) private void dateTimePicker_ValueChanged(object sender, EventArgs e)
@ -13,9 +15,82 @@ namespace lab4
int hours = Convert.ToInt32(numericUpDown.Value); int hours = Convert.ToInt32(numericUpDown.Value);
if (date != null) if (date != null)
{ {
label1.Text = "Âû âûáðàëè: " + date.ToString() + " â " + date.Hour.ToString() + " ÷àñîâ è " + date.Minute.ToString() + " ìèíóò"; label1.Text = "Âû âûáðàëè: "
+ date.ToString()
+ " â "
+ date.Hour.ToString()
+ " ÷àñîâ è "
+ date.Minute.ToString()
+ " ìèíóò";
label1.Text += $"\nÀ åñëè áû âû âûáðàëè âðåìÿ ÷åðåç {hours} ÷àñîâ è ñòîëüêî æå äíåé, òî áûëî áû " label1.Text += $"\nÀ åñëè áû âû âûáðàëè âðåìÿ ÷åðåç {hours} ÷àñîâ è ñòîëüêî æå äíåé, òî áûëî áû "
+ date.AddHours(hours).Hour.ToString() + " ÷àñîâ è " + date.AddDays(hours).Day.ToString() + " äíåé ìåñÿöà: " + date.AddDays(hours).Month.ToString(); + date.AddHours(hours).Hour.ToString()
+ " ÷àñîâ è "
+ date.AddDays(hours).Day.ToString()
+ " äíåé ìåñÿöà: "
+ date.AddDays(hours).Month.ToString();
}
}
private void button_Click(object sender, EventArgs e)
{
DateTime date = dateTimePicker.Value;
if (comboBoxFormats.SelectedIndex != -1)
{
label1.Text = "Äàòà â âûáðàííîì ôîðìàòå: " + date.ToString(comboBoxFormats.SelectedItem?.ToString());
}
}
private void buttonBrth_Click(object sender, EventArgs e)
{
DateTime yourB = dateTimePicker1.Value;
DateTime friendBrth = dateTimePicker2.Value;
if (friendBrth < yourB)
{
yourB.AddYears(-friendBrth.Year);
yourB.AddMonths(-friendBrth.Month);
yourB.AddDays(-friendBrth.Day);
label4.Text = "Âàø äðóã ñòàðøå âàñ íà "
+ yourB.AddYears(-friendBrth.Year).Year.ToString()
+ " ëåò, íà "
+ yourB.AddMonths(-friendBrth.Month).Month.ToString()
+ " ìåñÿöåâ è íà "
+ yourB.AddDays(-friendBrth.Day).Day.ToString()
+ " äíåé";
}
else
{
label4.Text = "Âû ñòàðøå äðóãà íà "
+ friendBrth.AddYears(-yourB.Year).Year.ToString()
+ " ëåò, íà "
+ friendBrth.AddMonths(-yourB.Month).Month.ToString()
+ " ìåñÿöåâ è íà "
+ friendBrth.AddDays(-yourB.Day).Day.ToString()
+ " äíåé";
}
}
private void btnToString_Click(object sender, EventArgs e)
{
DateTime date = dateTimePicker3.Value;
textBox1.Text = date.ToString();
}
private void btnToDate_Click(object sender, EventArgs e)
{
if(textBox1.Text != null)
{
string dateStr = textBox1.Text;
try
{
DateTime date = DateTime.Parse(dateStr);
dateTimePicker4.Value = date;
}
catch (Exception)
{
throw;
}
} }
} }
} }