C#/C#200제

[C#] 20일차 - 141. 콤보박스를 이용한 식당 리스트의 추가, 삭제

반나무 2021. 2. 22. 19:36
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace A141_ComboBox
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox cb = sender as ComboBox; 
            lblRestaurant.Text = "이번 주 모임장소는 : " + cb.SelectedIndex.ToString();

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            // 콤보박스는 값을 입력받을 수 있다.
            if (comboBox1.Text != "")
            {
                comboBox1.Items.Add(comboBox1.Text); // 콤보박스 Item Add
                lblRestaurant.Text = comboBox1.Text + "Added!";
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if(comboBox1.SelectedIndex >= 0)
            {
                lblRestaurant.Text = comboBox1.SelectedItem.ToString() + " Deleted!";
                comboBox1.Items.Remove(comboBox1.SelectedItem); // 콤보박스 Item Delete
            }
        }
    }
}

반응형