void SelectEnchantment(short id)
        {
            boxEnchantments.ItemSelectionChanged -= BoxEnchantmentsItemSelectionChanged;
            editId.ValueChanged          -= EditIdValueChanged;
            boxName.SelectedIndexChanged -= BoxNameSelectedIndexChanged;
            editLevel.ValueChanged       -= EditLevelValueChanged;

            Data.Enchantment enchantment = null;
            if (Data.enchantments.ContainsKey(id))
            {
                enchantment = Data.enchantments[id];
            }

            NbtTag tag = null;

            if (enchantments.ContainsKey(id))
            {
                tag = enchantments[id].Item1;
            }

            boxEnchantments.SelectedItems.Clear();
            if (enchantments.ContainsKey(id))
            {
                enchantments[id].Item2.Selected = true;
            }
            editId.Value = id; editId.Text = id.ToString();
            if (lastEnchantment != null)
            {
                boxName.Items.Remove(lastEnchantment);
            }
            if (enchantment == null)
            {
                lastEnchantment       = new Data.Enchantment(id, "Unknown enchantment " + id, 0, new List <short>());
                boxName.SelectedIndex = boxName.Items.Add(lastEnchantment);
            }
            else if (!boxName.Items.Contains(enchantment))
            {
                boxName.SelectedIndex = boxName.Items.Add(enchantment);
            }
            else
            {
                boxName.Text = enchantment.name;
            }

            editLevel.Enabled = boxAllow.Checked || (enchantment != null && enchantment.items.Contains(slot.Item.ID) &&
                                                     (tag == null || (short)tag["lvl"] >= 0 && (short)tag["lvl"] <= enchantment.max));
            bool allow = boxAllow.Checked || enchantment == null || (tag != null && ((short)tag["lvl"] < 0 || (short)tag["lvl"] > enchantment.max));

            editLevel.Minimum = allow ? -32768 : 0;
            editLevel.Maximum = allow ? (short)32767 : enchantment.max;
            editLevel.Value   = (tag != null) ? (short)tag["lvl"] : (short)0;

            boxEnchantments.ItemSelectionChanged += BoxEnchantmentsItemSelectionChanged;
            editId.ValueChanged          += EditIdValueChanged;
            boxName.SelectedIndexChanged += BoxNameSelectedIndexChanged;
            editLevel.ValueChanged       += EditLevelValueChanged;
        }
        void BoxAllowCheckedChanged(object sender, EventArgs e)
        {
            if (slot == null || slot.Item == null)
            {
                return;
            }

            Data.Enchantment enchantment = null;
            if (Data.enchantments.ContainsKey((short)editId.Value))
            {
                enchantment = Data.enchantments[(short)editId.Value];
            }

            editId.Enabled = boxAllow.Checked;

            if (enchantment == null && editLevel.Value == 0)
            {
                DeselectEnchantment();
                return;
            }

            boxName.Enabled = (slot.Item.Enchantable || boxAllow.Checked);
            foreach (var kvp in Data.enchantments)
            {
                var ench = kvp.Value;
                if (boxAllow.Checked || ench.items.Contains(slot.Item.ID))
                {
                    if (!boxName.Items.Contains(ench))
                    {
                        boxName.Items.Add(ench);
                    }
                }
                else if (boxName.Items.Contains(ench) &&
                         boxName.SelectedItem != ench)
                {
                    boxName.Items.Remove(ench);
                }
            }

            if (boxName.SelectedItem == null)
            {
                return;
            }

            editLevel.Enabled = boxAllow.Checked || (enchantment != null && enchantment.items.Contains(slot.Item.ID) &&
                                                     editLevel.Value >= 0 && editLevel.Value <= enchantment.max);
            bool allow = boxAllow.Checked || enchantment == null || editLevel.Value <0 || editLevel.Value> enchantment.max;

            editLevel.Minimum = allow ? -32768 : 0;
            editLevel.Maximum = allow ? (short)32767 : enchantment.max;
        }