Form1.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 WindowsFormsListView
  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. listView1.Items.Add(textBox1.Text.Trim());
  28. textBox1.Text = "";
  29. }
  30. }
  31. //移除项
  32. private void button2_Click(object sender, EventArgs e)
  33. {
  34. if (listView1.SelectedItems.Count == 0)
  35. {
  36. MessageBox.Show("请选择要删除的项");
  37. }
  38. else
  39. {
  40. listView1.Items.RemoveAt(listView1.SelectedItems[0].Index);
  41. listView1.SelectedItems.Clear();
  42. }
  43. }
  44. //清空
  45. private void button3_Click(object sender, EventArgs e)
  46. {
  47. if (listView1.Items.Count == 0)
  48. {
  49. MessageBox.Show("项目中已经没有项目");
  50. }
  51. else
  52. {
  53. listView1.Items.Clear();
  54. }
  55. }
  56. }
  57. }