formlistbox.cs

来自「csharp课本的源代码」· CS 代码 · 共 54 行

CS
54
字号
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ListBoxExample
{
    public partial class FormListBox : Form
    {
        public FormListBox()
        {
            InitializeComponent();
        }
        //删除选定的课程项     
        private void buttonDelete_Click(object sender, EventArgs e)
        {

            while (listBox1.SelectedIndex >= 0)
            {
                listBox1.Items.RemoveAt(listBox1.SelectedIndex);
            }
        }
        //清空课程列表
        private void buttonDeleteAll_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
        }
        //向课程列表中添加新课程
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            string addedText = textBoxAdd.Text;
            if (addedText == "")
            {
                MessageBox.Show("没有输入所要添加的课程!");
                return;
            }
            //检查当前所要添加的新课程是否已存在于课程列表中
            //若存在给出提示信息;否则添加新项
            if (listBox1.Items.Contains(addedText))
            {
                MessageBox.Show("课程<" + addedText + ">在列表中已存在!");
            }
            else
            {
                listBox1.Items.Add(addedText);
            }
            textBoxAdd.Clear();
        }
      
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?