Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a win form there is a button and listbox on this form now i want to add item in list from other class. code for this given as

code behind form

C#
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Class1 obj = new Class1();
            obj.updatelist();
        }
        public void UpdateL(string msg)
        {
            listBox1.Items.Add(msg);
        }
    }


and the code behind class1 is

C#
class Class1
    {
        Form1 obj = new Form1();
        public void updatelist()
        {
            obj.UpdateL("This is updated from other class");
        }
    }


all functions calls when i press button but no item added to listbox mean UI not updated from other class

can anybody help me
Thanks!
Posted
Comments
OriginalGriff 7-Jun-10 6:02am    
Is there any method to update listbox from this class." Same problem: now you are trying to fit something under the drivers seat of the original car by shoving it under the seat of the one you created in the glove box...

Your Class1 will look like this.

Create a delegate and event on Class1

class Class1
    {
        Form1 obj = new Form1();
        public delegate void UpdateList(string item);
        public event UpdateList OnUpdateList;
       
        public void updatelist()
        {
          //Invoke the event
            if (OnUpdateList != null)
            {
                OnUpdateList("This is updated from other class");
            }
        }
    }


Register the event in the Form1 class, your code will look like this.


public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Class1 obj = new Class1();

           // Register event.
           obj.OnUpdateList += UpdateL;
           obj.updatelist();
        }

      
        public void UpdateL(string msg)
        {
            listBox1.Items.Add(msg);
        }
    }
 
Share this answer
 
v2
Look at your definition of Class1:
Form1 obj = new Form1();

This constructs a new instance of the Form1 object, which you then modify.
Since this is contained within the class, it does not affect the instance outside (which contains the class1 instance).

This is difficult to explain without pictures, but I'll try:

What you are trying to do is have a car (Form1). Inside the car, is a glove box (Class1). When you open the glove box (via the button1_click event) you create a new car and repaint it. You then close the glove box, and expect the original car to be a different colour.

Find a book on C#. An elementary one, and read the chapter on instances. You need to understand this stuff in good detail before you go any further, as nothing will make sense until you do!

Good luck.
 
Share this answer
 
Comments
Nasir M@hmood 7-Jun-10 5:56am    
Is there any method to update listbox from this class.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900