By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,849 Members | 2,339 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,849 IT Pros & Developers. It's quick & easy.

create new controls each iteration of a loop

P: n/a
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,fileS torage,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( ));
}
}
Dec 7 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Mike Mac" wrote:
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,fileS torage,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( ));
}
}

Dec 7 '05 #2

P: n/a
You never change the variable "down", so you're creating several textboxes
on top of each other. Try "down += 25" instead of "down + 25"
"Mike Mac" <Mike Ma*@discussions.microsoft.com> wrote in message
news:B3**********************************@microsof t.com...
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,fileS torage,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( ));
}
}

Dec 7 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.