private void tabControl_SelectedIndexChanged(object sender, EventArgs e) { TabControl tc = sender as TabControl; if (tc.SelectedTab.Text == "Города") { lbCities.Items.Clear(); using (teamProject2022Entities le = new teamProject2022Entities()) { Cities city = new Cities(); foreach (var ct in le.Cities) { lbCities.Items.Add(ct.city); } } } }
private void btOk_Click(object sender, EventArgs e) { if (tbCity.Text == string.Empty) { return; } if (btOk.Text == "Добавить") { using (teamProject2022Entities le = new teamProject2022Entities()) { Cities city = new Cities() { city = tbCity.Text }; le.Cities.Add(city); le.SaveChanges(); } tabControl_SelectedIndexChanged(tabControl, EventArgs.Empty); } else if (btOk.Text == "Удалить") { using (teamProject2022Entities le = new teamProject2022Entities()) { Cities c = le.Cities.FirstOrDefault(x => x.city.Equals(tbCity.Text, StringComparison.InvariantCultureIgnoreCase)); if (c != null) { le.Cities.Remove(c); } le.SaveChanges(); } tabControl_SelectedIndexChanged(tabControl, EventArgs.Empty); tbCities.Text = ""; btOk.Text = "Добавить"; } }