?
Solved

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

Posted on 2003-03-28
6
Medium Priority
?
286 Views
Last Modified: 2010-08-05
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
Comment
Question by:ImJustPondering
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 5

Expert Comment

by:LazyStudent
ID: 8228591
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
 

Author Comment

by:ImJustPondering
ID: 8229573
Right, but I'm having a problem adding items to frmMain.cs from a method in clsSQL.cs. Any ideas?
0
 
LVL 5

Accepted Solution

by:
LazyStudent earned 150 total points
ID: 8230029
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 5

Expert Comment

by:LazyStudent
ID: 8230036
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
 

Author Comment

by:ImJustPondering
ID: 8233288
Exactly the explaination I was looking for. Thanks!
0
 
LVL 5

Expert Comment

by:LazyStudent
ID: 8233430
So why you graded me "B" and not "A":-( ?
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses
Course of the Month10 days, 18 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question