How to update listbox of another Form?

0

Follow the code below:

Class:

public class Louvor
{
    public int Value { get; set; }
    public string Text { get; set; }
}

Form2:

Form1 form = new Form1();

var data = new List<Louvor>();

var files = Directory.GetFiles($@"{pathname}\Músicas")
                         .Select(Path.GetFileName)
                         .ToArray();

for (int i = 0; i < files.Length; i++)
{

    data.Add(new Louvor() { Value = i, Text = files[i].Replace(".txt", "") });
}

form.listBox5.DisplayMember = "Text";
form.listBox5.DataSource = data;

listbox5 property:

  • Modifiers: Public

The code above happens nothing, it just runs and nothing happens. Any solution?

    
asked by anonymous 30.11.2017 / 17:18

1 answer

2

There is a problem understanding object instances.

What is a class?

Generally, imagine the class as a car project or a house plan, in which we have defined the characteristics (attributes) and actions (methods) of our entity (in this case we will use the car for example). So we can have as characteristics the color of the car, quantity of wheels, model and brand. Like actions we can have to accelerate, brake and change gears.

When we have a model, we can create several cars from that model, or we can deliver the plant from a house to the master builder and we create several houses from that plant. This is the same idea of our class, we have a template to create our objects .

Each object can have its particularity, imagine, I want to have a Ford Ka black, but you would like to have a white, this is possible is not it? Just follow my car design and build two cars with different colors.

The objects follow the same idea, we have different objects, created from the same model (our class) with different characteristics.

To create an object we use the reserved word new , which requests that the S.O. give us a new instance (an object) based on the model of our class.

In the code below we have two different objects, being form1 and form2 :

Form1 form1 = new Form1();
Form1 form2 = new Form1();

Yourissue

Nowthatwecontextualizetheconcept,let'sgotoyourquestionproblem.

WhentheprogramstartstheWindowsFormsprojectinclassProgram.cswearecreatinganewobjectfromtheForm1class:

//Reparequeestásendoutilizadoo"new", então temos um novo objeto criado:
Application.Run(new Form1());

At this point you have your form open, where you register, change, delete the data (it performs all its operations). Here we have an object that is saved in memory with all the data filled in.

When you are in your Form2 class you are creating a new object, a new instance of Form1 and you are filling in your data, ie nothing to do with your form which was previously used and that is open in the previous form because they are different instances .

To solve the problem we need to use the reference that already exists and change the content of it. To get an existing form use the following code:

Form1 form = Application.OpenForms["Form1"] as Form1;

Now when you change the data of your listbox you will see the result normally.

PS: Ignore the drawing made in Paint, it was just to try to explain better rsrsrs.

I hope I have helped.

    
28.12.2018 / 21:39