Taimoor Khalid
Taimoor Khalid

Reputation: 13

dynamically updating the label in c#

i am using EAGetMail to access the name of the attachments that i am receiving successfully. But on the other hand i want to place that name on the label as shown in the code but my label value is not changing at all.Instead when i click on the other attachment the name of that attachment and the previous attachment combine together and show on label. below is the label code i m using.

for (int a = 0; a < count; a++)
 {

     att = atts[a];
     MessageBox.Show(""+att.Name);
     label1.Text = att.Name.ToString();
     System.Threading.Thread.Sleep(1000);

     // MessageBox.Show(""+att.Name);

     label1.Location = new System.Drawing.Point(50, 20);
     label1.ForeColor = System.Drawing.Color.Black;
     label1.AutoSize = true;
     picture = new PictureBox();
     picture.SizeMode = PictureBoxSizeMode.StretchImage;
     picture.SizeMode = PictureBoxSizeMode.StretchImage;
     picture.Location = new System.Drawing.Point(20, 10);
     picture.Size = new System.Drawing.Size(30, 30);
     picture.Image = new Bitmap("C:/Users/HP/source/repos/EmailViewer/EmailViewer/Resources/atts.png");
     panel6.Controls.Add(picture);
     panel6.Controls.Add(label1);
}

Upvotes: 0

Views: 67

Answers (2)

Dimitri
Dimitri

Reputation: 1271

You can make use of Events:

Here is an example of how to use them. Let me know if you have any trouble implementing this.

Upvotes: 0

Code Name Jack
Code Name Jack

Reputation: 3333

You should create a new instance of Label and Picture everytime you are adding them to the container. Otherwise container will always refer to the same object.

so in your for loop,

for (int a = 0; a < count; a++)
{
 label1=new Label();
   ........Rest of the code.... 
}

Upvotes: 1

Related Questions