Form1.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace WindowsFormsListBox
  11. {
  12. public partial class Form1 : Form
  13. {
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. }
  18. //添加按钮
  19. private void button1_Click(object sender, EventArgs e)
  20. {
  21. if(textBox1.Text == "")
  22. {
  23. MessageBox.Show("请输入要添加的数据");
  24. }
  25. else
  26. {
  27. listBox1.Items.Add(textBox1.Text);
  28. textBox1.Text = "";
  29. }
  30. }
  31. //删除按钮
  32. private void button2_Click(object sender, EventArgs e)
  33. {
  34. if(listBox1.SelectedItems.Count == 0)
  35. {
  36. MessageBox.Show("请选择要删除的项目");
  37. }
  38. else
  39. {
  40. listBox1.Items.Remove(listBox1.SelectedItem);
  41. }
  42. }
  43. }
  44. }