C# 为什么赢了';除非通过单击按钮调用,否则我的方法不会工作吗?
我昨天发布了这个问题,但我认为我没有提供足够的信息来得到明确的答案。因此,这里再次添加了额外的代码,希望能让事情变得更清楚 我有一个带有listView的表单,它是通过调用showCheckedInFiles()方法填充的。当我向表单中添加一个简单的按钮并按下它时,该方法工作得非常好,这将调用该方法,但是当我从其他地方调用该方法时,它将不会填充我的listview 请帮帮我,我快疯了。下面的第一个方法是从另一个类调用的,如下面所示,我还包括了button方法供参考,正如我所说,button工作得很好,但我需要能够调用该方法而无需单击按钮!!:C# 为什么赢了';除非通过单击按钮调用,否则我的方法不会工作吗?,c#,C#,我昨天发布了这个问题,但我认为我没有提供足够的信息来得到明确的答案。因此,这里再次添加了额外的代码,希望能让事情变得更清楚 我有一个带有listView的表单,它是通过调用showCheckedInFiles()方法填充的。当我向表单中添加一个简单的按钮并按下它时,该方法工作得非常好,这将调用该方法,但是当我从其他地方调用该方法时,它将不会填充我的listview 请帮帮我,我快疯了。下面的第一个方法是从另一个类调用的,如下面所示,我还包括了button方法供参考,正如我所说,button工作得
public void openProject(string projectname)
{
projectName = projectname;
string userDir = CSDBpath + projectname + "\\checkedOUT\\" + userName;
if (!Directory.Exists(userDir)) //Does the user's directory exist, if not, create it
{
Directory.CreateDirectory(userDir);
}
showCheckedInFiles();
}
private void button3_Click(object sender, EventArgs e)
{
showCheckedInFiles();
}
调用上述函数的方法:
private void buttonOpenProject_Click(object sender, EventArgs e)
{
ListView.SelectedListViewItemCollection mySelectedItems;
mySelectedItems = listView1.SelectedItems;
Form1 mainform = new Form1();
string myProject = "";
foreach (ListViewItem item in mySelectedItems)
{
myProject = item.Text;
}
mainform.openProject(myProject);
//mainform.showCheckedInFiles();
this.Close();
}
这是实际的showCheckedInFiles()方法,除非从按钮\u 3\u单击方法调用,否则它不会生成我的listView。。。。我不想要
public void showCheckedInFiles() // ListView1 - load the DMs into the listView to create the list
{
listView1.Items.Clear(); // this clears the list of files each time the method is called preventing the list from being duplicated over and over - (refreshes it) !!
string[] checkedINfileList = Directory.GetFiles(CSDBpath + projectName, "*.sgm", SearchOption.AllDirectories); //JAKE I'VE ADDED THE EXTRA ARGUMENTS HERE and removed \\CheckedIN, MAY NEED TO DELETE FROM .SGM ETC
foreach (string file in checkedINfileList)
{
ListViewItem itemName = list1.getName(file); // get this information from the files in the array
long itemSize = list1.getSize(file);
DateTime itemModified = list1.getDate(file);
listView1.Items.Add(itemName); // now use that information to populate the listview
itemName.SubItems.Add(itemSize.ToString() + " Kb");
itemName.SubItems.Add(itemModified.ToString());
// readFromCSV(); //Reads the data to the CSV file using the method
// // StringBuilder sb = ReadingListView(); //writes the data to the CSV file
// // fileWrite.writeToCSV(sb);
showStatus(itemName);
}
showMyCheckedOutFiles();
}
这可能是因为
showCheckedInFiles
失败的原因不是因为它不是从哪里调用的,而是因为它是从哪里调用的。你可以再告诉我们一些
同时,我猜您是在创建listView句柄之前调用它(因此列表实际上还不存在),或者您是在另一个线程上调用它(但在这种情况下,您很可能会看到异常)。ButtonPenProject\u Click()似乎不会调用任何东西来显示它创建的主窗体。我看不出你有什么办法能看到它 在代码
projectName
这一行中,这是如何定义的projectName=projectName代码>也调试了此代码..?是否从UI线程调用showCheckedInFiles()?可能您遇到了调用问题。您需要向我们显示不起作用的调用代码。这就是问题所在。“我的方法不起作用”到底意味着什么?通常,如果你有更多信息要添加到你的问题中,或者你想重新措辞,你应该使用编辑
链接,而不是发布新问题。(然而,你的老问题获得了相当多的票数,所以我只是把它推到了边缘)。很难说他的实际流程是什么,但看看代码,我注意到了几个问题和/或可能提出的问题。那么告诉我ShowCheckedFiles()的哪一部分将显示他创建的表单?对Form.Show()的调用在哪里?它可能在调用的其他方法中,但谁知道呢?确切地说。。结构不良的问题,这就是为什么我从来没有在没有我刚才所说的所有相关信息的情况下发布答案…这就是为什么我说“看起来不”而不是“不”。。。我的回复至少会让他去检查,并报告更多信息(我希望…)啊哈,我看你是对的,马修,我会删除我的评论,只是注意到我自己..我的谢天谢地,伙计们,实际上我并不真的想创建一个名为mainform的新表单1。我要做的是将myProject变量传递回主窗体中已加载的方法。我认为这就是问题所在。正如你所知,我是一个完全的新手,这是我第一次正确的应用程序。有人能告诉我如何避免创建form1的新实例,并且仍然将myProject传递回form1类吗?