C# 无法在函数中添加ListViewItem
我写了一个由两个表单组成的代码, 主窗体-Form1,从子窗体AddTask获取3个字符串 主要形式: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
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();