繁体   English   中英

从组合框删除所有项目后,C#显示MessageBox

[英]C# Display MessageBox when all items from ComboBox are removed

我试图找出一种分配方法,该方法是在我从C#的组合框中删除所有项目后,显示一条基本上说“组合框为空”的消息。 分配非常简单。 我用C#编写了一个Windows窗体,在ComboBox中填充了十五个状态,当我从该列表中选择一个项目时,该窗体便被删除了。 我可以使用它,但是一旦所有物品都走了,它就坐在那儿,我必须手动退出。 有人可以指出正确的方向来使它起作用,我在想也许陈述是否正确? 到目前为止,这是我的代码...

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 Chapter_15_Ex._15._3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox1.Items.Remove(comboBox1.SelectedItem);

        }


        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}

谢谢

删除一项后,检查剩余项目数。 你可以CountItems的收集,看看有多少物品留在ComboBox

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    comboBox1.Items.Remove(comboBox1.SelectedItem);

    if (comboBox1.Items.Count == 0)
        MessageBox.Show("All Gone!");
}
        if (comboBox1.Items.Count == 0)
        {
            MessageBox.Show("Your combo is empty");
        }

您可以检查以下情况

if (comboBox1.Items.Count == 0)
{
    MessageBox.Show("Empty");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM