Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refreshing object on separate form

Tags:

c#

.net

oop

I have an application that shows data from a MySQL table. Basically, my application consists of two forms: the main form, and a form for adding stuff to the database.

The main form shows all the entries in the database and relevant information. When the user wants to add a new entry to the database, a secondary form is opened that prompts for information. Once the information is filled out, the user presses a Submit button and the form closes. My problem is that when the secondary form closes, the listBox in the main form doesn’t update to reflect the newly-added entry.

Here is the code that is executed when the user submits the secondary form:

    private void closeWindow()
    {
        mainForm parent = new mainForm();
        parent.listParts.Refresh();
        this.Close();
    }

Is there a reason when I call the listBox to be refreshed, it doesn’t show my newly-added information? Perhaps I am calling something in the wrong order? Or does the Refresh() method not even work like that?

Any help would be appreciated! Alternatively, if you know of a better way to do this, I’d love to hear it!

like image 599
Andrew De Forest Avatar asked Nov 29 '25 05:11

Andrew De Forest


1 Answers

The problem is that you're refreshing the wrong form:

private void closeWindow()
{
    mainForm parent = new mainForm();
    parent.listParts.Refresh();
    this.Close();
}

Since you use: new mainForm(), you're allocating a completely separate instance of the "mainForm", and refreshing it's content. This will not effect the existing, opened form.

I would recommend passing a reference to the mainForm to the constructor of the secondary form. It then would know which instance of mainForm it needs to use to call Refresh().

like image 84
Reed Copsey Avatar answered Dec 01 '25 19:12

Reed Copsey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!