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

148 lines
5.4 KiB
C#
Raw Normal View History

using Components;
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 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;
2024-10-07 14:42:26 +04:00
using (SaveFileDialog saveFileDialog = new SaveFileDialog())
{
2024-10-07 14:42:26 +04:00
saveFileDialog.InitialDirectory = "d:\\tmp";
saveFileDialog.Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*";
saveFileDialog.FilterIndex = 1;
saveFileDialog.RestoreDirectory = true;
2024-10-07 14:42:26 +04:00
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
2024-10-07 14:42:26 +04:00
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;
}
}
}