150 lines
5.9 KiB
C#
150 lines
5.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Data.Entity.Infrastructure;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace AdvertsingProfitControl
|
|
{
|
|
public partial class FrmRegisterAdSpecial : Form
|
|
{
|
|
private readonly ToolTip _tt = new ToolTip();
|
|
private bool _isUpdating;
|
|
|
|
public FrmRegisterAdSpecial()
|
|
{
|
|
InitializeComponent();
|
|
newAdSpecialTextBox.TextChanged += NewAdSpecialTextChanged;
|
|
var columnHeader = new ColumnHeader
|
|
{
|
|
Name = "AdSpecialsHeaderColumn",
|
|
Text = @"Current Ad Special Grouping Keywords:",
|
|
Width = adSpecialsListBox.Width - 5
|
|
};
|
|
adSpecialsListBox.Columns.Add(columnHeader);
|
|
adSpecialsListBox.MouseMove += OnMouseMove;
|
|
adSpecialsListBox.ItemSelectionChanged += UpdateAdSpecialTextBoxes;
|
|
RefreshAdSpecialListing();
|
|
}
|
|
|
|
private void UpdateAdSpecialTextBoxes(object sender, ListViewItemSelectionChangedEventArgs e)
|
|
{
|
|
newAdSpecialTextBox.Text = ((AdSpecial) e.Item.Tag).Name;
|
|
descriptionTextBox.Text = ((AdSpecial) e.Item.Tag).Description;
|
|
_isUpdating = true;
|
|
deleteAdSpecialButton.Enabled = true;
|
|
registerAdSpecialButton.Text = @"Update Ad Special";
|
|
}
|
|
|
|
private void NewAdSpecialTextChanged(object sender, EventArgs e)
|
|
{
|
|
if (_isUpdating && newAdSpecialTextBox.Text.Length == 0)
|
|
{
|
|
_isUpdating = false;
|
|
descriptionTextBox.Text = string.Empty;
|
|
registerAdSpecialButton.Text = @"Register Ad Special";
|
|
}
|
|
registerAdSpecialButton.Enabled = newAdSpecialTextBox.Text.Length > 0;
|
|
}
|
|
|
|
private void RegisterNewAdSpecial(object sender, EventArgs e)
|
|
{
|
|
var db = new AdvertisingProfitControlModel();
|
|
if (!_isUpdating)
|
|
{
|
|
if (!db.AdSpecials.Any(x => x.Name == newAdSpecialTextBox.Text))
|
|
{
|
|
var adSpecial = new AdSpecial
|
|
{
|
|
Name = newAdSpecialTextBox.Text,
|
|
Description = descriptionTextBox.Text
|
|
};
|
|
db.AdSpecials.Add(adSpecial);
|
|
try
|
|
{
|
|
db.SaveChanges();
|
|
informationLabel.Text = @"Registered '" + Environment.NewLine + adSpecial.Name + Environment.NewLine + @"' successfully.";
|
|
}
|
|
catch (DbUpdateException ex)
|
|
{
|
|
informationLabel.Text = @"Failed to register '" + Environment.NewLine + adSpecial.Name + @"'." + Environment.NewLine +
|
|
ex.Message;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show(@"Ad Special '" + newAdSpecialTextBox.Text + @"' already exists.",
|
|
@"Duplicate Entry", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
|
newAdSpecialTextBox.Text = string.Empty;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
var item = adSpecialsListBox.SelectedItems[0];
|
|
var index = ((AdSpecial) item.Tag).Id;
|
|
var adSpecial = db.AdSpecials.SingleOrDefault(x => x.Id == index);
|
|
if (adSpecial == null)
|
|
{
|
|
informationLabel.Text = @"Failed to retrieve " + Environment.NewLine + @"ad special keyword ID.";
|
|
return;
|
|
}
|
|
adSpecial.Name = newAdSpecialTextBox.Text;
|
|
adSpecial.Description = descriptionTextBox.Text;
|
|
db.SaveChanges();
|
|
informationLabel.Text = @"Updated '" + Environment.NewLine + newAdSpecialTextBox.Text + Environment.NewLine + @"' successfully.";
|
|
}
|
|
RefreshAdSpecialListing();
|
|
}
|
|
|
|
private void OnMouseMove(object sender, MouseEventArgs e)
|
|
{
|
|
var box = sender as ListBox;
|
|
if (box == null) return;
|
|
var listBox = box;
|
|
var point = new Point(e.X, e.Y);
|
|
var hoverIndex = listBox.IndexFromPoint(point);
|
|
if (hoverIndex >= 0 && hoverIndex < listBox.Items.Count)
|
|
{
|
|
// _tt.SetToolTip(listBox, _descriptions[hoverIndex]);
|
|
}
|
|
}
|
|
|
|
private void RefreshAdSpecialListing()
|
|
{
|
|
adSpecialsListBox.Items.Clear();
|
|
var db = new AdvertisingProfitControlModel();
|
|
var adSpecials = db.AdSpecials.Select(x => x);
|
|
foreach (var adSpecial in adSpecials)
|
|
{
|
|
var item = new ListViewItem
|
|
{
|
|
Text = adSpecial.Name,
|
|
Tag = adSpecial
|
|
};
|
|
adSpecialsListBox.Items.Add(item);
|
|
}
|
|
deleteAdSpecialButton.Enabled = false;
|
|
newAdSpecialTextBox.Text = string.Empty;
|
|
descriptionTextBox.Text = string.Empty;
|
|
}
|
|
|
|
private void deleteAdSpecialButton_Click(object sender, EventArgs e)
|
|
{
|
|
//TODO: Scan for Ad Specials that are in use by a table before removing.
|
|
var item = adSpecialsListBox.SelectedItems[0];
|
|
var adSpecial = (AdSpecial) item.Tag;
|
|
var db = new AdvertisingProfitControlModel();
|
|
var ad = db.AdSpecials.Single(x => x.Id == adSpecial.Id);
|
|
db.AdSpecials.Remove(ad);
|
|
db.SaveChanges();
|
|
RefreshAdSpecialListing();
|
|
informationLabel.Text = @"Removed '" + Environment.NewLine + adSpecial.Name + Environment.NewLine + @"' successfully.";
|
|
}
|
|
}
|
|
}
|