c# - 用户表单上的动态按钮
问题描述
我正在尝试在 C# 中设置一个小型桌面应用程序,它将读取一个 xml 文件并基于文件内的节点生成按钮。
XML 文件具有以下节点:
<?xml version="1.0" encoding="UTF-8"?>
<templates>
<version>1.0</version>
<incident>
<template id="1">
<name>some topic</name>
<description>Use this for something</description>
<item id="1">field 1</item>
<item id="2">field 2</item>
<item id="3">field 3</item>
<item id="4">fiel 4</item>
</template>
我有一个带有预定义按钮的用户表单(Form1),它正在工作,但是其中一个按钮应该读取这个 xml 文件并打开一个新的用户表单(Form3),上面有动态分配的按钮。
我在按钮下有这段代码:
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\\template_manager\\config.xml";
//MessageBox.Show(path);
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 string_title = xtr.ReadElementString();
string_title += xtr.ReadElementString() + Environment.NewLine;
//MessageBox.Show("Title: " + Environment.NewLine + string_title);
//var Form3 = new Form3(string_title);
//Form3.show();
Form3 frm3 = new Form3(string_title);
{
frm3.ShowDialog();
}
}
在表格 3 上,我有以下代码:
string dataFromForm1;
public Form3(string data)
{
//getting Windows Logon ID from the system and assigning it to a string kind variable
string Username = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
//-re-assigning windows logon ID to a variable
//string str = Username;
//Trimming the logon ID (removing "G0X/")
string result = Username.Length <= 4 ? "" : Username.Substring(4);
//passing the path of the xml file to a variable named path
string path = $"C:\Users\\{result}\\Documents\\e2e_template_manager\\config.xml";
//MessageBox.Show(path);
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNodeList templates = doc.SelectNodes(path);
int x = 10;
int y = 10;
foreach (XmlNode template in templates)
{
string name = template.SelectSingleNode("name").InnerText.Trim();
Button button = new Button
{
Text = name,
Left = x,
Top = y
};
Controls.Add(button);
y += button.Height + 5;
}
InitializeComponent();
}
public void show()
{
MessageBox.Show("Title: " + dataFromForm1);
}
}
你能帮我解决这个问题吗?动态生成的按钮也应指向 Form4 或 Form5。
解决方案
正如评论中提到的,您没有说明问题是什么(您期望发生的事情没有发生)。我的猜测是您的 Form3 没有出现。它没有出现是因为你没有告诉它出现。
//var Form3 = new Form3(string_title);
//Form3.show();
Form3 frm3 = new Form3(string_title);
{ // BTW: these braces are not necessary
frm3.ShowDialog();
}
注释掉的版本更接近,除非您想在您创建的表单上调用Show :
var frm3 = new Form3(string_title);
frm3.Show(this); // ...and give it the parent window
推荐阅读
- sql - 限制从一月到昨天的时间戳的随机值
- birt - BIRT REPORTING in 我需要删除日期的时间
- c - 循环检查是否输入了两个字符之一永远运行
- c# - foreach OrderBy 有条件的
- python - 我可以对一个较小的字典进行排序,但是对于较大版本的字典会得到一个 TypeError
- c# - 对笔记本的 GraphServiceclient 请求,查询选项“select”需要“name”而不是“displayname”
- java - RxJava 在 Single 异步后执行 Completable
- r - 该行中所有值的总和
- vue.js - 如何在 Vue CLI 3 中配置静态资产文件夹并设置预取?
- python - 如何将表单对象传递给html并在django中使用对象中的每个项目