Thursday, 15 January 2015

c# - Why won't my method work unless called from a button click? -



c# - Why won't my method work unless called from a button click? -

i posted question yesterday, don't think gave plenty info definate answer. here 1 time again additional code create things clearer.

i have form listview, populated calling showcheckedinfiles() method. method works fine when add together simple button form , press it, calls method, when phone call method elsewhere not populate listview.

please help it's driving me insane. first method below called class , shown underneath this, , i've included button method reference, say, button works perfectly, need able phone call method without clicking button!!:

public void openproject(string projectname) { projectname = projectname; string userdir = csdbpath + projectname + "\\checkedout\\" + username; if (!directory.exists(userdir)) //does user's directory exist, if not, create { directory.createdirectory(userdir); } showcheckedinfiles(); } private void button3_click(object sender, eventargs e) { showcheckedinfiles(); }

the method calls above:

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(); }

and actual showcheckedinfiles() method won't build listview unless called button_3_click method .... don't want!

public void showcheckedinfiles() // listview1 - load dms listview create list { listview1.items.clear(); // clears list of files each time method called preventing list beingness duplicated on , on - (refreshes it) !! string[] checkedinfilelist = directory.getfiles(csdbpath + projectname, "*.sgm", searchoption.alldirectories); //jake i've added arguments here , removed \\checkedin, may need delete .sgm etc foreach (string file in checkedinfilelist) { listviewitem itemname = list1.getname(file); // info files in array long itemsize = list1.getsize(file); datetime itemmodified = list1.getdate(file); listview1.items.add(itemname); // utilize info populate listview itemname.subitems.add(itemsize.tostring() + " kb"); itemname.subitems.add(itemmodified.tostring()); // readfromcsv(); //reads info csv file using method // // stringbuilder sb = readinglistview(); //writes info csv file // // filewrite.writetocsv(sb); showstatus(itemname); } showmycheckedoutfiles(); }

it reason showcheckedinfiles fails not due it's not beingness called from, due beingness called from. tell more that.

in meantime, guess you're calling before listview's handle has been created (so list doesn't exist yet), or maybe you're calling on different thread (but you'd see exception in case).

c#

No comments:

Post a Comment