Код для Datchik.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AnyaTask9
{
public class Datchik
{
public string Name;
public List<double> Znachenia = new List<double>();
public override string ToString()
{
return Name;
}
}
}
Код для 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.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AnyaTask9
{
public partial class Form1 : Form
{
public List<Datchik> DatchikList = new List<Datchik>();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
string[] lines = File.ReadAllLines(ofd.FileName);
int kolvo = int.Parse(lines[0]);
int chastota = int.Parse(lines[1]);
int skip = 2;
int datchikLinesKolvo = (chastota + 1);
for (int i = 0; i < kolvo; i++)
{
Datchik datchik = new Datchik();
datchik.Name = lines[skip + i * datchikLinesKolvo];
for (int j = i * datchikLinesKolvo + 1; j < chastota + i * datchikLinesKolvo + 1; j++)
{
datchik.Znachenia.Add(double.Parse(lines[skip + j]));
}
DatchikList.Add(datchik);
}
string text = "";
foreach (string line in lines)
{
text += line + "\n";
}
label1.Text = text;
}
listBox1.Items.Clear();
listBox1.Items.AddRange(DatchikList.ToArray());
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedItem is Datchik datchik)
{
listBox2.Items.Clear();
string[] znacheniaStr = new string[datchik.Znachenia.Count];
for (int i = 0; i < znacheniaStr.Length; i++) {
znacheniaStr[i] = datchik.Znachenia[i].ToString();
}
listBox2.Items.AddRange(znacheniaStr);
}
}
private void button2_Click(object sender, EventArgs e)
{
if (listBox1.SelectedIndices.Count > 0)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
using (StreamWriter sw = new StreamWriter(saveFileDialog.FileName))
{
sw.WriteLine(listBox1.SelectedIndices.Count);
sw.WriteLine(DatchikList[0].Znachenia.Count);
foreach (int index in listBox1.SelectedIndices)
{
Datchik datchik = DatchikList[index];
sw.WriteLine(datchik.Name);
foreach (double znach in datchik.Znachenia)
{
sw.WriteLine(znach);
}
}
}
}
}
}
private void button3_Click(object sender, EventArgs e)
{
FolderBrowserDialog folderDialog = new FolderBrowserDialog();
if (folderDialog.ShowDialog() == DialogResult.OK)
{
foreach (Datchik datchik in DatchikList)
{
string datchikFile = Path.Combine(folderDialog.SelectedPath, datchik.Name + ".txt");
using (StreamWriter sw = new StreamWriter(datchikFile))
{
sw.WriteLine(datchik.Name);
sw.WriteLine(datchik.Znachenia.Count);
foreach (double znach in datchik.Znachenia)
{
sw.WriteLine(znach);
}
}
}
}
}
}
}
Код для Form1.Designer.cs
namespace AnyaTask9
{
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.button1 = new System.Windows.Forms.Button();
this.listBox1 = new System.Windows.Forms.ListBox();
this.listBox2 = new System.Windows.Forms.ListBox();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(24, 397);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "Загрузить";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// listBox1
//
this.listBox1.FormattingEnabled = true;
this.listBox1.Location = new System.Drawing.Point(24, 25);
this.listBox1.Name = "listBox1";
this.listBox1.SelectionMode = System.Windows.Forms.SelectionMode.MultiSimple;
this.listBox1.Size = new System.Drawing.Size(229, 355);
this.listBox1.TabIndex = 1;
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
//
// listBox2
//
this.listBox2.FormattingEnabled = true;
this.listBox2.Location = new System.Drawing.Point(259, 25);
this.listBox2.Name = "listBox2";
this.listBox2.Size = new System.Drawing.Size(224, 355);
this.listBox2.TabIndex = 2;
//
// button2
//
this.button2.Location = new System.Drawing.Point(115, 397);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(124, 23);
this.button2.TabIndex = 3;
this.button2.Text = "Сохранить датчик";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(259, 397);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(98, 23);
this.button3.TabIndex = 4;
this.button3.Text = "Сохранить все";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(502, 25);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(35, 13);
this.label1.TabIndex = 5;
this.label1.Text = "label1";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.label1);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.listBox2);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.ListBox listBox2;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Label label1;
}
}