Learning/Mysql-example/UserEditForm.cs

184 lines
8.4 KiB
C#
Raw Normal View History

2023-05-05 18:58:11 +00:00
using Mysql_example.Database.Schema;
using Mysql_example.Util;
using System.ComponentModel;
using System.Globalization;
namespace Mysql_example
{
public partial class UserEditForm : Form
{
User user;
User user_copy;
public delegate void SaveUserDelegate(ref User user);
public SaveUserDelegate SaveUserCallback;
private DataGridViewCellEventArgs mouseLocation;
private bool isEmailValid = true;
public UserEditForm(ref User _user)
{
this.user = _user;
this.user_copy = new User(user);
InitializeComponent();
BindUserData();
BindAddressData();
this.Text = user.FirstName + " " + user.LastName;
}
private void BindUserData()
{
TextBoxFirstName.DataBindings.Clear();
TextBoxFirstName.DataBindings.Add(nameof(TextBoxFirstName.Text), user_copy, nameof(user_copy.FirstName));
TextBoxLastName.DataBindings.Clear();
TextBoxLastName.DataBindings.Add(nameof(TextBoxLastName.Text), user_copy, nameof(user_copy.LastName));
TextBoxEmail.DataBindings.Clear();
TextBoxEmail.DataBindings.Add(nameof(TextBoxEmail.Text), user_copy, nameof(user_copy.Email));
DateTimePickerBirthday.DataBindings.Clear();
DateTimePickerBirthday.DataBindings.Add(nameof(DateTimePickerBirthday.Value), user_copy, nameof(user_copy.Birthday));
}
private void BindAddressData()
{
DataGridViewColumn oldColumn = DataGridViewAddresses.Columns["Country"];
if (oldColumn != null)DataGridViewAddresses.Columns.Remove(oldColumn);
DataGridViewAddresses.DataSource = Utility.addressesToBindingList(user_copy.Addresses);
(DataGridViewAddresses.DataSource as BindingList<Address>).ListChanged += UserEditForm_ListChanged;
DataGridViewAddresses.Columns[nameof(Address.Region)].Visible= false;
DataGridViewAddresses.Columns[nameof(Address.RegionCode)].Visible = false;
DataGridViewComboBoxColumn countryColumn= new DataGridViewComboBoxColumn();
countryColumn.HeaderText = "Country";
countryColumn.Name= "Country";
countryColumn.DataSource = Address.regionInfos;
countryColumn.DisplayMember = nameof(RegionInfo.EnglishName);
countryColumn.DataPropertyName = nameof(Address.Region);
countryColumn.ValueMember = nameof(RegionInfo.Name);
DataGridViewAddresses.Columns.Add(countryColumn);
}
private void UserEditForm_ListChanged(object? sender, ListChangedEventArgs e)
{
BindingList<Address> bindingList = sender as BindingList<Address>;
if (e.ListChangedType == ListChangedType.ItemAdded && bindingList.Count > user_copy.Addresses.Values.Count)
{
Address newAddress = bindingList.Last();
user_copy.Addresses[newAddress.Id] = newAddress;
}
if (e.ListChangedType == ListChangedType.ItemDeleted && bindingList.Count < user_copy.Addresses.Values.Count)
{
Address oldAddress = user_copy.Addresses.Values.ToList()[e.NewIndex];
user_copy.Addresses.Remove(oldAddress.Id);
user_copy.deletedAddreses.Add(oldAddress);
if (user_copy.BillingAdress != null && user_copy.BillingAdress.Id == oldAddress.Id) user_copy.BillingAdress = null;
if (user_copy.BillingAdress != null && user_copy.ShippingAddress.Id == oldAddress.Id) user_copy.ShippingAddress = null;
}
}
private void DataGridViewAddresses_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
DataGridViewRow dataRow = DataGridViewAddresses.Rows[e.RowIndex];
Address address = dataRow.DataBoundItem as Address;
if(address == null) return;
string matches = "";
if (user_copy.BillingAdress != null && user_copy.BillingAdress.Id == address.Id) matches += "billing";
if (user_copy.ShippingAddress != null && user_copy.ShippingAddress.Id == address.Id) matches += "shipping";
switch (matches)
{
case "billing":
dataRow.DefaultCellStyle.BackColor = Color.Green;
break;
case "shipping":
dataRow.DefaultCellStyle.BackColor = Color.Yellow;
break;
case "billingshipping":
dataRow.DefaultCellStyle.BackColor = Color.Blue;
break;
default:
dataRow.DefaultCellStyle.BackColor = base.BackColor;
break;
}
}
private void CancelButton_Click(object sender, EventArgs e)
{
this.Close();
}
private void RevertButton_Click(object sender, EventArgs e)
{
DialogResult dialogResult = MessageBox.Show("You will loose all your Changes", "revert Changes", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.No) return;
user_copy = new User(user);
BindUserData();
BindAddressData();
}
private void SaveButton_Click(object sender, EventArgs e)
{
if (!isEmailValid)
{
MessageBox.Show("Can't Save Email is Invalid");
return;
}
SaveUserCallback(ref user_copy);
Close();
}
// Update Copy of User Callbacks
private void DataGridViewAddresses_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
{
mouseLocation = e;
}
private void ToolStripMenuItemMakeDefaultBilling_Click(object sender, EventArgs e)
{
if (DataGridViewAddresses.Rows.Count - 1 == mouseLocation.RowIndex) return;
DataGridViewRow row = DataGridViewAddresses.Rows[mouseLocation.RowIndex];
Address address = row.DataBoundItem as Address;
user_copy.BillingAdress = address;
DataGridViewAddresses.Refresh();
}
private void ToolStripMenuItemMakeDefaultShipping_Click(object sender, EventArgs e)
{
if (DataGridViewAddresses.Rows.Count - 1 == mouseLocation.RowIndex) return;
DataGridViewRow row = DataGridViewAddresses.Rows[mouseLocation.RowIndex];
Address address = row.DataBoundItem as Address;
user_copy.ShippingAddress = address;
DataGridViewAddresses.Refresh();
}
private void ToolStripMenuItemDelete_Click(object sender, EventArgs e)
{
DialogResult dialogResult = MessageBox.Show("Delete Address?", "Address Deletion", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.No) return;
if (DataGridViewAddresses.Rows.Count - 1 == mouseLocation.RowIndex) return;
DataGridViewRow row = DataGridViewAddresses.Rows[mouseLocation.RowIndex];
Address address = row.DataBoundItem as Address;
(DataGridViewAddresses.DataSource as BindingList<Address>).Remove(address);
}
private void DataGridViewAddresses_CellClick(object sender, DataGridViewCellEventArgs e)
{
bool validClick = (e.RowIndex != -1 && e.ColumnIndex != -1);
if (validClick) DataGridViewAddresses.BeginEdit(true);
if (DataGridViewAddresses.Columns[e.ColumnIndex] is DataGridViewComboBoxColumn && validClick) ((ComboBox)DataGridViewAddresses.EditingControl).DroppedDown = true;
}
private void DataGridViewAddresses_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
DataGridViewAddresses.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
public void TextBoxEmail_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
if (Utility.IsValidEmail(TextBoxEmail.Text))
{
ErrorProviderUserEdit.SetError(TextBoxEmail, "");
isEmailValid = true;
return;
}
ErrorProviderUserEdit.SetError(TextBoxEmail, "Email is Invalid");
ErrorProviderUserEdit.SetIconPadding(TextBoxEmail, 3);
isEmailValid = false;
}
}
}