Solved

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

Posted on 2010-08-23
4
218 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
[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 Comments
 
LVL 86

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

628 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