Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Can';无法清除列表框_C# - Fatal编程技术网

C# Can';无法清除列表框

C# Can';无法清除列表框,c#,C#,我正在使用一个new按钮,单击该按钮可重置表单应用程序。 然而,我遇到了一个似乎无法解决的问题 基本上,我想做的是在单击“新建”按钮时清除列表框 “新建”按钮应完全按照以下步骤初始化程序: 启动(但不重新启动应用程序)。如果没有数据 已保存,允许用户(通过消息框)确认 继续而不保存当前数据或返回当前 会议 以下是我如何尝试的: private void mnuNew_Click(object sender, EventArgs e) { for (int index =

我正在使用一个
new
按钮,单击该按钮可重置表单应用程序。 然而,我遇到了一个似乎无法解决的问题

基本上,我想做的是在单击“新建”按钮时清除列表框

“新建”按钮应完全按照以下步骤初始化程序: 启动(但不重新启动应用程序)。如果没有数据 已保存,允许用户(通过消息框)确认 继续而不保存当前数据或返回当前 会议

以下是我如何尝试的:

private void mnuNew_Click(object sender, EventArgs e)
    { 
        for (int index = 0; index < animalmgr.Count; index++)
        {
            Animal animal = animalmgr.GetAt(index);

            if (animal != null)
            {
                // error message
                DialogResult dialogResult = MessageBox.Show("The data will be lost. Continue?", "Are you sure?", MessageBoxButtons.YesNo);
                if (dialogResult == DialogResult.Yes)
                {
                    InitializeGUI();
                }    
                else if  (dialogResult == DialogResult.No)
                {
                }
            }

            else
            {
                ClearLists();  <--------This does not work!
            }
        }
    }


private void InitializeGUI()
        {
            animalmgr.DeleteAll();
            Resultlst.Items.Clear();
            foodItemslst.Items.Clear();
        }

public void  ClearLists()
        {
            Gendercmb.DataSource = Enum.GetValues(typeof(GenderType));
            Categorylst.DataSource = Enum.GetValues(typeof(Categorytype));
            Resultlst.Items.Clear();
            foodItemslst.Items.Clear();
        }
private void mnuNew\u单击(对象发送者,事件参数e)
{ 
对于(int index=0;index
public void  ClearLists()
{
    Gendercmb.DataSource = Enum.GetValues(typeof(GenderType));
    Categorylst.DataSource = Enum.GetValues(typeof(Categorytype));
    Resultlst.DataSource = new List<ListItem>();
    foodItemslst.DataSource = new List<ListItem>();
}
public void ClearLists()
{
Gendercmb.DataSource=Enum.GetValues(typeof(GenderType));
Categorylst.DataSource=Enum.GetValues(typeof(Categorytype));
Resultlst.DataSource=新列表();
foodItemslst.DataSource=新列表();
}

请记住,如果这是一个asp.net webforms应用程序,您还需要同时对foodItemslst和ResultList调用DataBind()dunction。

我相信问题在于您对

animal != null

看看你的逻辑,上面的一行给出了
animal
一个值,即使该值为0。因此
animal
永远不会为空,因为它总是有一个值。

你调试过这个吗?animal
实际上为空吗?首先我会在代码中放置断点。然后我会调试代码并逐步执行此
foodItemslst
对象的范围是什么。如果
animal
不为null,则
If语句将为true并显示messagebox@MethodMan<代码> FooTimeLST> <代码>只是另一个应该被清除的列表框,就像'RultList'动物从来没有空过,问题就在别处。您的逻辑。此外,这也是调试器存在的原因。谢谢,但问题不在于方法
ClearLists()
问题在于
If语句
永远不会到达
else语句
在ClearLists()中放置一个消息框函数只是为了确保函数正在被调用。因为必须调用if或else语句。除非您的mnuNew_Click()事件根本没有被触发。或者另一种情况可能是animalmgr为空。我按您所说的那样尝试,但消息框没有显示。正如我所想,永远不会调用
else语句
。将调试点放在for循环上,并检查是否命中该点并进入for循环。理论上,该值可能为
null
y、 这是一个合理的假设,但问题在于OP没有提供的代码-要么是
animalmgr
,要么是
GetAt()
方法(如果GetAt()方法是由Microsoft提供的,请纠正我)。