471,570 Members | 902 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,570 software developers and data experts.

create new controls each iteration of a loop

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
2 1700
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
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.

Similar topics

6 posts views Thread by Rudolf Bargholz | last post: by
6 posts views Thread by SamIAm | last post: by
7 posts views Thread by Matt | last post: by
32 posts views Thread by =?Utf-8?B?U2l2?= | last post: by
reply views Thread by Hatem Nassrat | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by lumer26 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.