简体   繁体   English

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

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

I am trying to figure out, for an assignment, a way to display a message that basically says "combo box is empty" after I remove all items from a combo box in C#. 我试图找出一种分配方法,该方法是在我从C#的组合框中删除所有项目后,显示一条基本上说“组合框为空”的消息。 The assignment is very simple. 分配非常简单。 I write a windows form in C# that is populated with fifteen states in a ComboBox and when I select an item from that list it is removed. 我用C#编写了一个Windows窗体,在ComboBox中填充了十五个状态,当我从该列表中选择一个项目时,该窗体便被删除了。 I have it working but once all the items are gone It just sits there and I have to manually exit. 我可以使用它,但是一旦所有物品都走了,它就坐在那儿,我必须手动退出。 Can someone point me in the right direction to get this to work, I am thinking maybe if statement is in order? 有人可以指出正确的方向来使它起作用,我在想也许陈述是否正确? Here is my code so far... 到目前为止,这是我的代码...

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();
        }
    }
}

Thank You 谢谢

Check the number of items left, after you remove one. 删除一项后,检查剩余项目数。 You can Count on the Items collection to see how many items are left in the ComboBox . 你可以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");
        }

You can check for the condition below 您可以检查以下情况

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

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

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