Duplicated Userforms

  • Thread starter Thread starter kocsism
  • Start date Start date
K

kocsism

Guest
I have 2 forms in VIsual Studio, on the first Form I have buttons (pre-definied) and on the second dinamicly created ased on the content of an XML file.

Form one looks like this:

namespace Form1
{

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
label1.Text = "Hello, " + System.DirectoryServices.AccountManagement.UserPrincipal.Current.DisplayName;
}

private void Button1_Click(object sender, EventArgs e)
{

string Username = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();

var result = Username.Length <= 4 ? "" : Username.Substring(4);

string path = $"C:\\Users\\{result}\\Documents\\config.xml";


XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlTextReader xtr = new XmlTextReader(path);

string string_title = "";
while (xtr.Read())
{
if (xtr.NodeType == XmlNodeType.Element && xtr.Name == "name")
{


string_title += xtr.ReadElementString() + Environment.NewLine;

Form3 frm3 = new Form3(string_title);
{
frm3.ShowDialog();
//this.Close();
}
}



}


XmlNodeList nodeList;
XmlNode root = doc.DocumentElement;

nodeList = root.SelectNodes("descendant::Template[name]");


Th second Form is something like this:

namespace e2e_template
{
public partial class Form3 : Form
{
public Form3(string data)
{
string Username = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();

string Result = Username.Length <= 4 ? "" : Username.Substring(4);

string Path = $"C:\\Users\\{Result}\\Documents\\config.xml";
int x = 0;
int y = 0;
XmlDocument Xd = new XmlDocument();
Xd.Load($"C:\\Users\\{Result}\\Documents\\config.xml");

XmlNode testNode = Xd.SelectSingleNode("//Templates");
XmlNodeList list = testNode.SelectNodes("//template");

foreach (XmlNode xn in list)
{
string name = xn.SelectSingleNode("name").InnerText.Trim();

Button button = new Button
{
Text = name,
Width = 250,
Height = 50,
Left = x + 8,
Top = y,
};
button.Click += new EventHandler(Button_Click);
Controls.Add(button);
y += button.Height + 5;
}
InitializeComponent();
}

private void Button_Click(object sender, EventArgs e)
{
Form4 form = new Form4();
Button b = sender as Button;
string name = b.Text;
Create(form, name);
form.ShowDialog();
}

private void Create(Form form, string name)
{
string Username = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();

string Result = Username.Length <= 4 ? "" : Username.Substring(4);

string Path = $"C:\\Users\\{Result}\\Documents\\config.xml";
int x = 0;
int y = 0;
int b = 0;
int c = 0;
XmlDocument Xd = new XmlDocument();
Xd.Load($"C:\\Users\\{Result}\\Documents\\config.xml");

XmlNode testNode = Xd.SelectSingleNode("//Templates");
XmlNodeList list = testNode.SelectNodes("//template");

foreach (XmlNode node in list)
{
foreach (XmlNode xnode in node.ChildNodes)
{
if (xnode.InnerText == name)
{
XmlNode newnode = xnode.ParentNode;
foreach (XmlNode itemnode in newnode.ChildNodes)
{
if (itemnode.Name == "item")
{
Label label = new Label
{

Text = itemnode.InnerText,
Width = 250,
Height = 75,
Left = x + 20,
Top = y + 50,

};
TextBox txt = new TextBox {
Width = 300,
Height = 75,
Left = c + 20,
Top = b + 65,

};
form.Controls.Add(label);
form.Controls.Add(txt);
y += label.Height + 5;
b += txt.Height + 60;
txt.BringToFront();

}
}
}
}
}
}

private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
}
}


but when I want to close the second form instead of showing the first form it is re-opening the second form as many times as many buttons are dinamicly created? How should I change the code to only have the second form drawn, but it would still read the data from the xml file and would create the buttons like it does now?

Continue reading...
 
Back
Top