Reputation: 67
I have one problem I can't fix:
I want my class admin
to contain this method:
public void OpretSpejder()
{
if (!(string.IsNullOrEmpty(Snavn_txt.Text)))
if (!(string.IsNullOrEmpty(Senavn_txt.Text)))
if (!(string.IsNullOrEmpty(Sa_txt.Text)))
if (!(string.IsNullOrEmpty(Scpr_txt.Text)))
{
XmlDocument doc = new XmlDocument();
doc.Load(@"Spejder.xml");
var nodeCount = 0;
using (var reader = XmlReader.Create(@"Spejder.xml"))
{
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element &&
reader.Name == "Spejder")
{
nodeCount++;
}
}
}
nodeCount++;
XmlElement Spejder = doc.CreateElement("Spejder");
Spejder.SetAttribute("ID", nodeCount.ToString());
XmlNode Navn = doc.CreateElement("Navn");
Navn.InnerText = Snavn_txt.Text;
Spejder.AppendChild(Navn);
XmlNode Efternavn = doc.CreateElement("Efternavn");
Efternavn.InnerText = Senavn_txt.Text;
Spejder.AppendChild(Efternavn);
XmlNode Alder = doc.CreateElement("Alder");
Alder.InnerText = Sa_txt.Text;
Spejder.AppendChild(Alder);
XmlNode Cpr = doc.CreateElement("Cpr");
Cpr.InnerText = Scpr_txt.Text;
Spejder.AppendChild(Cpr);
doc.DocumentElement.AppendChild(Spejder);
doc.Save(@"Spejder.xml");
Snavn_txt.Text = String.Empty;
Senavn_txt.Text = String.Empty;
Sa_txt.Text = String.Empty;
Scpr_txt.Text = String.Empty;
MessageBox.Show("Spejder Oprettet");
}
}
and I want the method to be executed in a button click in another form. But it cannot recognize my textboxes.. that's the only problem.
For each instance it just says: The name 'Scpr_txt' does not exist in the current context.
Upvotes: 3
Views: 200
Reputation: 276
There are two ways of doing this:
the first way is to add parameters to your OpretSpejder
function.
the second way is to make a public Form2
in your Form1
like this:
Form2 f2 = new Form2();
then you can use:
f2.textbox1.text
f2.textbox2.text
...
Upvotes: 1
Reputation: 148150
I won't couple the presentation layer class with my classes. Instead of accessing a TextBox directly you should pass the text to a method.
public void OpretSpejder(string Snavn_txt, string Sa_txt, string Scpr_txt )
{
if (!(string.IsNullOrEmpty(Senavn_txt)))
if (!(string.IsNullOrEmpty(Sa_txt)))
if (!(string.IsNullOrEmpty(Scpr_txt)))
{
Upvotes: 2
Reputation: 441
It is trying to tell you that it doesn't recognize where the TextBox exists. You could pass a reference to the textbox into the method.
Upvotes: 0