Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 289
  • Last Modified:

Populating a listview on a Parent Form from a class file: Need Help! C#

Hi, I've searched everywhere for documentation but can't find a solution to my problem...

Lets say I have a listview on a Parent Form (frmMain.cs). I also have a file (clsSQL.cs) with methods to query a database. When frmMain.cs initializes, it calls a method in clsSQL.cs to query the database for a list of records. I want that list of records to directly be inserted into a listview box on frmMain.cs.

I have made the listview public and a when I try to add something to it from clsSQL.cs (frmMain.lvMsgList.Items.Add("test3");) I get an error:
* An object reference is requred for the nonstatic field, method, or property.

Can anyone provide an example on how I would do this or point me in the write direction on trying to populate data on a Parent Form from a class file?

Thanks!
0
ImJustPondering
Asked:
ImJustPondering
  • 4
  • 2
1 Solution
 
LazyStudentCommented:
frmMain is a class not an object.
frmMain.cs contains definitions for frmMain class.
If you use your function within methods of frmMain you have to write just
// code
lvMsgList.Items.Add("test3");
//or
this.lvMsgList.Items.Add("test3");
// end of code

Error you get is understandable. When you try to run method as ClassName.SomeMethod() SomeMethod have to be defined as static ( that is you are not going to change object context by this method )
0
 
ImJustPonderingAuthor Commented:
Right, but I'm having a problem adding items to frmMain.cs from a method in clsSQL.cs. Any ideas?
0
 
LazyStudentCommented:
if you want to add someting to page frmMain you have to pass refererence to object of class frmMain in function.
For example - if you have in class clsSQL method
"AddItemToList" it will look like this:

public void AddItemToList(frmMain frmObj)
{
   frmObj.lvMsgList.Items.Add("test3");
}
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LazyStudentCommented:
in my example lvMsgList has to be public field but better to declare public method in frmMain class like "AddItemsToMsg" and lvMsgList can be private in this case.
function will look:

public void AddItemToList(frmMain frmObj)
{
  frmObj.AddItemsToMsg("test3");
}

and in frmMain class:

public void  AddItemsToMsg(string str)
{
lvMsgList.Items.Add(str);
}
0
 
ImJustPonderingAuthor Commented:
Exactly the explaination I was looking for. Thanks!
0
 
LazyStudentCommented:
So why you graded me "B" and not "A":-( ?
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now