Solved

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

Posted on 2010-08-23
4
212 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Entity Framework 7 32
Javascript and Jquery not firing 9 42
Variable Event ? 3 23
Code works but it's slow 24 44
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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

895 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

11 Experts available now in Live!

Get 1:1 Help Now