Windows Developer Center > Windows Forms Forums > Windows Forms General > Refresh listbox from another form
Ask a questionAsk a question
 

AnswerRefresh listbox from another form

  • Saturday, April 21, 2007 1:08 AMguilhermecvm2 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Well, when i press the "Add" button in Form2, i'd like to the listView1 in Form1, to do this again:

    public void teste()
    {
    DataSetTableAdapters.ClientesTableAdapter adapter = new DataSetTableAdapters.ClientesTableAdapter();

                DataSet.ClientesDataTable clientes = adapter.GetData();

                foreach (DataSet.ClientesRow cliente in clientes)
                {
                    ListViewItem item1 = listView1.Items.Add(cliente.nome);
                }
    }

    I'm calling it with:
    Form1 Form1 = new Form1();
    Form1.teste();

    but the listview is adding nothing!
    i have tried to put *.add("test");

    but it doesn't work too. When i try to put
    MessageBox.Show("test");
    it works, then the problem is with the listbox adding.

    ps: when i call the teste(); method on "Form1_Load", it works fine!


    Any help?

Answers

  • Monday, April 23, 2007 7:08 AMdecyclone Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    The solution I pointed was an MDI scenario, where one child form could cause another child form to refresh data. I think you dont't have the MDI case.

    So, there are two ways I see you can solve your problem.can be solved.

    1)

    If you know the name of the form that has a listbox to refresh,

    Create a public method in that form, say RefreshListBox();

    And then from anywhere in the code, use :

    ((FormWithListBox)Application.OpenForms["FormWithListBox1"]).RefreshListBox();

    If you don't want to place code to refresh listbox in that form, just create a public property to return that listbox, and use it to clear the old data and fill new data.


    2)

    Create a global DataTable which can be used as ListBox's DataSource, and when you change the DataSource from anywhere, the listbox will be updated.

    Hope this solves your problem.

All Replies

  • Saturday, April 21, 2007 9:23 AMdecyclone Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi guilhermecvm,

    Check out the similar post here.
  • Saturday, April 21, 2007 1:12 PMguilhermecvm2 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    sorry but i'm confused with that code, do i have to do all this just to refresh a listbox or have another way?
  • Monday, April 23, 2007 7:08 AMdecyclone Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    The solution I pointed was an MDI scenario, where one child form could cause another child form to refresh data. I think you dont't have the MDI case.

    So, there are two ways I see you can solve your problem.can be solved.

    1)

    If you know the name of the form that has a listbox to refresh,

    Create a public method in that form, say RefreshListBox();

    And then from anywhere in the code, use :

    ((FormWithListBox)Application.OpenForms["FormWithListBox1"]).RefreshListBox();

    If you don't want to place code to refresh listbox in that form, just create a public property to return that listbox, and use it to clear the old data and fill new data.


    2)

    Create a global DataTable which can be used as ListBox's DataSource, and when you change the DataSource from anywhere, the listbox will be updated.

    Hope this solves your problem.