오늘의 코딩

winform listBox crud

용성 2022. 1. 18. 14:22

오후

private void button1_Click(object sender, EventArgs e)
        {
            if (txb.Text == "")
            {
                MessageBox.Show("리스트를 작성해주세요");
            } else
            {
                listBox1.Items.Add(txb.Text);
                txb.Text="";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int sel = listBox1.SelectedIndex;
            if(txb.Text == "")
            {
                MessageBox.Show("수정하실 내용을 적어주세요");
            }else
            {
                listBox1.Items[sel] = txb.Text;
                txb.Text = "";
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int sel = listBox1.SelectedIndex;
            if (sel < 0)
            {
                MessageBox.Show("삭제할 리스트를 클릭해주세요");
            } else
            {
            listBox1.Items.RemoveAt(sel);
            }
        }
    }

결과물

728x90