How to fill listview in a different class and file?

O

Ole Baba

In Form1.cs I have defined the "InstituteAdministrator"
class which contains ListView "listViewAddress". In a second
file (FormTreeView.cs) I have the class FormTreeView with
the method "dataTreeView1_AfterSelect". When the user
selects a node in the treeview, then listViewAddress should
be filled and displayed with the corresponding addresses.

As long as I keep the whole thing in one file, it works
fine, but not with two files and two classes.

I want to use dotnetmagic.com's libraries, which come with
this two-files example for docking.

First I created a method within the InstituteAdministrator
class which establishes the database connection and fills
the listview. Within FormTreeView class I made a reference
to the Institute Administrator class and called the filling
method I defined, passing "e.Node.Tag.ToString()" as
parameter to the method.

The program seemed to work, however the data is not
displaying in the listview. Since then I'm trying out (try
and error...). So far I did not find sufficient information
in books or online examples to help me to understand what is
going on. (needless to say that I'm a newbie)

Which are the steps to make this work? Any hints, anyone?

Link to screenshots and source-files:
http://www.osswald.com/vstudio/

Direct link to source files (flash-paper prints):
http://www.osswald.com/vstudio/images/form1.cs.swf
http://www.osswald.com/vstudio/images/FormTreeView.cs.swf

Thanks, Oliver
 
I

Ignacio Machin \( .NET/ C# MVP \)

hi

I assume you have both forms visible, if so you need to pass a reference of
the listviewform to the FormTreeView, you can do that whne you create the
latter. in the first form create a public method that will receive the ID
from thre selected tree, then the method will retrieve the data and populate
the listview

In the worst case that the TreeView's form is not create by the first form
you could keep a reference to the currently displayed form in a static
property.


hope this help,
 
O

Ole Baba

Ignacio Machin \( .NET/ C# MVP \) said:
I assume you have both forms visible,
if so you need to pass a reference of
the listviewform to the FormTreeView,
you can do that whne you create the
latter. in the first form create a
public method that will receive the ID
from thre selected tree, then the method
will retrieve the data and populate
the listview

Great! It worked! It took me some moments to figure out what "pass a
reference to the FormTreeView" means...

So, in Form1 (the form with the listview) I wrote:

FormTreeView ftv = new FormTreeView();
ftv.MyParentForm = this;

and added a method "fillListViewAddress"


Then, in the FormTreeView class I wrote:

public InstituteAdministrator MyParentForm;

and added the method:

internal void dataTreeView1_AfterSelect(object sender,
System.Windows.Forms.TreeViewEventArgs e)
{
MyParentForm.fillListViewAddress(e.Node.Tag.ToString());
}

This seems to do what I have hoped for. Thanks a lot!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top