C# 为什么我的表单在关闭时仍向列表中添加项目?

C# 为什么我的表单在关闭时仍向列表中添加项目?,c#,winforms,listview,C#,Winforms,Listview,到目前为止,我有一个表单1,我按下一个按钮,它将加载第二个表单,该表单有两个文本框和一个组合框,用户将填写这些文本框和组合框,然后将它们添加到表单1上列表视图中显示的列表中。但是,它可以正常工作,除非当我使用按钮或按下角落中的红色X关闭第二个表单时,它仍然会向列表中添加一个空白项。这并不是我想要它做的 这是我在form1上的按钮代码: private void button1_Click(object sender, EventArgs e) { // if th

到目前为止,我有一个表单1,我按下一个按钮,它将加载第二个表单,该表单有两个文本框和一个组合框,用户将填写这些文本框和组合框,然后将它们添加到表单1上列表视图中显示的列表中。但是,它可以正常工作,除非当我使用按钮或按下角落中的红色X关闭第二个表单时,它仍然会向列表中添加一个空白项。这并不是我想要它做的

这是我在form1上的按钮代码:

    private void button1_Click(object sender, EventArgs e)
    {
        // if there is less than items in the list, load the form
        if (addTask.Count < 10)
        {
            // New instance to load the form
            newTaskForm frm2 = new newTaskForm();
            frm2.ShowDialog();
            NewTask task = new NewTask();

                // Get the values entered by the user, eg title will be the text in textBox1 etc.
                task.title = frm2.textBox1.Text;
                task.description = frm2.textBox2.Text;
                try
                {
                    task.priority = frm2.comboBox1.SelectedItem.ToString();
                }
                catch
                {
                }
                task.completionDate = frm2.dateTimePicker1.Value.ToString();
                addTask.Add(task); // Add task to the list
                listView1.Items.Add(task.title); // Display task title in the list view

                // close form
                frm2.Close();
                frm2.Dispose();

            }            

        // if there are 10 items in the list, display a message
        else
        {
            MessageBox.Show("Maximum number of tasks added");
        }
    }

我只是不明白,当我按下红色的X键关闭第二个窗体时,它会在列表/列表视图中添加一个空白项?

您没有选中ShowDialog的DialogResult:

我建议对newTaskForm进行改进:

private void button1_Click(object sender, EventArgs e)
{
  // check to see if all the fields have been filled in properly
  if (string.IsNullOrWhiteSpace(textBox1.Text) || string.IsNullOrWhiteSpace(textBox2.Text) || comboBox1.SelectedItem == null)
  {
      MessageBox.Show("Please fill in all fields");
  }
  else
  {
      DialogResult = DialogResult.OK;
      Close();
  }
}

您没有检查ShowDialog的DialogResult:

我建议对newTaskForm进行改进:

private void button1_Click(object sender, EventArgs e)
{
  // check to see if all the fields have been filled in properly
  if (string.IsNullOrWhiteSpace(textBox1.Text) || string.IsNullOrWhiteSpace(textBox2.Text) || comboBox1.SelectedItem == null)
  {
      MessageBox.Show("Please fill in all fields");
  }
  else
  {
      DialogResult = DialogResult.OK;
      Close();
  }
}

我已经编辑了你的标题。请看,如果共识是否定的,他们就不应该这样。我已经编辑了你的标题。请看,如果共识是否定的,他们就不应该这样做。谢谢!我原来有这个代码,我只记得为什么我删除了它,它刷新我的表格,所以我必须按下关闭按钮两次,然后它关闭它。将新项目添加到列表时也是如此。。为什么会这样?@Jamie1234,form.ShowDialog只是显示一个表单并使其成为模态。它不能取消关闭。在第二个表单中是否有FormClosing事件的处理程序?这可能是问题的根源。此外,我会更改按钮1\单击查看更新我添加了您的更新,但仍然无法修复此奇怪的错误。不,我在第二个表单中没有任何表单关闭事件的处理程序,OP中的代码是我自己输入的表单上的所有代码。就像我说的,如果我删除if语句,那么它可以正常工作,但是当我关闭表单时,它会向列表中添加一个空项。如果有帮助的话,我正在尝试复制这个问题的选定答案:@Jamie1234,我创建了一个简单的2表单项目,添加了您的代码,但无法复制问题。哦,我还决定在一个项目中只做2个简单表单,看看哪里出了问题。。。我离开了frm2.ShowDialog;在第一个表单上的按钮代码中,然后添加了您告诉我的if语句,这就是为什么它加载了两次-。-。非常感谢你的帮助谢谢!我原来有这个代码,我只记得为什么我删除了它,它刷新我的表格,所以我必须按下关闭按钮两次,然后它关闭它。将新项目添加到列表时也是如此。。为什么会这样?@Jamie1234,form.ShowDialog只是显示一个表单并使其成为模态。它不能取消关闭。在第二个表单中是否有FormClosing事件的处理程序?这可能是问题的根源。此外,我会更改按钮1\单击查看更新我添加了您的更新,但仍然无法修复此奇怪的错误。不,我在第二个表单中没有任何表单关闭事件的处理程序,OP中的代码是我自己输入的表单上的所有代码。就像我说的,如果我删除if语句,那么它可以正常工作,但是当我关闭表单时,它会向列表中添加一个空项。如果有帮助的话,我正在尝试复制这个问题的选定答案:@Jamie1234,我创建了一个简单的2表单项目,添加了您的代码,但无法复制问题。哦,我还决定在一个项目中只做2个简单表单,看看哪里出了问题。。。我离开了frm2.ShowDialog;在第一个表单上的按钮代码中,然后添加了您告诉我的if语句,这就是为什么它加载了两次-。-。非常感谢你的帮助
newTaskForm frm2 = new newTaskForm();
DialogResult res = frm2.ShowDialog();
if (res != DialogResult.OK)
   return;
NewTask task = new NewTask();
private void button1_Click(object sender, EventArgs e)
{
  // check to see if all the fields have been filled in properly
  if (string.IsNullOrWhiteSpace(textBox1.Text) || string.IsNullOrWhiteSpace(textBox2.Text) || comboBox1.SelectedItem == null)
  {
      MessageBox.Show("Please fill in all fields");
  }
  else
  {
      DialogResult = DialogResult.OK;
      Close();
  }
}