Files

174 lines
7.5 KiB
C#

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 Form1 : Form
{
private string ConnectionString { get; } = "Server=DESKTOP-HJES64T;Database=Inventory;Integrated Security=true;";
private int CommentsMaxLength { get; } = 3072;
private int NotesMaxLength { get; } = 3072;
private Dictionary<string, int> CategoryIds { get; } = new();
private Dictionary<string, int> SubCategoryIds { get; } = new();
private Dictionary<string, int> PropertyTypeIds { get; } = new();
public Form1()
{
InitializeComponent();
ManageCategoriesToolStripMenuItem.Click += ShowCategoryManagerForm;
exitToolStripMenuItem.Click += ExitToolStripMenuItem_Click;
CorePropertyCommentTextBox.TextChanged += CorePropertyCommentTextBox_TextChanged;
CorePropertyCommentTextBox.MaxLength = CommentsMaxLength;
CorePropertyCommentGroupBox.Text = $"Comments (Remaing Characters: {CorePropertyCommentTextBox.MaxLength - CorePropertyCommentTextBox.TextLength})";
CorePropertyNotesTextBox.TextChanged += CorePropertyNotesTextBox_TextChanged;
CorePropertyNotesTextBox.MaxLength = NotesMaxLength;
CorePropertyNotesGroupBox.Text = $"Notes (Remaing Characters: {CorePropertyNotesTextBox.MaxLength - CorePropertyNotesTextBox.TextLength})";
CorePropertyNameTextBox.TextChanged += ToggleCreateItemButton;
CorePropertyBoughtForTextBox.TextChanged += ToggleCreateItemButton;
CorePropertySoldForTextBox.TextChanged += ToggleCreateItemButton;
CorePropertyValueTextBox.TextChanged += ToggleCreateItemButton;
CategorySelectionComboBox.SelectedIndexChanged += CategorySelectionComboBox_SelectedIndexChanged;
SubCategorySelectionComboBox.SelectedIndexChanged += SubCategorySelectionComboBox_SelectedIndexChanged;
LoadCategories();
}
private void SubCategorySelectionComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
//throw new NotImplementedException();
using var connection = new SqlConnection(ConnectionString);
using var command = new SqlCommand("SELECT [Name], [Property ID], [Sub Category Property ID], [Property Type ID], [Property Type] FROM dbo.GetSubCategoryProperties(@ID)", connection);
command.Parameters.AddWithValue("ID", SubCategoryIds[SubCategorySelectionComboBox.Text]);
connection.Open();
var reader = command.ExecuteReader();
while (reader.Read())
{
PropertiesFlowLayoutPanel.Controls.Add(new Label() { Text = reader["Name"].ToString() + ":" });
PropertiesFlowLayoutPanel.Controls.Add(new TextBox() { Name = reader["Property ID"].ToString() });
}
}
private void CategorySelectionComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
SubCategoryIds.Clear();
SubCategorySelectionComboBox.Items.Clear();
ItemTreeView.Nodes.Clear();
PropertiesFlowLayoutPanel.Controls.Clear();
using var connection = new SqlConnection(ConnectionString);
using var command = new SqlCommand("SELECT ID, Name FROM dbo.GetCategorySubCategories(@ID)", connection);
command.Parameters.AddWithValue("ID", CategoryIds[CategorySelectionComboBox.Text]);
connection.Open();
var reader = command.ExecuteReader();
while (reader.Read())
{
SubCategoryIds.Add(reader["Name"].ToString(), Convert.ToInt32(reader["ID"]));
SubCategorySelectionComboBox.Items.Add(reader["Name"].ToString());
}
if (SubCategorySelectionComboBox.Items.Count > 0)
{
SubCategorySelectionComboBox.Enabled = true;
SubCategorySelectionComboBox.SelectedIndex = 0;
}
else SubCategorySelectionComboBox.Enabled = false;
reader.Close();
command.CommandText = "SELECT [Name], [Property ID], [Category Property ID], [Property Type ID], [Property Type] FROM dbo.GetCategoryProperties(@ID)";
command.CommandType = CommandType.Text;
reader = command.ExecuteReader();
while (reader.Read())
{
PropertiesFlowLayoutPanel.Controls.Add(new Label() { Text = reader["Name"].ToString() + ":"});
PropertiesFlowLayoutPanel.Controls.Add(new TextBox() { Name = reader["Property ID"].ToString() });
}
}
private void LoadCategories()
{
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"]));
CategorySelectionComboBox.Items.Add(reader["Category Name"]);
}
reader.Close();
if (CategorySelectionComboBox.Items.Count > 0)
{
CategorySelectionComboBox.SelectedIndex = 0;
CorePropertyNameTextBox.Enabled = true;
}
else CorePropertyNameTextBox.Enabled = false;
}
private void ToggleCreateItemButton(object sender, EventArgs e)
{
var valueValid = CorePropertyValueTextBox.Text.Trim().Length == 0;
var boughtForValid = CorePropertyBoughtForTextBox.Text.Trim().Length == 0;
var soldForValid = CorePropertySoldForTextBox.Text.Trim().Length == 0;
if (!valueValid) valueValid = decimal.TryParse(CorePropertyValueTextBox.Text, out decimal _);
if (!boughtForValid) boughtForValid = decimal.TryParse(CorePropertyBoughtForTextBox.Text, out decimal _);
if (!soldForValid) soldForValid = decimal.TryParse(CorePropertySoldForTextBox.Text, out decimal _);
CreateItemButton.Enabled = CorePropertyNameTextBox.Text.Trim().Length > 0 && valueValid && boughtForValid && soldForValid;
}
private void CorePropertyNotesTextBox_TextChanged(object sender, EventArgs e)
{
CorePropertyNotesGroupBox.Text = $"Notes (Remaing Characters: {CorePropertyNotesTextBox.MaxLength - CorePropertyNotesTextBox.TextLength})";
}
private void CorePropertyCommentTextBox_TextChanged(object sender, EventArgs e)
{
CorePropertyCommentGroupBox.Text = $"Comments (Remaing Characters: {CorePropertyCommentTextBox.MaxLength - CorePropertyCommentTextBox.TextLength})";
}
private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
{
Close();
}
private void ShowCategoryManagerForm(object sender, EventArgs e)
{
var form = new CategoryManager(ConnectionString);
form.ShowDialog();
}
private struct ItemPropertyPairing
{
public int PropertyID;
public int ItemID;
public int PropertyTypeID;
public string Value;
}
}
}