Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.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# 无法在函数中添加ListViewItem_C#_Function_Listview_Listviewitem - Fatal编程技术网

C# 无法在函数中添加ListViewItem

C# 无法在函数中添加ListViewItem,c#,function,listview,listviewitem,C#,Function,Listview,Listviewitem,我写了一个由两个表单组成的代码, 主窗体-Form1,从子窗体AddTask获取3个字符串 主要形式: public partial class Form1 : Form { int count = 0; string taskName2, DateTime2, More2; public Form1(string taskName1, string DateTime1, string More1, bool startworking) { Ini

我写了一个由两个表单组成的代码, 主窗体-Form1,从子窗体AddTask获取3个字符串

主要形式:

public partial class Form1 : Form
{
    int count = 0;
    string taskName2, DateTime2, More2;
    public Form1(string taskName1, string DateTime1, string More1, bool startworking)
    {
        InitializeComponent();

        taskName2 = taskName1;
        DateTime2 = DateTime1;
        More2 = More1;
        if(startworking)
        {
            StartWorking();
        }
    }
您可以看到我创建了3个全局使用的字符串,Form1得到了3个字符串和1个布尔变量。当布尔值为true时,函数StartWorking开始

在子表单中,我有一个按钮和3个文本框。该按钮有一个单击事件:

string taskName1 = textBox1.Text;
string DateTime1 = textBox2.Text;
string More1 = textBox3.Text;
Form celender = new Form1(taskName1, DateTime1, More1, true);
this.Close();
因此,当我按下子窗体上的按钮时,布尔值被设置为true,StartWorking函数启动

到这里一切都很好

功能StartWorking:

public void StartWorking()
{
    MessageBox.Show(taskName2 + "  " + DateTime2 + "  " + More2);

    ListViewItem lvi = new ListViewItem(taskName2);
    lvi.SubItems.Add(DateTime2);
    lvi.SubItems.Add(More2);
    listView1.Items.Add(lvi);    
}
现在,在函数中,MessageBox起作用并显示字符串,但当我看到listview1时,没有任何变化。为什么它不创建任何内容?

在实例化Form1之后,您没有显示它。使用Show方法celender.Show显示Form1;并按如下方式更改代码:

Hide();
string taskName1 = textBox1.Text;
string DateTime1 = textBox2.Text;
string More1 = textBox3.Text;
Form1 celender = new Form1(taskName1,DateTime1,More1,true);         
celender.Show();
celender.Closed += (s, args) => this.Close();