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 WindowsFormsListBox { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //添加按钮 private void button1_Click(object sender, EventArgs e) { if(textBox1.Text == "") { MessageBox.Show("请输入要添加的数据"); } else { listBox1.Items.Add(textBox1.Text); textBox1.Text = ""; } } //删除按钮 private void button2_Click(object sender, EventArgs e) { if(listBox1.SelectedItems.Count == 0) { MessageBox.Show("请选择要删除的项目"); } else { listBox1.Items.Remove(listBox1.SelectedItem); } } } }