Reputation: 23
I'm developing a Windows Forms project that requires a user to add new fields(textboxes and labels) by clicking a button, what I want to achieve is. after the user added a new field, a textbox is automatically created and saved to the settings of my form. So that when i run my project again. the newly added textbox (from runtime) is available. Is there any possible ways to do it? I'm new to c# Thanks in advance!
Upvotes: 2
Views: 945
Reputation: 2992
You try this
DataTable is inside System.Data
using System.Data;
Saving to xml
private void button1_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable("Settings"); //we use DataTable class
dt.Columns.Add("ID");
dt.Columns.Add("Control");
dt.Columns.Add("ControlName");
dt.Columns.Add("Top");
dt.Columns.Add("Left");
dt.Columns.Add("Width");
dt.Columns.Add("Height");
/*
* You add more settings here
*/
int id = 0;
foreach (Control ctrl in this.Controls) //this.Controls is the parent control where the textbox is located
{
string c = ctrl.GetType().Name;
switch (c)
{
case "TextBox":
DataRow dr = dt.NewRow();
dr["ID"] = id++;
dr["Control"] = c;
dr["ControlName"] = ctrl.Name;
dr["Top"] = ctrl.Top;
dr["Left"] = ctrl.Left;
dr["Width"] = ctrl.Width;
dr["Height"] = ctrl.Height;
/*
* You add more settings here
*/
dt.Rows.Add(dr);
break;
}
}
dt.WriteXml(@"c:\TestFile.xml", XmlWriteMode.WriteSchema); //You can use save dialog to browse the location
}
To retrieve
private void button2_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable("Settings");
dt.ReadXml(@"c:\TestFile.xml");
foreach (DataRow dr in dt.Rows)
{
switch (dr["Control"].ToString())
{
case "TextBox":
var t = new TextBox();
t.Name = dr["ControlName"].ToString();
t.Top = Convert.ToInt32(dr["Top"]);
t.Left = Convert.ToInt32(dr["Left"]);
t.Width = Convert.ToInt32(dr["Width"]);
t.Height = Convert.ToInt32(dr["Height"]);
this.Controls.Add(t);
break;
}
}
}
Upvotes: 3
Reputation: 18737
I have done something like that before. I used a panel for that, and the code was like:
private void panel1_MouseDoubleClick(object sender, System.Windows.Forms.MouseEventArgs e)
{
System.Windows.Forms.TextBox txtbox = new System.Windows.Forms.TextBox();
txtbox.Location = e.Location; //Creates the textbox where user double clicked
panel1.Controls.Add(txtbox);
txtbox.Focus();
txtbox.Visible = true;
}
You can set the Left
and Top
of the textbox wherever you want. Inorder to store the textboxes you will have to create a table and store the details of each textbox (content,position,etc). And while opening the form later, these details should be read from the table and create those textboxes.
For saving the textboxes:
foreach (Control c in panel1.Controls)
{
if (c.GetType().Name == "TextBox")
{
//Save the texbox content,Leftposition,RightPosition,etc to database
}
}
On the form_Load
:
private void MyForm_Load(object sender, EventArgs e)
{
//Retrieve all textbox details from table as dataset.
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
System.Windows.Forms.TextBox txtbox = new System.Windows.Forms.TextBox();
txtbox.Text = ds.Tables[0].Rows[i]["Content"].ToString();
txtbox.Left = Convert.ToInt32(ds.Tables[0].Rows[i]["Left"].ToString());
txtbox.Top = Convert.ToInt32(ds.Tables[0].Rows[i]["Top"].ToString());
panel1.Controls.Add(txtbox);
txtbox.Visible = true;
}
}
You will have to create a table like:
Content Left Top
NameA 100 100
NameB 132 241
NameC 242 311
Upvotes: 0
Reputation: 766
....
btn.Click += new Event Handler(AddTextBox);
....
public void AddTextBox(Object o, EventArgs e) {
TextBox newTextBox = new TextBox();
newTextBox.Location = new Point(...);
Controls.Add(newTextBox);
}
As for saving the controls for future use, you could simply save the control type and position. As already mentioned this could be done in a database, or you could opt for xml or simply a text file of your own formatting.
Upvotes: 0
Reputation: 31
To archieve that, you have to make your own structure to save in settings or any external xml file. For example, you can make a class with fields "name" and "text", then create a generic list of that class with List listOfBoxes , serialize it into a XML file for later de-serialization.
When you read the XML file, just loop throu the list and create a textbox and label for every item, asigning the name and text and after creating and adding to the parent container control, I recommand you to use flowlayout as a container control, or you will have to calculate de x-y coordinates of the control
if you dont know how to do any of those steps just ask me.
Insted of using files or settings, you can use also a databse table to store the controls data
Upvotes: 1