create new controls each iteration of a loop

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello all,

I have a loop and would like to create a new textbox on each iteration of
the loop. The code below only generates one textbox no matter how many times
the loop loops! Any insight would be greatly appreciated.


int over = 25;
int down = 0;
for (int i = theEmail.Attachments.Count; i > 0; i--)
{
try
{
OL.Attachment attachment =theEmail.Attachments;

string fileStorage=@"c:\" +theEmail.Attachments.DisplayName;


rmaxOutlook.attachmentFormBuilder newAttachTextField=new
attachmentFormBuilder(attachment.DisplayName,fileStorage,i);

TextBox newTxtAttachField = new TextBox();
newTxtAttachField.Width = 338;
newTxtAttachField.Location = new Point(over, down + 25);
newTxtAttachField.Text = attachment.DisplayName;
this.Controls.Add(newTxtAttachField);
attachment.SaveAsFile(@"c:\" + theEmail.Attachments.DisplayName);
}

catch (Exception exc)
{
System.Windows.Forms.MessageBox.Show(exc.ToString());
}
}
 
With non-esential code stripped out:

int over = 25;
int down = 0;
for (int i = 10; i > 0; i--)
{
try
{
TextBox newTxtAttachField = new TextBox();
newTxtAttachField.Width = 338;
newTxtAttachField.Location = new Point(over, down + 25);
down+=25;
newTxtAttachField.Text = "box"+i.ToString() ;
newTxtAttachField.Name ="Textbox"+i.ToString();
this.Controls.Add(newTxtAttachField);
}
catch (Exception exc)
{
System.Windows.Forms.MessageBox.Show(exc.ToString());
}
}

--Peter
 
You never change the variable "down", so you're creating several textboxes
on top of each other. Try "down += 25" instead of "down + 25"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top