private void deleteCategoryButton_Click(object sender, EventArgs e) { int categoryNo = (int)categoryComboBox.SelectedValue; bool categoryDeleted = false; // Category cannot be deleted if it has sub categories using (var core = new StandardBusinessLayer(DataCache)) { core.Connect(); int subCategoriesCount = core.CountCategoriesByParentCategoryNo(categoryNo); if (subCategoriesCount > 0) { MessageBox.Show("Kategorin kan inte tas bort eftersom den har underkategorier.", CurrentApplication.Name, MessageBoxButtons.OK, MessageBoxIcon.Stop); return; } int cashBookTransactionsCount = core.CountCashBookTransactionsByCategoryNo(categoryNo); if (cashBookTransactionsCount > 0) { MessageBox.Show("Kategorin kan inte tas bort eftersom den har registrerade kassabokshändelser.", CurrentApplication.Name, MessageBoxButtons.OK, MessageBoxIcon.Stop); return; } Category category = core.GetCategory(categoryNo); DialogResult result = MessageBox.Show("Är du säker på att du vill ta bort kategorin '" + category.Name + "'?", CurrentApplication.Name, MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2); if (result == DialogResult.Yes) { core.DeleteCategory(categoryNo); categoryDeleted = true; } } if (categoryDeleted) { ReloadForm(); } }