How to update listbox from another Form?

Asked

Viewed 239 times

1

Follows code:

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;

Estate listbox5:

  • Modifiers: Public

The above code happens nothing, it just executes and nothing happens. Some solution ?

  • No missing form.listBox5.Databind();

  • @Evandro you meant to sayDataBindings() ?, because the DataBind() there is no.

  • Matheus, the property Modifiers listbox is as public?

  • @Leandro is already as public.

  • @Matheusmiranda is Winforms! tries form.listBox5.DataBindings.Add("Text", date, "Text")

  • @Matheusmiranda or form.listBox5.Refresh();

  • @Evandro didn’t work with form.listBox5.DataBindings.Add("Text", data, "Text") and form.listBox5.Refresh().

Show 2 more comments

1 answer

0

There is a problem in understanding object instances.

What is a class?

Roughly, imagine the class as a design of a car or a plan of a house, in it 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, number of wheels, model and brand. As actions we can have accelerate, brake and change gear.

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

Each object can have its particularity, just imagine, I want to have a black Ford Ka, however you would like to have a white one, isn’t that possible? Just follow my car design and build two cars with different colors.

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, that requests the S.O. to give us a new instance (an object) based on our class model.

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

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

Referências

Your problem

Now that we’ve mastered the concept, let’s go to your question problem.

When the program starts the Windowsforms project in the class Program.cs we are creating a new object from the class Form1:

//Repare que está sendo utilizado o "new", então temos um novo objeto criado:
Application.Run(new Form1());

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

When you’re in your class Form2 you are creating a new object, a new instance of Form1 and is filling in her data, IE, It has nothing to do with your main form that was being used earlier and that is open in the previous form, because are different instances.

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

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

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

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

I hope I’ve helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.