Quest'approccio è terribile, qual è il risultato che vorresti avere? Ci sono mille modi sicuramente migliori per eseguire il compito. Vorresti forse salvare i valori di quelle textBox su file e caricarli in seguito?
Follow along with the video below to see how to install our site as a web app on your home screen.
Nota: This feature may not be available in some browsers.
<contatto>
<textbox1>Prova1</textbox1>
<textbox2>Prova2</textbox2>
<textbox3>Prova3</textbox3>
<textbox4></textbox4>
<textbox5>Prova5</textbox5>
<textbox6>Prova6</textbox6>
<textbox7></textbox7>
<textbox8></textbox8>
<textbox9>Prova9</textbox9>
<textbox10>Prova10</textbox10>
</contatto>
// FormBinding.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Windows.Forms;
namespace FormDatastore
{
internal class FormBinding
{
public string FormName { get; }
public string FileName { get; }
public string LastError { get; private set; }
public bool AutoSave { get; set; }
private readonly List<TextBox> textBoxes;
public FormBinding(string formName, string fileName)
{
FormName = formName;
FileName = fileName;
textBoxes = new List<TextBox>();
}
public void Bind(params TextBox[] textBoxes)
{
foreach (TextBox textBox in textBoxes)
{
textBox.TextChanged += TextChanged;
this.textBoxes.Add(textBox);
}
}
private void TextChanged(object sender, EventArgs e)
{
if (AutoSave)
Save();
}
public bool Load()
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load(FileName);
XmlNode form = doc.SelectSingleNode("/" + FormName);
foreach (XmlNode childItem in form.ChildNodes)
foreach (TextBox t in textBoxes)
if (t.Name == childItem.Name)
t.Text = childItem.InnerText;
return true;
}
catch (Exception ex)
{
LastError = ex.Message;
return false;
}
}
public bool Save()
{
try
{
string formXml = this.GetXML();
File.WriteAllText(FileName, formXml);
return true;
}
catch (Exception ex)
{
LastError = ex.Message;
return false;
}
}
private string GetXML()
{
XmlDocument doc = new XmlDocument();
XmlNode form = doc.CreateElement(FormName);
foreach (TextBox t in textBoxes)
{
var childItem = doc.CreateElement(t.Name);
childItem.InnerText = t.Text;
form.AppendChild(childItem);
}
doc.AppendChild(form);
return doc.InnerXml;
}
}
}
// ...
using FormDatastore;
// ...
private FormBinding binding;
// Tuo evento Shown del form
private void FormN_Shown(object sender, EventArgs e)
{
binding = new FormBinding("Contatto", "abc.xml");
binding.Bind(textBox1, textBox2, textBox3); // ...
binding.Bind(textBox4); // ...
binding.AutoSave = true;
binding.Load();
}
AutoSave
a true la classe si occuperà di aggiornare il file in automatico ad ogni modifica di una delle textbox, altrimenti puoi impostarlo a false o non impostarlo per niente e dovrai chiamare binding.Save();
manualmente quando desideri farlo. Il metodo binding.Load()
invece legge il contenuto del file xml e lo carica nelle textBox associate. Esempio di XML prodotto dall'esempio sopra:<Contatto>
<textBox1>abc</textBox1>
<textBox2>def</textBox2>
<textBox3>ghi</textBox3>
<textBox4>jkl</textBox4>
</Contatto>