PIbd-31_PotapovNS_COP_20/WinFormSolution/WinFormsApp/FormNoVisual.cs

225 lines
8.3 KiB
C#

using Components;
using System.Collections.Generic;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using LegendPosition = Components.ComponentExcelWithPieDiagram.LegendPosition;
namespace WinFormsApp
{
public partial class FormNoVisual : Form
{
private List<ComponentExcelWithPieDiagram.ChartData> _chartData;
private class TestData
{
public string Name { get; set; }
public int Age { get; set; }
public string City { get; set; }
}
private List<TableEmployee> _employees = new List<TableEmployee>();
private double _chartDataValuesSum
{
get
{
return _chartData.Sum(x => x.SeriesValue);
}
}
public FormNoVisual()
{
InitializeComponent();
_chartData = new List<ComponentExcelWithPieDiagram.ChartData>();
comboBoxLegendPosition.DataSource = Enum.GetValues(typeof(LegendPosition));
UpdateNumericUpDownSeriesValueMaximumValue();
}
private void buttonCreateExcelFile_Click(object sender, EventArgs e)
{
string[] list = new string[listBoxImages.Items.Count];
for (int i = 0; i < listBoxImages.Items.Count; i++)
{
list[i] = listBoxImages.Items[i].ToString();
}
try
{
componentExcelWithImage.CreateExcelWithImages(textBoxFilePath.Text, textBoxTitle.Text, list);
MessageBox.Show("Файл успешно создан", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show($"Ошибка при создании файла:\n{ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void buttonSetFilePath_Click(object sender, EventArgs e)
{
var filePath = string.Empty;
using (SaveFileDialog saveFileDialog = new SaveFileDialog())
{
saveFileDialog.InitialDirectory = "d:\\tmp";
saveFileDialog.Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*";
saveFileDialog.FilterIndex = 1;
saveFileDialog.RestoreDirectory = true;
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
filePath = saveFileDialog.FileName;
}
}
if (!string.IsNullOrEmpty(filePath))
{
textBoxFilePath.Text = filePath;
}
else
{
textBoxFilePath.Text = string.Empty;
}
}
private void buttonAddImage_Click(object sender, EventArgs e)
{
var filePath = string.Empty;
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
openFileDialog.InitialDirectory = "d:\\tmp";
openFileDialog.Filter = "Image files (*.jpeg;*.jpg;*.png)|*.jpeg;*.jpg;*.png|All files (*.*)|*.*";
openFileDialog.FilterIndex = 1;
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
filePath = openFileDialog.FileName;
}
}
if (!string.IsNullOrEmpty(filePath))
{
listBoxImages.Items.Add(filePath);
}
}
private void buttonClearImages_Click(object sender, EventArgs e)
{
listBoxImages.Items.Clear();
}
private void buttonAddSeries_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxSeriesName.Text) || numericUpDownSeriesValue.Value == 0)
{
return;
}
ComponentExcelWithPieDiagram.ChartData chartData = new();
chartData.SeriesName = textBoxSeriesName.Text;
chartData.SeriesValue = (double)numericUpDownSeriesValue.Value;
_chartData.Add(chartData);
textBoxSeriesName.Text = string.Empty;
UpdateDataGridViewChartData();
UpdateNumericUpDownSeriesValueMaximumValue();
}
private void buttonClearSeries_Click(object sender, EventArgs e)
{
_chartData.Clear();
dataGridViewSeries.DataSource = null;
}
private void buttonCreateExcelWithPieDiagram_Click(object sender, EventArgs e)
{
LegendPosition legendPosition;
Enum.TryParse<LegendPosition>(comboBoxLegendPosition.SelectedValue.ToString(), out legendPosition);
try
{
componentExcelWithPieDiagram.CreateExcelWithPieChart(textBoxFilePath.Text, textBoxTitle.Text, textBoxDiagramTitle.Text, legendPosition, _chartData);
MessageBox.Show("Файл успешно создан", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show($"Ошибка при создании файла:\n{ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void UpdateDataGridViewChartData()
{
dataGridViewSeries.DataSource = null;
dataGridViewSeries.DataSource = _chartData;
}
private void UpdateNumericUpDownSeriesValueMaximumValue()
{
numericUpDownSeriesValue.Maximum = (decimal)(100 - _chartDataValuesSum);
numericUpDownSeriesValue.Value = numericUpDownSeriesValue.Maximum;
}
private void buttonAddEmployee_Click(object sender, EventArgs e)
{
double salary;
if (double.TryParse(textBoxSalary.Text.Replace(".", ","), out salary))
{
_employees.Add(new TableEmployee(
_employees.Count,
textBoxName.Text,
textBoxSurname.Text,
(int)numericUpDownAge.Value,
textBoxWorkPlace.Text,
textBoxPost.Text,
salary
));
UpdateTableEmployees();
}
}
private void buttonClearEmployes_Click(object sender, EventArgs e)
{
_employees.Clear();
UpdateTableEmployees();
}
private void buttonGenerateExcelWithTable_Click(object sender, EventArgs e)
{
List<(int StartRow, int EndRow, int StartCol, int EndCol, string title)> mergeCellsInfo = new List<(int StartRow, int EndRow, int StartCol, int EndCol, string title)>
{
(2, 3, 1, 1, "Личные данные"),
(5, 6, 1, 1, "Работа")
};
List<(string title, string propertyName, float height)> headersConfig = new List<(string title, string propertyName, float height)>
{
("ID", "ID", 20),
("Фамилия", "SurName", 20),
("Имя", "Name", 20),
("Возраст", "Age", 20),
("Место работы", "WorkPlace", 40),
("Должность", "WorkPost", 40),
("Зарплата", "Salary", 30)
};
try
{
componentExcelTableWithColumnHeader.GenerateExcelFile<TableEmployee>(
textBoxFilePath.Text,
textBoxTitle.Text,
mergeCellsInfo,
_employees,
headersConfig
);
MessageBox.Show("Файл успешно создан", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show($"Ошибка при создании файла:\n{ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void UpdateTableEmployees()
{
dataGridViewEmployees.DataSource = null;
dataGridViewEmployees.DataSource = _employees;
}
}
}