Hello,
Could you please suggest me how I can save the contents of these dynamically created textboxes into a file? I am not able to access these dynamically created textboxes.
This is the code I've written to create text boxes.
private void button2_Click(object sender, EventArgs e)
{
int left1 = 80;
string num = textBox2.Text;
int n = Int32.Parse(num);
int b = 1;
for (int k = 1; k <=n; k++)
{
int top1 = 30, a = 0;
for (int p = 1; p <= 4; p++)
{
string str = "txt" + b + a;
TextBox txt = new TextBox();
panel1.Controls.Add(txt); //these textboxes are added on the panel
txt.Name = str;
txt.Size = new Size(30, 20);
txt.Top += top1;
top1 += 30;
txt.Left = left1;
txt.BackColor = Color.Cornsilk;
a++;
txt.Show();
}
left1 += 100;
} b++;
}
Now with a click of another button, I want to access the text I've entered in these textboxes (that are present on the panel) and save them in a file.
This is the code I've written,
private void button3_Click(object sender, EventArgs e)
{
string num = textBox2.Text;
int n = Int32.Parse(num);
string dir = textBox1.Text;
string file = "@H:\\" + dir + "\\Test.txt";
int a = 1, b = 1;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j < 4; j++)
{
StreamWriter writer = new StreamWriter(file);
string str = "txt" + b + a;
a++;
//How can I access the [Name] of those textbox I've created dynamically on the panel????
//I want to retrieve the text of these textboxes and save it in a file
string data = str.Text; //Error
string delm1 = "-";
writer.Write(data);
writer.Write(delm1);
writer.Close();
}
b++;
string delim2 = "$";
StreamWriter writer1 = new StreamWriter(file);
writer1.Write(delim2);
writer1.Close();
}
Could you please suggest an effective code?
Thank you in advance.
Loading
Hirendra SisodiyaPosted Mar 22, 2010, 7:40 AM
you can alsoo try this on button3_Click event:
string num = textBox2.Text;
int n = Int32.Parse(num);
string dir = textBox1.Text;
string file = "C:\\" + dir + "\\Test.txt";
int a = 0, b = 1;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j < 4; j++)
{
System.IO.StreamWriter writer = new System.IO.StreamWriter(file,true);
string str = "txt" + b + a;
a++;
Control obj = default(Control);
string TextBox_Name;
string TextBox_Text;
foreach (object obj1 in panel1.Controls)
{
if (obj1.GetType().Name == "TextBox")
{
TextBox txt = new TextBox();
txt = (TextBox)obj1;
if (txt.Name == str)
{
string data = txt.Text;
string delm1 = "-";
writer.Write(data);
writer.Write(delm1);
}
}
}
writer.Close();
//How can I access the [Name] of those textbox I've created dynamically on the panel????
}
b++;
string delim2 = "$";
System.IO.StreamWriter writer1 = new System.IO.StreamWriter(file,true);
writer1.Write(delim2);
writer1.Close();
}
thanksPlease mark as Answere if you like my answere
Ahmar HusainPosted Nov 25, 2013, 2:09 AM
for (int p = 1; p <= 4; p++)
{
txt.ID= "txtBox_"p.ToString();
}
Now you have assigned unique ids to all the text boxes so u can find them like this
for (int p = 1; p <= 4; p++)
{
TextBox t = (TextBox)panel1.FindControl("textBox_P" + i.ToString());
}
panel1 is the ID of your panel where you previously added the textbox.
Shruti RaoPosted Mar 22, 2010, 9:52 AM
Amit ChoudharyPosted Mar 22, 2010, 6:25 AM
try like this:
List
foreach(Controls c in panel1.Controls)
{
if(c is TextBox)
{
txtnames.Add(c.Text);
//or
txtnames.Add(c.ID);
}
}
Hope it helps.
Please mark as answer if it helps you.
Hirendra SisodiyaPosted Mar 22, 2010, 6:21 AM
you can use this code for finding text and name
Windows.Forms.Control obj = default(Windows.Forms.Control);
string TextBox_Name;
string TextBox_Text;
foreach (var obj in Panel1.Controls)
{
if (obj.GetType().Name == "TextBox")
{
TextBox txt = new TextBox();
txt = (TextBox)obj;
TextBox_Name = txt.Name; //Find textbox name
TextBox_Name = txt.Text; //Find textbox text
}
}
thanks
Please mark as Answere if you like my answere