Solved

What is the best practice for updating a WinForm from another class?

Posted on 2010-08-23
4
211 Views
Last Modified: 2013-12-17
I have a MainForm with a Listbox on it.  I have another separate class that is doing a process that I want the Listbox to report on.  What would be the best way to do this?  An example would be great.  Sorry to be vague but I am working on the concept and I do not have much more than that.  Feel free to ask me any questions you may think of.  Thanks for any help!
0
Comment
Question by:Todd710
4 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 300 total points
ID: 33504453
The class should raise a custom event that the main form subscribes to.  When the event is received you update the ListBox with the new information...
0
 
LVL 14

Assisted Solution

by:systan
systan earned 200 total points
ID: 33507855
Set the [Listbox modifiers to Public] of your other class.

Then to your Mainform;
Call your other class like this;

{
....
OtherClassForm2 f2= new OtherClassForm2();
listBox1.....  =  f2.listBox1. . . . . . .
......
.....
}
0
 
LVL 2

Expert Comment

by:phill_cahill
ID: 33510276
This example is a little crude, but i think it performs what your asking.

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Create a dataset to receive your data in
            DataSet dsMaster;

            //Call the data function from the class your
            //working with and place it in your master dataset
            dsMaster =  getDataForList.getMyData();

            //update your list box
            listBox1.DataSource = dsMaster.Tables[0];
        }
    }

    public static class getDataForList
    {
        public static DataSet getMyData()
        {
            DataSet ds = new DataSet();
            //Do your SQL lookup ETC and place it into the DataSet called DS
            return ds;
        }
    }
}
0
 

Author Closing Comment

by:Todd710
ID: 33510762
The custom event model is the right answer for what I need but an example would have been nice.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now