Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Little help with my form

Status
Not open for further replies.

BrasilianGuy

IS-IT--Management
Oct 27, 2005
25
US
With my code below I'm trying to accomplish:

1. Change the color of the button for checked and uncheked - Done

2. Save the value of the button into a xml file if button is clicked - Not working

3. delete the value from the xml file if button is uncheked - Not done

4. retrieve the value to repopulate the form everytime I open it! - Not done


steps 3 and 4 I can work on them latter but now I'm stuck on step number 2

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
{
if (checkBox1.BackColor == Color.Red)
checkBox1.BackColor = Color.White;
else
checkBox1.BackColor = Color.Red;
}
}

private void button2_Click(object sender, EventArgs e)
{
{
if (checkBox1.Checked)
{
XmlDocument docXML = new XmlDataDocument();

docXML.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<Numbers><Number>14</Number></Numbers>");

docXML.Save("Employees.xml");
}
else
{
//user didn't check the box
}
}

}


isn't it supposed to create a file in the root called employees.xml??
 
it should save a document in the application root, yes. this will be something like "<appfolder>\bin\debug".

try:

Code:
docXML.Save("\Employees.xml");

and go from there.

mr s. <;)

 
Thanks.... I was looking at the wrong place.

can you halp me with this code here:

private void Form1_Load(object sender, EventArgs e)
{
XmlDocument docXML = new XmlDataDocument();
docXML.Load("employee.xml");

XmlDocument numberstatus = new XmlDataDocument();

if
(numberstatus = docXML.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<Numbers><Number>14</Number><status>Checked</status></Numbers>"));

checkBox1.Checked = true;

else
checkBox1.Checked = false;


}

What I'm trying to do is to read the xml file when the form is loaded and set the check box status according to the value in the XML.

Thanks in advance

Marcus
 
The problem is - your form architecture is not geared towards what you need.

personally, I would create a datatransfer class that simply reads and writes values. Then you can simply use it something like this:

DataTransferClass dtc = new DataTransferClass();

dtc.OpenFile("employees.xml");

checkBox1.Checked = Convert.ToBoolean(dtc.ReadSetting("CheckBox1State"));


private void button2_Click(object sender, EventArgs e)
{
dtc.WriteSetting("CheckBox1State", checkBox1.Checked.ToString());
}


the class should look something like this:

public class DataTransferClass
{
public DataTransferClass()
{
}

private XmlDocument doc = new XmlDocument();
public string FilePath;

public bool OpenFile(string filepath)
{
try
{
doc.Load(filepath);
FilePath = filepath;

XmlNode node = doc.SelectSingleNode("//BaseNode");

return (node != null);
}
catch(Exception ex)
{
return false;
}
}
}

public void WriteSetting(string key, string val)
{
XmlNode node = doc.SelectSingleNode("//BaseNode");

if (node == null)
{
throw new IOException("Could not find XmlNode appSettings");
}

XmlElement elem = (XmlElement)node.SelectSingleNode(string.Format("//add[@key='{0}']", key));

if (elem != null)
{
// add value for key
elem.SetAttribute("value", val);
}
else
{
// key was not found so create the 'add' element
// and set it's key/value attributes
elem = doc.CreateElement("add");
elem.SetAttribute("key", key);
elem.SetAttribute("value", val);
node.AppendChild(elem);
}
doc.Save(FilePath);
}


public string ReadKey(string key)
{
XmlNodeList nodes = doc.SelectNodes("//add");

if (nodes != null)
{
for (int i = 0; i < nodes.Count; i++)
{
XmlNode node = nodes;

if (node.Attributes.Count == 2)
{
if (node.Attributes[0].InnerText == key)
{
return node.Attributes[1].InnerText;
}
}
}
}

return "";
}
}


That piece of code was partially borrowed from somewhere but I don't remember who wrote it! So my appologies to them!

The xml file would essentially look like an app.config file

<?xml version="1.0" encoding="utf-8"?>
<BaseNode>
<add key="CheckBox1State" value="True" />
</BaseNode>

Hope that gets you somewhere!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top