Код для Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AnyaTask7
{
public partial class Form1 : Form
{
public string Vopros;
public List<VoteVariant> Varianti = new List<VoteVariant>();
public string PapkaSokhranen;
public DateTime NachaloTime;
public TimeSpan Prodolzhitelnost;
public Timer golosovanTimer;
public DateTime KonecTime;
public int neoprGolosa = 0;
public int obshGolosa = 0;
public Form1()
{
InitializeComponent();
golosovanTimer = new Timer();
golosovanTimer.Interval = 1000;
golosovanTimer.Tick += golosovanTimer_Tick;
golosovanTimer.Start();
}
private void golosovanTimer_Tick(object sender, EventArgs e)
{
var now = DateTime.Now;
txtVremya.Text = now.ToString();
if (now >= NachaloTime && now <= KonecTime)
{
TimeSpan ostalos = KonecTime - now;
lblStatus.Text = $"До конца голосования: {ostalos}";
}
else if (now > KonecTime && KonecTime != DateTime.MinValue)
{
lblStatus.Text = "Голосование завершено";
var files = Directory.GetFiles(PapkaSokhranen);
obshGolosa = files.Length;
foreach (var f in files)
{
string fileText = File.ReadAllText(f);
if (int.TryParse(fileText, out var nomerVarianta) && nomerVarianta >= 0 && nomerVarianta < Varianti.Count)
{
Varianti[nomerVarianta].KolvoProgolos += 1;
}
else
{
neoprGolosa += 1;
label5.Text = $"Неопределенные голоса: {neoprGolosa}";
}
}
golosovanTimer.Stop();
string save = $"{Vopros}\n";
save += $"Дата и время проведения голосования = {NachaloTime}\n";
save += $"Интервал проведения голосования = {Prodolzhitelnost}\n";
foreach (var item in Varianti)
{
save += $"{item.Text} = {item.KolvoProgolos}\n";
}
save += $"Неопределенные голоса = {neoprGolosa}\n";
save += $"Общее количество голосов = {obshGolosa}\n";
File.WriteAllText(Path.Combine(PapkaSokhranen, "Результаты.txt"), save);
}
lstResults.Items.Clear();
foreach (var item in Varianti)
{
lstResults.Items.Add(item);
}
}
private void button2_Click(object sender, EventArgs e)
{
Vopros = txtVopros.Text;
foreach (var item in txtOtveti.Lines)
{
Varianti.Add(new VoteVariant()
{
Text = item,
});
}
NachaloTime = dateTimePicker1.Value;
Prodolzhitelnost = TimeSpan.FromMinutes((int)(numericUpDown1.Value));
KonecTime = NachaloTime + Prodolzhitelnost;
lblVopros.Text = Vopros;
lstResults.Items.Clear();
foreach (var item in Varianti)
{
lstResults.Items.Add(item);
}
}
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog saveFileDialog = new FolderBrowserDialog();
saveFileDialog.ShowDialog();
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
PapkaSokhranen = saveFileDialog.SelectedPath;
}
}
}
}
Код для Form1.Designer.cs
namespace AnyaTask7
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.txtVopros = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.txtOtveti = new System.Windows.Forms.TextBox();
this.labl2 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.dateTimePicker1 = new System.Windows.Forms.DateTimePicker();
this.numericUpDown1 = new System.Windows.Forms.NumericUpDown();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.button2 = new System.Windows.Forms.Button();
this.txtVremya = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.lblStatus = new System.Windows.Forms.Label();
this.lstResults = new System.Windows.Forms.ListBox();
this.lblVopros = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit();
this.SuspendLayout();
//
// txtVopros
//
this.txtVopros.Location = new System.Drawing.Point(103, 28);
this.txtVopros.Name = "txtVopros";
this.txtVopros.Size = new System.Drawing.Size(100, 22);
this.txtVopros.TabIndex = 0;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(42, 28);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(55, 16);
this.label1.TabIndex = 1;
this.label1.Text = "Вопрос";
//
// txtOtveti
//
this.txtOtveti.Location = new System.Drawing.Point(103, 72);
this.txtOtveti.Multiline = true;
this.txtOtveti.Name = "txtOtveti";
this.txtOtveti.Size = new System.Drawing.Size(181, 111);
this.txtOtveti.TabIndex = 2;
//
// labl2
//
this.labl2.AutoSize = true;
this.labl2.Location = new System.Drawing.Point(45, 77);
this.labl2.Name = "labl2";
this.labl2.Size = new System.Drawing.Size(56, 16);
this.labl2.TabIndex = 3;
this.labl2.Text = "Ответы";
//
// button1
//
this.button1.Location = new System.Drawing.Point(50, 189);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(234, 23);
this.button1.TabIndex = 4;
this.button1.Text = "Выбрать папку для сохранения";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// dateTimePicker1
//
this.dateTimePicker1.Location = new System.Drawing.Point(205, 239);
this.dateTimePicker1.Name = "dateTimePicker1";
this.dateTimePicker1.Size = new System.Drawing.Size(200, 22);
this.dateTimePicker1.TabIndex = 5;
//
// numericUpDown1
//
this.numericUpDown1.Location = new System.Drawing.Point(236, 276);
this.numericUpDown1.Name = "numericUpDown1";
this.numericUpDown1.Size = new System.Drawing.Size(120, 22);
this.numericUpDown1.TabIndex = 6;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(50, 278);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(180, 16);
this.label2.TabIndex = 7;
this.label2.Text = "Продолжительность в мин";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(55, 239);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(144, 16);
this.label3.TabIndex = 8;
this.label3.Text = "Дата и время начала";
//
// button2
//
this.button2.Location = new System.Drawing.Point(53, 321);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 9;
this.button2.Text = "Начать";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// txtVremya
//
this.txtVremya.AutoSize = true;
this.txtVremya.Location = new System.Drawing.Point(481, 120);
this.txtVremya.Name = "txtVremya";
this.txtVremya.Size = new System.Drawing.Size(44, 16);
this.txtVremya.TabIndex = 10;
this.txtVremya.Text = "label4";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(484, 164);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(129, 16);
this.label4.TabIndex = 11;
this.label4.Text = "Осталось времени";
//
// lblStatus
//
this.lblStatus.AutoSize = true;
this.lblStatus.Location = new System.Drawing.Point(629, 164);
this.lblStatus.Name = "lblStatus";
this.lblStatus.Size = new System.Drawing.Size(44, 16);
this.lblStatus.TabIndex = 12;
this.lblStatus.Text = "label5";
//
// lstResults
//
this.lstResults.FormattingEnabled = true;
this.lstResults.ItemHeight = 16;
this.lstResults.Location = new System.Drawing.Point(487, 210);
this.lstResults.Name = "lstResults";
this.lstResults.Size = new System.Drawing.Size(120, 84);
this.lstResults.TabIndex = 13;
//
// lblVopros
//
this.lblVopros.AutoSize = true;
this.lblVopros.Location = new System.Drawing.Point(484, 54);
this.lblVopros.Name = "lblVopros";
this.lblVopros.Size = new System.Drawing.Size(44, 16);
this.lblVopros.TabIndex = 14;
this.lblVopros.Text = "label5";
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(487, 321);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(44, 16);
this.label5.TabIndex = 15;
this.label5.Text = "label5";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.label5);
this.Controls.Add(this.lblVopros);
this.Controls.Add(this.lstResults);
this.Controls.Add(this.lblStatus);
this.Controls.Add(this.label4);
this.Controls.Add(this.txtVremya);
this.Controls.Add(this.button2);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.numericUpDown1);
this.Controls.Add(this.dateTimePicker1);
this.Controls.Add(this.button1);
this.Controls.Add(this.labl2);
this.Controls.Add(this.txtOtveti);
this.Controls.Add(this.label1);
this.Controls.Add(this.txtVopros);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox txtVopros;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txtOtveti;
private System.Windows.Forms.Label labl2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.DateTimePicker dateTimePicker1;
private System.Windows.Forms.NumericUpDown numericUpDown1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label txtVremya;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label lblStatus;
private System.Windows.Forms.ListBox lstResults;
private System.Windows.Forms.Label lblVopros;
private System.Windows.Forms.Label label5;
}
}
Код для VoteVariant.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AnyaTask7
{
public class VoteVariant
{
public string Text;
public int KolvoProgolos;
public override string ToString()
{
return $"{Text} - {KolvoProgolos}";
}
}
}