using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace InventoryTest { public partial class CategoryManager : Form { private static string ConnectionString { get; set; } private static Dictionary CategoryIds { get; } = new(); private static Dictionary SubCategoryIds { get; } = new(); public CategoryManager(string connectionString) { ConnectionString = connectionString; InitializeComponent(); CategoryComboBoxManager.SelectedIndexChanged += CategoryComboBoxManager_SelectedIndexChanged; SubCategoryComboBoxManager.SelectedIndexChanged += SubCategoryComboBoxManager_SelectedIndexChanged; CategoryPropertyNameTextBox.TextChanged += CategoryPropertyNameTextBox_TextChanged; SubCategoryPropertyNameTextBox.TextChanged += SubCategoryNameTextBox_TextChanged; NewCategoryNameTextBox.TextChanged += NewCategoryNameTextBox_TextChanged; CategoryPropertiesListBox.SelectedIndexChanged += CategoryPropertiesListBox_SelectedIndexChanged; SubCategoryPropertiesListBox.SelectedIndexChanged += SubCategoryPropertiesListBox_SelectedIndexChanged; LoadCategories(); } private void SubCategoryPropertiesListBox_SelectedIndexChanged(object sender, EventArgs e) { DeleteSubCategoryPropertyButton.Enabled = SubCategoryPropertiesListBox.SelectedIndex >= 0; } private void CategoryPropertiesListBox_SelectedIndexChanged(object sender, EventArgs e) { DeleteCategoryPropertyButton.Enabled = CategoryPropertiesListBox.SelectedIndex >= 0; } private void NewCategoryNameTextBox_TextChanged(object sender, EventArgs e) { CreateNewCategoryButton.Enabled = !string.IsNullOrEmpty(NewCategoryNameTextBox.Text); CreateNewSubCategoryButton.Enabled = !string.IsNullOrEmpty(NewCategoryNameTextBox.Text); } private void SubCategoryNameTextBox_TextChanged(object sender, EventArgs e) { CreateSubCategoryButton.Enabled = !string.IsNullOrEmpty(SubCategoryPropertyNameTextBox.Text); } private void CategoryPropertyNameTextBox_TextChanged(object sender, EventArgs e) { CreateCategoryPropertyButton.Enabled = !string.IsNullOrEmpty(CategoryPropertyNameTextBox.Text); } private void SubCategoryComboBoxManager_SelectedIndexChanged(object sender, EventArgs e) { LoadSubCategoryProperties(); } private void CategoryComboBoxManager_SelectedIndexChanged(object sender, EventArgs e) { SubCategoryComboBoxManager.Items.Clear(); CategoryPropertiesListBox.Items.Clear(); SubCategoryPropertiesListBox.Items.Clear(); DeleteCategoryPropertyButton.Enabled = false; DeleteSubCategoryPropertyButton.Enabled = false; var id = CategoryIds[CategoryComboBoxManager.Text]; SubCategoryIds.Clear(); using var connection = new SqlConnection(ConnectionString); using var command = new SqlCommand("SELECT * FROM dbo.GetCategorySubCategories(@ID)", connection); command.Parameters.AddWithValue("ID", id); connection.Open(); var reader = command.ExecuteReader(); while (reader.Read()) { SubCategoryComboBoxManager.Items.Add(reader["Name"]); SubCategoryIds.Add(reader["Name"].ToString(), Convert.ToInt32(reader["ID"])); } reader.Close(); if (SubCategoryComboBoxManager.Items.Count > 0) { SubCategoryComboBoxManager.Enabled = true; SubCategoryComboBoxManager.SelectedIndex = 0; ToggleSubCategoryProperty(true); } else { ToggleSubCategoryProperty(false); SubCategoryComboBoxManager.Enabled = false; } LoadCategoryProperties(); } private void LoadCategories() { CategoryComboBoxManager.Items.Clear(); SubCategoryComboBoxManager.Items.Clear(); CategoryIds.Clear(); using var connection = new SqlConnection(ConnectionString); using var command = new SqlCommand("SELECT [Category ID], [Category Name] FROM Category", connection); connection.Open(); var reader = command.ExecuteReader(); while (reader.Read()) { CategoryIds.Add(reader["Category Name"].ToString(), Convert.ToInt32(reader["Category ID"])); CategoryComboBoxManager.Items.Add(reader["Category Name"]); } reader.Close(); if (CategoryComboBoxManager.Items.Count > 0) { ToggleCategoryProperty(true); CategoryComboBoxManager.SelectedIndex = 0; } else ToggleCategoryProperty(false); } private void LoadCategoryProperties() { CategoryPropertiesListBox.Items.Clear(); using var connection = new SqlConnection(ConnectionString); using var command = new SqlCommand("SELECT * FROM GetCategoryProperties(@CategoryID)", connection); command.Parameters.AddWithValue("CategoryID", CategoryIds[CategoryComboBoxManager.Text]); connection.Open(); var reader = command.ExecuteReader(); while (reader.Read()) CategoryPropertiesListBox.Items.Add(reader["Name"]); } private void LoadSubCategoryProperties() { SubCategoryPropertiesListBox.Items.Clear(); DeleteSubCategoryPropertyButton.Enabled = false; using var connection = new SqlConnection(ConnectionString); using var command = new SqlCommand("SELECT * FROM GetSubCategoryProperties(@ID)", connection); command.Parameters.AddWithValue("ID", SubCategoryIds[SubCategoryComboBoxManager.Text]); connection.Open(); var reader = command.ExecuteReader(); while (reader.Read()) SubCategoryPropertiesListBox.Items.Add(reader["Name"]); } private void ToggleSubCategoryProperty(bool enabled) { SubCategoryPropertyNameTextBox.Enabled = enabled; DeleteSubCategoryButton.Enabled = enabled; } private void ToggleCategoryProperty(bool enabled) { CategoryPropertyNameTextBox.Enabled = enabled; } private void CreateCategoryPropertyButton_Click(object sender, EventArgs e) { using var connection = new SqlConnection(ConnectionString); using var command = new SqlCommand("CreateCategoryPropertyByName", connection) { CommandType = CommandType.StoredProcedure }; command.Parameters.AddWithValue("PropertyName", CategoryPropertyNameTextBox.Text); command.Parameters.AddWithValue("CategoryID", CategoryIds[CategoryComboBoxManager.Text]); connection.Open(); command.ExecuteNonQuery(); LoadCategoryProperties(); CategoryPropertyNameTextBox.Text = string.Empty; CategoryPropertyNameTextBox.Focus(); } private void CreateSubCategoryButton_Click(object sender, EventArgs e) { using var connection = new SqlConnection(ConnectionString); using var command = new SqlCommand("CreateSubCategoryPropertyByName", connection) { CommandType = CommandType.StoredProcedure }; command.Parameters.AddWithValue("PropertyName", SubCategoryPropertyNameTextBox.Text); command.Parameters.AddWithValue("SubCategoryID", SubCategoryIds[SubCategoryComboBoxManager.Text]); connection.Open(); command.ExecuteNonQuery(); LoadSubCategoryProperties(); SubCategoryPropertyNameTextBox.Text = string.Empty; SubCategoryPropertyNameTextBox.Focus(); } private void CreateNewCategoryButton_Click(object sender, EventArgs e) { using var connection = new SqlConnection(ConnectionString); using var command = new SqlCommand("CreateCategory", connection) { CommandType = CommandType.StoredProcedure }; command.Parameters.AddWithValue("Category", NewCategoryNameTextBox.Text); connection.Open(); CategoryIds.Add(NewCategoryNameTextBox.Text, Convert.ToInt32(command.ExecuteScalar())); CategoryComboBoxManager.Items.Add(NewCategoryNameTextBox.Text); NewCategoryNameTextBox.Text = string.Empty; NewCategoryNameTextBox.Focus(); if (CategoryComboBoxManager.SelectedIndex == -1) CategoryComboBoxManager.SelectedIndex = 0; } private void CreateNewSubCategoryButton_Click(object sender, EventArgs e) { using var connection = new SqlConnection(ConnectionString); using var command = new SqlCommand("CreateSubCategory", connection) { CommandType = CommandType.StoredProcedure }; command.Parameters.AddWithValue("SubCategory", NewCategoryNameTextBox.Text); command.Parameters.AddWithValue("CategoryID", CategoryIds[CategoryComboBoxManager.Text]); connection.Open(); SubCategoryIds.Add(NewCategoryNameTextBox.Text, Convert.ToInt32(command.ExecuteScalar())); SubCategoryComboBoxManager.Items.Add(NewCategoryNameTextBox.Text); NewCategoryNameTextBox.Text = string.Empty; NewCategoryNameTextBox.Focus(); SubCategoryComboBoxManager.Enabled = true; if (SubCategoryComboBoxManager.SelectedIndex == -1) SubCategoryComboBoxManager.SelectedIndex = 0; SubCategoryPropertyNameTextBox.Enabled = true; DeleteSubCategoryButton.Enabled = true; } private void DeleteCategoryButton_Click(object sender, EventArgs e) { using var connection = new SqlConnection(ConnectionString); using var command = new SqlCommand("DeleteCategory", connection) { CommandType = CommandType.StoredProcedure }; command.Parameters.AddWithValue("CategoryID", CategoryIds[CategoryComboBoxManager.Text]); connection.Open(); command.ExecuteNonQuery(); LoadCategories(); } private void DeleteSubCategoryButton_Click(object sender, EventArgs e) { using var connection = new SqlConnection(ConnectionString); using var command = new SqlCommand("DeleteSubCategory", connection) { CommandType = CommandType.StoredProcedure }; command.Parameters.AddWithValue("SubCategoryID", SubCategoryIds[SubCategoryComboBoxManager.Text]); connection.Open(); command.ExecuteNonQuery(); SubCategoryIds.Remove(SubCategoryComboBoxManager.Text); SubCategoryComboBoxManager.Items.RemoveAt(SubCategoryComboBoxManager.SelectedIndex); } private void DeleteCategoryPropertyButton_Click(object sender, EventArgs e) { } } }