c# - Access listbox from another class? -
hey guys made class when user selects item listbox uninstalls item, except problem can't access list box. tried public aswell, in code of form1.cs thing clostest list box
keep in mind name of listbox programslistbox ok guys re edited post;
private void button1_click(object sender, eventargs e) { if(programslistbox.selectedindex == -1) { messagebox.show("please select item uninstall!"); } else { programslistbox_selectedindexchanged("",eventargs.empty); } }
this code form1.cs class, , have class called uninstallitem.cs want code be, below other class
namespace pc_tech_registery_cleaner { class uninstallitem {
public void uninstallselecteditem() { form1 c = new form1(); } }
}
and below still in form1.cs class, experimenting :
public void programslistbox_selectedindexchanged(object sender, eventargs e) { //this access uninstall item class can uninstall selected item. uninstallitem c = new uninstallitem(); c.uninstallselecteditem(); }
within form1.cs create instance of unistallitem class , use it. on button click call "removeselected" method of uninstaitem class passing programslistbox , should remove selected item.
public class form1:form { listbox programslistbox; uninstallitem unistall; public form1(){ initializecomponent(); uninstall = new uninstallitem(); button1.click+= button1_click; } void button1_click(object sender, eventargs e){ unistall.removeselected(programslistbox); } }
then in external class;
public class uninstallitem{ public uninstallitem{} public void removeselected(listbox list) { if(list.selectedindex==-1) { messagebox.show("please select item list"); return; } list.items.removeat(list.selectedindex); } }
Comments
Post a Comment