I开发者_高级运维 am pretty new to programming in c#. I have a listview (ListViewProjects)
box in ProjectListForm
but I want to be able to access the items in the listview
box in AddProjectForm
. How can I access them? I was going to try to do something like this
ProjectListForm.ListViewProjects.Items.Clear
but when I type in ProjectListForm., ListViewProjects
isn't an option.
You can either set the list view's design time options to generate a public member (in control's properties, under Design set Modifiers to Public), or possibly better you could add a public property to ProjectListForm
to expose either the list view or just the Items
collection, eg:
public class ProjectListForm : Form
{
...
public ListView.ListViewItemCollection ProjectsListViewItems
{
get { return ListViewProjects.Items; }
}
}
The best way IMO is to write public methods in ProjectListForm
that do what you're trying to achieve in AddProjectForm
, such as ProjectListForm.ClearProjects()
. That way you're not forcing AddProjectForm
to depend on the implementation of ProjectListForm
, say if you later want to change to a TreeView
.
You cannot achieve this way in C#. you need to create the instance of the object.
To do this you have to create the method in ProjectListForm
and in that methos you have to call this.ListViewProjects.Items.Clear
.
Now from the AddProjectForm
you have to call this method of the running instance of ProjectListForm
.
If you want to know more about how to make the call, then please give the flow about how do you show the AddProjectForm from the ProjectListForm.
精彩评论