我昨天发布了这个问题,但我认为我没有提供足够的信息来获得明确的答案。所以这里又是附加代码,希望能让事情更清楚。
我有一个带有 listView 的表单,它是通过调用 showCheckedInFiles() 方法填充的。当我向表单添加一个简单的按钮并按下它时,该方法工作得非常好,这会调用该方法,但是当我从其他地方调用该方法时,它不会填充我的列表视图。
请帮助它让我发疯。下面的第一个方法是从另一个类调用的,它显示在下面,我也包含了按钮方法以供参考,正如我所说,按钮工作得很好,但我需要能够在不单击的情况下调用该方法一个按钮!!:
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() 方法,除非从 button_3_click 方法调用,否则它不会构建我的 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();
}