M.Lopez
M.Lopez

Reputation: 67

How do I add attachments without replacing the old one?

I'm working on a messaging software, I'm working on the Attachements part, I can attach the files but, when I try to add more, it replaces the old ones.

This is the code:

List<string> listaAnexos = new List<string>();
Archivo.Multiselect = true;
Archivo.ShowDialog();
int cAnex = 0;
string[] anexos = Archivo.FileNames;

foreach (string i in anexos) 
{ 
    listaAnexos.Add(i);
    cAnex++;   
}
lbAnexos.DataSource = listaAnexos;
txtCAnex.Text = cAnex.ToString();

Thanks

Upvotes: 1

Views: 74

Answers (1)

Grant Winney
Grant Winney

Reputation: 66439

Assuming the above piece of code is called multiple times, you most likely need to declare listaAnexos outside of your method.

Every time you run the above method, you create a new instance of listAnexos to add files to, which you then assign to lbAnexos.DataSource, overwriting whatever was in there before.

Declare listaAnexos as a class instance, instead of inside your method.

public class YourClass
{
    private List<string> listaAnexos = new List<string>();

    private void YourMethod()
    {
        Archivo.Multiselect = true;
        Archivo.ShowDialog();

        ...

        foreach (string i in anexos) 
        { 
            listaAnexos.Add(i);
            ...

Upvotes: 1

Related Questions