using ProjectKnapsack.classes; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using static ProjectKnapsack.classes.KnapsackParameters; namespace ProjectKnapsack.forms; public partial class MainForm : Form { private KnapsackVisualizer visualization; private KnapsackManager manager; private Storage storage; private int currentStep; private List items; // Добавление списка предметов public MainForm() { InitializeComponent(); visualization = new KnapsackVisualizer(); items = new List(); // Инициализация списка предметов } private void startButton_Click(object sender, EventArgs e) { var parametersForm = new ParametersForm(); if (parametersForm.ShowDialog() == DialogResult.OK) { var parameters = parametersForm.Parameters; if (parameters != null) // Добавьте проверку на null перед использованием параметров { manager = new KnapsackManager(parameters); manager.Execute(); storage = manager.Storage; currentStep = 0; items = parameters.Items.ToList(); // Инициализация списка предметов из параметров DisplayCurrentState(); } else { MessageBox.Show("Parameters are null.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } private void DisplayCurrentState() { //if (storage != null && visualization != null) //{ // var state = storage.GetState(currentStep); // if (state != null) // { // visualization.Visualize(state.Items, state.Capacity, this); // } // else // { // MessageBox.Show("State is null.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); // } //} if (storage != null && visualization != null) { var state = storage.GetState(currentStep); if (state != null) { visualization.Visualize(state.Items, state.Capacity, state.CurrentWeight, state.CurrentValue, this); } else { MessageBox.Show("State is null.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } private void saveMenuItem_Click(object sender, EventArgs e) { if (storage != null) { using (var saveFileDialog = new SaveFileDialog()) { if (saveFileDialog.ShowDialog() == DialogResult.OK) { storage.SaveToFile(saveFileDialog.FileName); } } } else { MessageBox.Show("No data to save.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void loadMenuItem_Click(object sender, EventArgs e) { using (var openFileDialog = new OpenFileDialog()) { if (openFileDialog.ShowDialog() == DialogResult.OK) { storage = new Storage(); storage.LoadFromFile(openFileDialog.FileName); currentStep = 0; DisplayCurrentState(); } } } private void aboutMenuItem_Click(object sender, EventArgs e) { InformationForm informationForm = new InformationForm(); informationForm.Show(); } private void nextStepButton_Click(object sender, EventArgs e) { //if (currentStep < storage.StateCount - 1) //{ // currentStep++; // DisplayCurrentState(); //} if (currentStep < storage.StateCount - 1) { currentStep++; var state = storage.GetState(currentStep); if (state != null) { if (state.Items.Count > currentStep) { var addedItem = state.Items[currentStep]; items.Remove(addedItem); // Удаление добавленного предмета из списка предметов DisplayCurrentState(); } else { MessageBox.Show("Следующего шага нет"); return; } } } } private void prevStepButton_Click(object sender, EventArgs e) { //if (currentStep > 0) //{ // currentStep--; // DisplayCurrentState(); //} if (currentStep > 0) { currentStep--; var state = storage.GetState(currentStep); if (state != null) { var addedItem = state.Items[currentStep]; items.Add(addedItem); // Добавление предмета обратно в список предметов DisplayCurrentState(); } } } }