hello friends I am new to .net. I have created a form in C# with various check buttons and text buttons for user options. I cannot collect all this data and write to a file.
I was able to put code that wrote to a file in the program.cs file. But now how do I write code to collect the text box data and checkbutton option (which are in the form.cs files)?
So I wanna know how to access the checkbutton options and text buttons from program.cs.
Is there another way to do this? i.e collect user data from texbox and checkbuttons and put to a file?
Please help.
Thakns
Please gimme some hint.
Loading
Shailendra KumarPosted Jun 4, 2014, 8:27 AM
You can iterate your controls from the forms control collection and use XElement to Collect and store it
ControlValue = new Dictionary();
Below is the example. Hope it helps
using System.Xml.Linq;
Dictionary
private void IterateControls(Control Ctrl)
{
foreach (Control CtrlChild in Ctrl.Controls)
{
if (CtrlChild is TextBox )
{
ControlValue.Add(CtrlChild.Name,CtrlChild.Text);
}
else if (CtrlChild is CheckBox )
{
ControlValue.Add(CtrlChild.Name,CtrlChild.CheckState);
}
else
{IterateControls(CtrlChild);} // This will iterate for the controls placed over over other container controls like panel or others
}
}
private void CollectData()
{
foreach (Control Ctrl in this.Controls)
{
IterateControls(Ctrl);
}
XElement root = new XElement("DETAILS",
from keyValue in ControlValue
select new XElement(keyValue.Key, keyValue.Value));
root.Save(Application.StartupPath + Path.DirectorySeparatorChar + "Settings.xml");
}
carle laiPosted Jan 7, 2010, 10:09 PM
Kirtan PatelPosted Jan 2, 2010, 6:19 AM