Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2010-08-23
4
Medium Priority
?
223 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 86

Accepted Solution

by:
Mike Tomlinson earned 900 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 600 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

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

972 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